How to convert a string (String or &str) to a number (int) in Rust?
Hello everyone! I just started learning Rust, switched from Python, and encountered a problem that stumped me. I need to read a number from standard input or from a text file and perform arithmetic operations on it.
In Python this is done in one line: `number = int(input())`. But in Rust I get compilation errors.
Here's my code that doesn't work:
```rust
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
let number: i32 = input; // Error! Type mismatch
println!("Entered number: {}", number + 10);
}
```
The compiler gives an error: `expected i32, found struct String`. I understand that `read_line` returns a string, not a number. But how do I