I want to concatenate a String.
In my opinion the following code should give some sort of a panic or compiler error.
let mut s = String::from("abc").push_str("x");
println!("{:?}", s); // Prints ()
But this code works:
let mut s = String::from("abc");
s.push_str("x");
println!("{:?}", s); // Prints "abcx"
In my opinion the following code should give some sort of a panic or compiler error.
Why should it? This code is perfectly legal.
s
is now a unit value ()
and because you are using {:?}
which is the Debug
output it prints ()
.
This is a good time to introduce clippy to you. With cargo clippy
you receive the following warning:
warning: this let-binding has unit value
--> src/main.rs:2:5
|
2 | let mut s = String::from("abc").push_str("x");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `String::from("abc").push_str("x");`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
= note: `#[warn(clippy::let_unit_value)]` on by default
which indicates that something is fishy in your code.