'String allocation behaving strangely
This compiles fine (from the rust lang book):
fn main() {
let mut s = String::from("hello");
s.push_str(", world");
println!("{}", s);
}
This does not:
fn main() {
let mut s = String::from("hello").push_str(", world");
println!("{}", s);
}
The strange thing is that the error occurs in the println! statement:
3 | println!("{}", s); |
^()cannot be> formatted with the default formatter
I'm new to this and trying to push the envelope a little bit, so can someone clarify this?
Solution 1:[1]
push_str() does not have a return value, i.e. it returns () which cannot be printed. So by appending it s gets type empty tuple and not String anymore.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Unlikus |
