To check if a string contains only numeric digits in Rust, you can iterate through each character in the string and check if it is a digit using 'char::is_numeric' method.
In this example,
fn contains_only_numeric_digits(s: &str) -> bool {
for c in s.chars() {
if !c.is_numeric() {
return false;
}
}
true
}
fn main() {
let str = "12345";
let all_digits = contains_only_numeric_digits(str);
println!("String contains only numeric digits: {}", all_digits);
}
String contains only numeric digits: true
In this tutorial, we learned How to Check if String contains only Numeric Digits in Rust language with well detailed examples.