Rust : Convert String to Integer

Rust : Convert String to Integer

Convert string to integer
Rust

Convert string to integer

Do you want to convert string to integer? Here are the methods to extract integer value i from its string representation s.
1234” to 1234

Method 1:

let i = match s.parse::<i32>() {
  Ok(i) => i,
  Err(_e) => -1,
};

Above s is parsed to 32-bits signed integer here (change number type if needed). -1 is used here as a fallback value, but any error handling instructions can be used.


Method 2:

let i: i32 = s.parse().unwrap_or(0);

This explicitly sets the value to 0 if it can’t be parsed to an integer.


Method 3:

let i = s.parse::<i32>().unwrap();

This panics if s is not a valid number. s is parsed to 32-bits signed integer here (change number type if needed).


I : Integer := Integer'Value (s);

For more information Click Here

Click here to see more articles on rust

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

%d bloggers like this: