Dealing with trying to grab the value of a regex capture (noob help)

(Full Code below)

let mc = x.as_str();
let cap: _ =  re.captures(mc);
                    
let text1 = cap.get(0).map_or("", |m| m.as_str());

So I've tried this and I get:

 let text1 = match cap.get(0).map_or("", |m| m.as_str()) {
    |                                       ^^^ method not found in `Option<regex::Captures<'_>>`
    |
note: the method `get` exists on the type `regex::Captures<'_>`
   1.7.0\src\re_unicode.rs:948:5
    |
948 |     pub fn get(&self, i: usize) -> Option<Match<'t>> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider using `Option::expect` to unwrap the `regex::Captures<'_>` value, panicking if the value is an `Option::None`
    |
30  |                 let text1 = match cap.expect("REASON").get(0).map_or("", |m| m.as_str()) {
    |                                      +++++++++++++++++

(I don't want to use expect because the program will invariably break because of the None values being passed into the capture)
I've then tried to unwrap the capture (The program just panicked when it hit a None), I then tried to match case the None value to change it to a Some value.. Like this:

let mc = x.as_str();
let cap: _ =  match re.captures(mc).unwrap() {
       Some(val) => val,
       None => "blahblah",
        };
                    
let text1 = match cap.get(0).map_or("", |m| m.as_str());

and then I get

error[E0308]: mismatched types
  --> src\main.rs:27:21
   |
26 |                 let cap: _ =  match re.captures(mc).unwrap() {
   |                                     ------------------------ this expression has type `regex::Captures<'_>`
27 |                     Some(val) => val,
   |                     ^^^^^^^^^ expected struct `regex::Captures`, found enum `Option`
   |
   = note: expected struct `regex::Captures<'_>`
                found enum `Option<_>`

error[E0308]: mismatched types
  --> src\main.rs:28:21
   |
26 |                 let cap: _ =  match re.captures(mc).unwrap() {
   |                                     ------------------------ this expression has type `regex::Captures<'_>`
27 |                     Some(val) => val,
28 |                     None => "blahblah",
   |                     ^^^^ expected struct `regex::Captures`, found enum `Option`
   |
   = note: expected struct `regex::Captures<'_>`
                found enum `Option<_>`

Really all I want to do is grab the value of the capture as a str if it returns a Some, and just error handle the None values the capture receives.. But I keep going back and forth and not knowing what to do, any help? Here's the full code:

use ansi_hex_color;
use irc::client::prelude::*;
use futures::prelude::*;
use irc::error::Error;
use regex::Regex;


#[tokio::main]
async fn main() -> Result<(), Error> {

    let re = Regex::new("[#][[:xdigit:]]+").unwrap();

    let mut client = Client::new(r"path").await?;
    client.identify()?;
    let mut stream = client.stream()?;
    let sender = client.sender();
    client.send("CAP REQ :twitch.tv/commands twitch.tv/tags")?;

    
    while let Some(message) = stream.next().await.transpose()? {
    
        match message.command {
            Command::PRIVMSG(ref target, ref _msg) => {
                let x = message.to_string();
                let mc = x.as_str();
                let cap: _ =  match re.captures(mc).unwrap() {
                    Some(val) => val,
                    None => "blahblah",
                };
                    
                   
                
                let text1 = cap.get(0).map_or("", |m| m.as_str());
                let color = ansi_hex_color::colored(text1, "", _msg);
                
                println!("{}: {}", message.source_nickname().unwrap_or("default"), color );
                
                
                if message.source_nickname() == Some("name") && _msg == "^ver" {
                        sender.send_privmsg(target, "@name ver 0.0.1 [unstable]")?;   
                }

                if message.source_nickname() == Some("name") && _msg == "^g bee" {
                    sender.send_privmsg(target, "@name Bees are winged insects closely related to wasps and ants, known for their roles in pollination and, in the case of the best-known bee species, the western ...")?;   
            }
            }
            _ => (),
        }
    }

    Ok(())
}

Nevermind, I figured out the solution:

let rr = match re.captures(mc) {
                    Some(i) => i.get(0).unwrap().as_str(),
                    None => "",
                };

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.