How to convert an integer (i32, u64, etc.) to a String or &str in Rust?
Hello! I just started learning Rust, and I have a seemingly simple problem. I'm writing a small program that takes a number from the user, multiplies it by 2, and outputs the result along with text. But I can't figure out how to properly convert an integer to a string to concatenate it with other text.
Here's my code:
```rust
fn main() {
let number: i32 = 42;
let result = number * 2;
// Trying to output: "Result: 84"
let message = "Result: " + result; // Error!
println!("{}", message);
}
```
The compiler gives an error: "cannot add `i32` to `&str`". I understand that in Rust you can't just add a string and a number like in Python or JavaScript. I tried using `to_string()`, but then I get something like `"Result: " + result.t`
No answers yet.
Log in to leave an answer