How to efficiently extract tailing number from string

I am trying to extract tailing number from string.
My current implement seems not good, as there are dual collect::<String>.

fn main() {
    let x = "11 DDD12";
    let y = x
        .chars()
        .rev()
        .take_while(char::is_ascii_digit)
        .collect::<String>();
    let y: i32 = y.chars().rev().collect::<String>().parse().unwrap();
    println!("{:?}", y);
}

Use rsplit_once() to split off the end of the string based on some criterion:

let string = "11 DDD12";
let digits = string
    .rsplit_once(|c: char| !c.is_ascii_digit())
    .map_or(string, |(_head, digits)| digits);
let number: i32 = digits.parse().unwrap();

Playground

3 Likes