• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by John Dev


30 Nov, 2024

Updated at 27 Dec, 2024

What if you have NONE for return

I have coded myself into a corner. Not the first time. The below code rarely returns None. But if it does, it of course panics. If I try to handle the None case for RE.captures(&text), I get an error that I should be returning type IpAddr. How can I handle the option type from RE.

fn address_helper(text: &str) -> IpAddr {

    lazy_static! {
        static ref RE: Regex = Regex::new(r"\d+\.\d+\.\d+\.\d+").unwrap();
    }
    let caps = RE.captures(&text).unwrap();
    return IpAddr::from_str(caps.get(0)
        .map_or("",|m| m.as_str())).unwrap()
}

2 posts - 2 participants

Read full topic