Determine if a string is numeric: Difference between revisions

Content deleted Content added
Rob-codes (talk | contribs)
m corrected spelling
Menjaraz (talk | contribs)
Line 4,540:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// This function is not limited to just numeric types but rather anything that implements the FromStr trait.
// This function is not limited to just numeric types but rather anything that implements the FromStr trait.
fn parsable<T: FromStr>(s: &str) -> bool {
s.parse::<T>().is_ok()
}
}</syntaxhighlight>
 
Sample code:
 
<syntaxhighlight lang="rust">
use std::str::FromStr;
 
fn parsable<T: FromStr>(s: &str) -> bool {
s.parse::<T>().is_ok()
}
 
fn main() {
let test_cases = [
"142857",
"3.14",
"not of this earth!"
];
 
let types: &[(&str, fn(&str) -> bool)] = &[
("i32", parsable::<i32> as fn(&str) -> bool),
("i64", parsable::<i32> as fn(&str) -> bool),
("i128", parsable::<i32> as fn(&str) -> bool),
("f64", parsable::<f64> as fn(&str) -> bool),
];
 
println!("");
for &case in &test_cases {
for &(type_name, parse_fn) in types {
println!(
"'{}' {} be parsed as {}",
case,
if parse_fn(case) { "can" } else { "_cannot_" },
type_name
);
}
println!("");
}
}
</syntaxhighlight>
 
=={{header|Scala}}==