第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在Rust中將字符串與字符串文字進(jìn)行匹配?

如何在Rust中將字符串與字符串文字進(jìn)行匹配?

我試圖找出如何匹配StringRust 中的一個。我最初嘗試像這樣進(jìn)行匹配,但是我發(fā)現(xiàn)Rust不能隱式地從轉(zhuǎn)換std::string::String為&str。fn main() {    let stringthing = String::from("c");    match stringthing {        "a" => println!("0"),        "b" => println!("1"),        "c" => println!("2"),    }}這有錯誤:error[E0308]: mismatched types --> src/main.rs:4:9  |4 |         "a" => println!("0"),  |         ^^^ expected struct `std::string::String`, found reference  |  = note: expected type `std::string::String`             found type `&'static str`然后我試圖構(gòu)建新的String對象,因為我無法找到一個功能投下String的&str。fn main() {    let stringthing = String::from("c");    match stringthing {        String::from("a") => println!("0"),        String::from("b") => println!("1"),        String::from("c") => println!("2"),    }}這給了我3次以下錯誤:error[E0164]: `String::from` does not name a tuple variant or a tuple struct --> src/main.rs:4:9  |4 |         String::from("a") => return 0,  |         ^^^^^^^^^^^^^^^^^ not a tuple variant or struct如何String在Rust中實際匹配s?
查看完整描述

3 回答

?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗 獲得超4個贊

你也可以


match &stringthing as &str {

    "a" => println!("0"),

    "b" => println!("1"),

    "c" => println!("2"),

    _ => println!("something else!"),

}


查看完整回答
反對 回復(fù) 2019-12-02
  • 3 回答
  • 0 關(guān)注
  • 2608 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號