I am trying to pass a string by reference and manipulate the string in the function:
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);
}
I have looked at examples on borrowing and mutibility. Also tried (*s).push('3'), but got
error: type `collections::string::String` does not implement any method in scope named `push`
I'm sure there's something obvious I'm missing or perhaps more reference material to read, but I'm not sure how to proceed. Thanks!
push_char->pushmust be relatively recent, since I don't recall encountering it the last time I used that part of String functionality. What version are you using (if nightly, from what date)?rustc 0.12.0-nightly (740905042 2014-09-29 23:52:21 +0000)rustc 0.12.0-nightly (740905042 2014-09-29 23:52:21 +0000). The error that result is actually a lot more informative:error: cannot borrow immutable dereference of&-pointer as mutable. Basically it is exactly as @IdolfHatler described!