Without using regexp, I want to do simple string validation. Specifically I want to validate that a string begins with one class of "character" (yes, I know) and is followed by zero or more "characters" (yes, I know) of a different class.
I've primarily been working in C for the past decades, so I'm trying to learn to use some of the more functional-like constructs:
fn validate_heading(heading: &str) -> Result<(), Error> {
let ch = heading.chars().next();
if let Some(ch) = ch {
if !is_heading_leading_char(ch) {
return Err(Error::BadFormat("Invalid leading character".to_string()));
}
} else {
return Err(Error::BadFormat("Empty or broken string".to_string()));
}
if let Some(_idx) = heading.chars().nth(1).position(|c| !is_heading_char(c)) {
return Err(Error::BadFormat("Invalid heading".to_string()));
}
Ok(())
}
nth()
returns an Optional
, so position()
fails.
What's an idiomatic way to do this?