- i32
- parse
- expect
- ::<>()
Convert string to (integer) number - parse, turbofish
- In the printing we won't see the difference, but we can do numberical operations on numbers.
- We can either define the expect type next to the variable name where we are assigning to.
- Or we can use the so-called turbofish operator ::<>() as we do in the second example.
examples/convert/string-to-int/src/main.rs
fn main() { let text = "23"; println!("'{}'", text); println!(); let number: i32 = text .parse() .expect("Could not convert to i32"); println!("'{}'", number); println!("'{}'", number + 1); println!(); let number = text .parse::<i32>() .expect("Could not convert to i32"); println!("'{}'", number); println!("'{}'", number + 1); }
'23' '23' '24' '23' '24'