Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #137 Check if string contains only digits

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

let b = s.chars().all(char::is_numeric);
let b = s.bytes().all(|c| c.is_ascii_digit());

only return true for ASCII digits
let chars_are_numeric: Vec<bool> = s.chars()
				.map(|c|c.is_numeric())
				.collect();
let b = !chars_are_numeric.contains(&false);

Note that is_numeric is not just 0 -> 9.
See documentation for more info.
B := (for all Char of S => Char in '0' .. '9');

New implementation...
< >
programming-idioms.org