Logo

Programming-Idioms

History of Idiom 137 > diff from v19 to v20

Edit summary for version 20 by Alfriadox:
[Rust] Docs

Version 19

2017-12-06, 13:55:13

Version 20

2017-12-06, 13:56:45

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Code
let chars_are_numeric: Vec<bool> = s.chars()
				.map(|c|c.is_numeric())
				.collect();
let b = !chars_are_numeric.contains(&false);
Code
let chars_are_numeric: Vec<bool> = s.chars()
				.map(|c|c.is_numeric())
				.collect();
let b = !chars_are_numeric.contains(&false);
Comments bubble
Note that is_numeric is not just 0 -> 9.
See documentation for more info.
Doc URL
https://doc.rust-lang.org/std/primitive.char.html#method.is_numeric
Demo URL
https://play.rust-lang.org/?gist=9ca489c1de25feb2f09a1770e40b62b1&version=stable
Demo URL
https://play.rust-lang.org/?gist=9ca489c1de25feb2f09a1770e40b62b1&version=stable