我試圖找出如何匹配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?
如何在Rust中將字符串與字符串文字進(jìn)行匹配?
青春有我
2019-12-02 11:10:56