0

I'm a newbie in Rust.

My code inside a function that returns Option<(SomeType, String)> looks like this:

let mut keywords: HashMap<String, SomeType> = HashMap::new();
keywords.insert("a".to_string(), SomeType::A);
keywords.insert("b".to_string(), SomeType::B);
keywords.insert("c".to_string(), SomeType::C);

match keywords.get("a") {
    Some(some_type) => {
        // do something
        return Some((some_type, "some string".to_string()));
    }
    _ => { return None }
}

When I tried to run this, the compiler throw this error:

expected enum `SomeType`, found `&SomeType`

So, I tried this:

return Some((*some_type, "some string".to_string()));

But now, the compiler throws this error:

move occurs because `*some_type` has type `SomeType`, which does not implement the `Copy` trait

How can I solve this??

bichanna
  • 954
  • 2
  • 21
  • Do you want to pull out the value from the map and return it? Or do you want it to stay in the map and return a copy? – kmdreko Feb 02 '22 at 03:24
  • I want to return a copy. – bichanna Feb 02 '22 at 03:26
  • please read tag info to create an mcve https://stackoverflow.com/tags/rust/info – Stargateur Feb 02 '22 at 03:37
  • 4
    You need to clone the key, since you don't own it (but merely have a reference, hence the error message). A lot of your recent questions are answered in the Rust book, which you should take the time to read and explore. – GManNickG Feb 02 '22 at 04:56

0 Answers0