0

I seem to be mixing types but I can't quite figure out how to fix this. Can someone help me?

let args_vector: Vec<String>  = env::args().collect();
for arg in &args_vector[1..]{
    match arg{
        "--bytes"  => {
                flag.c = true;
            },
        "--chars" => {
                flag.m =true;
            },
        _ => println! ("Error"),
    }
}

On the matches, I'm getting this error:

mismatched types: expected struct `std::string::String`, found str  
Boiethios
  • 38,438
  • 19
  • 134
  • 183
KDN
  • 485
  • 2
  • 7
  • 18

1 Answers1

3

Here arg is of type String in the match and "--bytes" is of type &str. So arg of type String has to be converted to &str. This can be done using String::as_ref().

let args_vector: Vec<String> = env::args().collect();
for arg in &args_vector[1..] {
    match arg.as_ref() {
        "--bytes" => {
            flag.c = true;
        }
        "--chars" => {
            flag.m = true;
        }
        _ => println!("Error")
    };
}

Note the missing ; after println! to make all match arms return the same type.

Malice
  • 1,457
  • 16
  • 32