Programming-Idioms

History of Idiom 22 > diff from v11 to v12

Edit summary for version 12 :

Version 11

2015-08-20, 16:08:12

Version 12

2015-08-20, 16:21:56

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 s: i32 = i.parse().unwrap_or(0);
Comments bubble
This explicitly sets the value to 0 if it can't be parsed to an integer.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20i%20%3D%20%2242%22%3B%0A%20%20%20%20let%20s%3A%20i32%20%3D%20i.parse().unwrap_or(0)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D%0A&version=stable