expression
Validate a single portable child name
Validate a deliberately restricted single ASCII child name in a trusted directory. Reject path separators, colon and non-ASCII input under this application policy. This is not general filename validation or defense against hostile symlinks, reserved names or concurrent changes. Expected stdout (not executed): true false false Common mistake: Treating lexical validation as filesystem confinement.
Formal Syntax & Structure
rust syntaxrust
matches!(components.next(), Some(Component::Normal(_)))Working Example
Example Snippetrust
fn main() { use std::path::{Path, Component}; fn allowed(s: &str) -> bool { let mut c = Path::new(s).components(); !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') && matches!(c.next(), Some(Component::Normal(_))) && c.next().is_none() } println!("{} {} {}", allowed("report_2"), allowed("../report"), allowed("a/b"));}