Unicode Char Classes Beyond ASCII
Validate text with Unicode-aware predicates: is_alphabetic accepts é and 名, is_numeric accepts Arabic digits, and control characters are rejected.
Code
// Unicode 字符类别 char classes:ASCII 工具看不见的字母在这里都算字母
fn valid_display_name(name: &str) -> bool {
!name.is_empty()
&& name.chars().all(|c| c.is_alphanumeric() || c == ' ')
&& !name.chars().any(|c| c.is_control())
}
// 账号编码只允许 ASCII 数字
fn valid_account_code(code: &str) -> bool {
!code.is_empty() && code.chars().all(|c| c.is_ascii_digit())
}
fn main() {
for c in ['ñ', '名', '٣', 'Ω', ' ', '7', '\n'] {
println!(
"{c:?}: alphabetic={} numeric={} control={} whitespace={}",
c.is_alphabetic(),
c.is_numeric(),
c.is_control(),
c.is_whitespace(),
);
}
println!("name ok: {}", valid_display_name("josé garcía"));
println!("name declined: {}", valid_display_name("bad\u{7f}name"));
println!("code ok: {}", valid_account_code("0042"));
println!("code declined: {}", valid_account_code("12a4"));
}How this snippet works
The code’s whole job belongs to the functions valid_display_name, valid_account_code and main.
The cycling part of this snippet is for c in ['ñ', '名', '٣', 'Ω', ' ', '7', '\n'].
Points of care
- Standard library end to end — paste it in and it runs.
Using it
Signature: fn valid_display_name(name: &str) -> bool
The run emits 4 labeled sections — name ok:, name declined:, code ok:, code declined: — one per step.
Treat main as the orchestrator calling valid_display_name and valid_account_code in a fixed order.
Key statements
fn valid_display_name(name: &str) -> bool {
!name.is_empty()
&& name.chars().all(|c| c.is_alphanumeric() || c == ' ')
&& !name.chars().any(|c| c.is_control())
}
fn valid_account_code(code: &str) -> bool {
!code.is_empty() && code.chars().all(|c| c.is_ascii_digit())
}
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- What does Unicode Char Classes Beyond ASCII print when run?
- Run it as a complete program — the output lines carry labels such as `name ok:`, `name declined:`, `code ok:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.