我一直收到此错误。我认为这是因为我遮蔽了answer
,因为当我注释掉那一部分时,我不再收到错误。我不明白为什么会这样。
fn main() {
let mut answer = String::new();
let num = 40;
if num % 2 == 0 {
answer.push_str("fact2 ");
}
if num % 5 == 0 {
answer.push_str("fact5 ");
}
let answer = answer.trim();
answer.push_str("bob was here");
println!("{}", answer);
}
error[E0599]: no method named `push_str` found for type `&str` in the current scope
--> src/main.rs:13:12
|
13 | answer.push_str("bob was here");
| ^^^^^^^^
答案 0 :(得分:2)
答案 1 :(得分:2)
您是对的,let answer = answer.trim();
是问题所在。它将answer
设置为类型&str
,并且push_str
是为可变的String
定义的。
您可以通过将该行更改为以下内容来修复它:
answer = answer.trim().to_string();