亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我為另一個特征實現了一個特征,但是無法從兩個特征中調用方法

我為另一個特征實現了一個特征,但是無法從兩個特征中調用方法

Git
呼啦一陣風 2019-12-06 10:10:04
我有一個特質Sleep:pub trait Sleep {    fn sleep(&self);}我可以為每個結構提供不同的睡眠實現方式,但事實證明,大多數人以很少的方式睡眠。您可以在床上睡覺:pub trait HasBed {    fn sleep_in_bed(&self);    fn jump_on_bed(&self);}impl Sleep for HasBed {    fn sleep(&self) {        self.sleep_in_bed()    }}如果您在露營,則可以在帳篷里睡覺:pub trait HasTent {    fn sleep_in_tent(&self);    fn hide_in_tent(&self);}impl Sleep for HasTent {    fn sleep(&self) {        self.sleep_in_tent()    }}有一些奇怪的情況。我有一個可以睡在墻上的朋友,但是大多數情況下,大多數人會遇到一些簡單的情況。我們定義一些結構,然后讓它們入睡:struct Jim;impl HasBed for Jim {    fn sleep_in_bed(&self) {}    fn jump_on_bed(&self) {}}struct Jane;impl HasTent for Jane {    fn sleep_in_tent(&self) {}    fn hide_in_tent(&self) {}}fn main() {    use Sleep;    let jim = Jim;    jim.sleep();    let jane = Jane;    jane.sleep();}哦!編譯錯誤:error[E0599]: no method named `sleep` found for type `Jim` in the current scope  --> src/main.rs:44:9   |27 | struct Jim;   | ----------- method `sleep` not found for this...44 |     jim.sleep();   |         ^^^^^   |   = help: items from traits can only be used if the trait is implemented and in scope   = note: the following trait defines an item `sleep`, perhaps you need to implement it:           candidate #1: `Sleep`error[E0599]: no method named `sleep` found for type `Jane` in the current scope  --> src/main.rs:47:10   |34 | struct Jane;   | ------------ method `sleep` not found for this...47 |     jane.sleep();   |          ^^^^^   |   = help: items from traits can only be used if the trait is implemented and in scope   = note: the following trait defines an item `sleep`, perhaps you need to implement it:           candidate #1: `Sleep`該編譯器錯誤很奇怪,因為如果某個特性實現了另一個特性時出現了問題,我希望在做完該特性時能早點聽到,而不是在嘗試使用結果時在程序的最底層。在此示例中,只有2個結構和2種睡眠方式,但是在一般情況下,有許多結構和幾種睡眠方式(但不如結構體那么多)。A Bed主要是針對的實現Sleep,但在一般情況下,a Bed有很多用途,并且可以實現很多事情。唯一立即顯而易見的方法是將其轉換impl Sleep for...為可構造自身使用的宏,但這似乎很麻煩而且很糟糕。
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 543 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號