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

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

屬于特征的對象的向量

屬于特征的對象的向量

Git
慕的地6264312 2019-10-21 12:56:04
考慮以下代碼:trait Animal {    fn make_sound(&self) -> String;}struct Cat;impl Animal for Cat {    fn make_sound(&self) -> String {        "meow".to_string()    }}struct Dog;impl Animal for Dog {    fn make_sound(&self) -> String {        "woof".to_string()    }}fn main () {    let dog: Dog = Dog;    let cat: Cat = Cat;    let v: Vec<Animal> = Vec::new();    v.push(cat);    v.push(dog);    for animal in v.iter() {        println!("{}", animal.make_sound());    }}編譯器告訴我這v是Animal我嘗試推送cat(類型不匹配)時的向量那么,如何創建屬于特征的對象的向量并在每個元素上調用相應的trait方法?
查看完整描述

2 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

Vec<Animal>是不合法的,但是編譯器無法告訴您,因為類型不匹配以某種方式將其隱藏。如果我們刪除對的調用push,則編譯器將給我們以下錯誤:


<anon>:22:9: 22:40 error: instantiating a type parameter with an incompatible type `Animal`, which does not fulfill `Sized` [E0144]

<anon>:22     let mut v: Vec<Animal> = Vec::new();

                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

之所以不合法,是因為a 在內存中連續Vec<T>存儲了許多T對象。但是,Animal是一個特征,特征沒有大小(不能保證a Cat和a Dog具有相同的大?。?。


要解決此問題,我們需要在中存儲大小為一定的內容Vec。最直接的解決方案是將這些值包裝在Box,即中Vec<Box<Animal>>。Box<T>具有固定大?。ㄈ绻鸗是特征,則為“胖指針”,否則為簡單指針)。


這是一個工作main:


fn main () {

    let dog: Dog = Dog;

    let cat: Cat = Cat;

    let mut v: Vec<Box<Animal>> = Vec::new();

    v.push(Box::new(cat));

    v.push(Box::new(dog));

    for animal in v.iter() {

        println!("{}", animal.make_sound());

    }

}


查看完整回答
反對 回復 2019-10-21
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您可以使用參考特征對象&Animal借用元素并將這些特征對象存儲在中Vec。然后,您可以枚舉它并使用特征的界面。


Vec通過&在trait前面添加a 來更改的通用類型將起作用:


fn main() {

    let dog: Dog = Dog;

    let cat: Cat = Cat;

    let mut v: Vec<&Animal> = Vec::new();

    //             ~~~~~~~

    v.push(&dog);

    v.push(&cat);

    for animal in v.iter() {

        println!("{}", animal.make_sound());

    }

    // Ownership is still bound to the original variable.

    println!("{}", cat.make_sound());

}

如果您可能希望原始變量保留所有權并在以后重新使用,那就太好了。


請牢記上述情況,您不能轉讓的所有權,dog或者cat因為Vec曾在相同范圍借用了這些具體實例。


引入新的作用域可以幫助處理特定情況:


fn main() {

    let dog: Dog = Dog;

    let cat: Cat = Cat;

    {

        let mut v: Vec<&Animal> = Vec::new();

        v.push(&dog);

        v.push(&cat);

        for animal in v.iter() {

            println!("{}", animal.make_sound());

        }

    }

    let pete_dog: Dog = dog;

    println!("{}", pete_dog.make_sound());

}


查看完整回答
反對 回復 2019-10-21
  • 2 回答
  • 0 關注
  • 529 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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