I would use the ? operator and move the parsing into its own function. That way you can avoid all the match or unwrap()s.
fn get_number(text: &str) -> Option<u32> {
// create an iterator over the pieces in our input string
let mut words = text.split(":");
// skip past the first word, returning None if there wasn't one
let _user = words.next()?;
// then save the second word to a variable
let number = words.next()?;
// finally, parse the number and because we don't care about the
// error we can use ok() to convert the Result<u32, Error> to an
// Option<u32>
number.parse().ok()
}
fn get_number(s: &str) -> Option<u32>
{
s.split(':')
.skip(1) // skip "user"
.next()? // the ? returns None if necessary
.parse() // result is Result<u32,...>
.ok() // transforms a Result into an Option
}
I see that in all the response you used find and not split.
Actually my string comes from the reading of the serial port and it will be something similar to "A:U:V:W:X:Y:Z"
And I must get each of the values: A, U, V, etc with the correct data type according to the case.