How to Convert a String to int in Rust

Created
Modified

Using parse Method

The str::parse::<T>() method parses this string slice into another type. For example,

fn main() {
  let s = "100".to_string();
  println!("{}", s.parse::<i32>().unwrap());

  match "10a".parse::<i32>() {
    Ok(n) => println!("Ok"),
    Err(e) => println!("{}", e),
  }
}
100
invalid digit found in string

Related Tags

#string# #int#