Logo

Programming-Idioms

History of Idiom 22 > diff from v25 to v26

Edit summary for version 26 by :
[Rust] Comment emphasize

Version 25

2015-12-30, 21:19:56

Version 26

2015-12-30, 21:21:35

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: i32 = s.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%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
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