我用String ::從( 「串」)得到一個字符串鏽字符串不是字符串
let dog = String::from("dog")
和
dog == String::from("dog")
返回false。即使在模式匹配。
match dog.as_ref() {
"dog" => println!("Dog is a dog"), //no output
_ => println!("Dog is not a dog")
}
出什麼問題了?
例
use std::io;
fn main() {
let mut sure = String::from("");
println!("Hello, world!");
println!("Are you sure(Y/N)");
io::stdin().read_line(&mut sure).expect("Failed");
println!("sure {}", sure);
let surely = {sure == String::from("Y")};
println!("surely {} ", surely); //this line output is "surely false"
if surely {
dog_loop("HA");
}
}
無法在[Rust Playground]中重現(https://play.rust-lang.org/?gist=5e1f563dd638e1358f409e07c8d7931c&version=stable&backtrace=0)。你可以創建一個[mcve]嗎? – Aurora0001
現在編輯。我添加了例子。 –