Programming-Idioms

History of Idiom 22 > diff from v23 to v24

Edit summary for version 24 :
New Rust implementation by user [programming-idioms.org]

Version 23

2015-11-30, 12:37:26

Version 24

2015-12-30, 21:19:20

Idiom #22 Convert string to integer

Extract integer value i from its string representation s (in radix 10)

Idiom #22 Convert string to integer

Extract integer value i from its string representation s (in radix 10)

Code
let i = match s.parse::<i32>() {
  Ok(i) => i,
  Err(e) => {
    -1
  }
};
Comments bubble
s is parsed to 32-bits signed integer here, but you can change number type to whatever you want.
-1 is used here as a fallback value, but any error handling instructions can be used.