char is digit rust
In Rust, you can check if a character (char) is a digit using the char::is_digit
method. This method returns true if the character is a decimal digit (0-9) and false otherwise. Here's an example:
fn main() {
let ch1 = '7';
let ch2 = 'x';
if ch1.is_digit(10) {
println!("{} is a digit", ch1);
} else {
println!("{} is not a digit", ch1);
}
if ch2.is_digit(10) {
println!("{} is a digit", ch2);
} else {
println!("{} is not a digit", ch2);
}
}
Output:
7 is a digit
x is not a digit
In this example, we use the is_digit
method on ch1
and ch2
to check if they are digits. Since '7' is a digit, the first if condition is true and it prints "7 is a digit". However, 'x' is not a digit, so the second if condition is false and it prints "x is not a digit".