我正在尝试通过引用传递一个字符串并在函数中操作该字符串:
fn manipulate(s: &mut String) {
// do some string manipulation, like push
s.push('3'); // error: type `&mut collections::string::String`
// does not implement any method in scope named `push`
}
fn main() {
let mut s = "This is a testing string".to_string();
manipulate(&s);
println!("{}", s);
}
我看过关于 borrowing 的例子和 mutibility .还尝试了 (*s).push('3')
,但得到了
error: type `collections::string::String` does not implement any method in scope named `push`
我确定我缺少一些明显的东西或者可能有更多引用资料需要阅读,但我不确定如何继续。谢谢!
最佳答案
您的代码在最新版本的 rustc 上稍作修改即可运行。
fn manipulate(s: &mut String) {
s.push('3');
}
fn main() {
let mut s = "This is a testing string".to_string();
manipulate(&mut s);
println!("{}", s);
}
关于rust - 通过引用传递字符串并操作字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26151324/