Rust: Convert Str to Int

Rust: Convert Str to Int

  • rust
  • 3 mins read

Introduction

In this tutorial, we will learn how to convert a string (str) to an integer (int) in Rust. We will cover different methods for converting a string to an int and provide examples for each method.

Method 1: Using the parse() method

The parse() method is a built-in method in Rust that can convert a string to an int. It takes an optional radix (base) as an argument, with a default value of 10.

Example:

fn main() {
    let num_str = "42";
    let num = num_str.parse::<i32>();
    match num {
        Ok(n) => println!("The number is: {}", n),
        Err(e) => println!("Error: {}", e),
    }
}

Output:

The number is: 42

The .parse() method returns a Result<T, ParseIntError> where T is the type you want to parse it to, in this case i32. The Result can be either Ok variant containing the int value or an Err variant containing an error message.

You can use the match statement to check if the Result is Ok or Err and then you can handle the value or error message accordingly. To check if string is number, see this tutorial.

Method 2: Using the trim().parse() method

If the string contains whitespaces, you can use the trim() method to remove them before calling parse() method.

Example:

fn main() {
    let num_str = " 42 ";
    let num = num_str.trim().parse::<i32>().unwrap();
    println!("The number is: {}", num);
}

Output:

The number is: 42

Method 3: Using the str::parse() method

You can also use the str::parse() method to convert a string to an int.

Example:

fn main() {
    let num_str = "42";
    let num = str::parse::<i32>(num_str).unwrap();
    println!("The number is: {}", num);
}

Output:

The number is: 42

Conclusion

In this tutorial, we learned how to convert a string to an int in Rust using the parse(), trim().parse() and str::parse() methods. Remember that the parse() method will return a Result type, which must be unwrapped to access the actual int value.

FAQ

How can I convert a string to an int in Rust?

You can use the .parse() method on the string value and pass the type you want to parse it to, in this case i32. The .parse() method returns a Result<T, ParseIntError> where T is the type you want to parse it to. You can use the match statement to check if the Result is Ok or Err and then you can handle the value or error message accordingly.

What is the purpose of the match statement when converting a string to an int in Rust?

The .parse() method returns a Result<T, ParseIntError> where T is the type you want to parse it to. The Result can be either Ok variant containing the int value or an Err variant containing an error message. The match statement is used to check if the result is an Ok variant or an Err variant, and then handle the value or error message accordingly.

What should I do if the string I am trying to convert to an int contains whitespaces?

If the string contains whitespaces, you can use the trim() method to remove them before calling the .parse() method. This will ensure that the whitespaces do not affect the int value returned by the .parse() method.