std-method
String::retain
Keep scalars for which the predicate is true. The remaining values preserve their order. Expected stdout (not executed): 12 Common mistake: Assuming is_ascii_digit recognizes Persian digits.
Formal Syntax & Structure
rust syntaxrust
fn retain<F>(&mut self, f: F) where F: FnMut(char) -> boolWorking Example
Example Snippetrust
fn main() { let mut code = String::from("A-1 B-2"); code.retain(|c| c.is_ascii_digit()); println!("{code}");}