Programming-Idioms

History of Idiom 22 > diff from v12 to v13

Edit summary for version 13 :

Version 12

2015-08-20, 16:21:56

Version 13

2015-08-20, 16:22:38

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