2
考慮的例子(不建):爲什麼`name = * name.trim();`給我`預期的結構`std :: string :: String`,找到str`?
use std::io::{self, Write};
fn main() {
io::stdout().write(b"Please enter your name: ");
io::stdout().flush();
let mut name = String::new();
io::stdin().read_line(&mut name);
name = *name.trim();
println!("Hello, {}!", name);
}
爲什麼我得到下面的錯誤?
error[E0308]: mismatched types
--> src/main.rs:8:12
|
8 | name = *name.trim();
| ^^^^^^^^^^^^ expected struct `std::string::String`, found str
|
= note: expected type `std::string::String`
= note: found type `str`
值得注意的是'trim'不是一個'String'方法,它實際上是一個'''通過'Deref'特徵的奇蹟繼承的'str'方法。 – user4815162342