1

While learning Rust and in an attempt to do something useful, I'm looking to craft a SAFE API call for DNS:Add service. The API is well defined and should by rights be a simple PUT request, the body of which is a number of strings and one variable that is limited to two options.

I've looked to define a struct and enum for the body of the put with its real limits, rather than just using strings; as I expect defining close the reality, is better practice.

I think I must be close but then have a mismatch, trying to limit input to an enum. The third argument should be only 'drive' or 'app' as RootPathVariant.

So, I'm looking to assign the root_path with a match of arguments=arg[2] but atm the left-side of the match is &'static str, which is wrong where it wants a String.

enum is defined:

pub enum RootPathVariant { App, Drive }

impl RootPathVariant {
    pub fn as_str(&self) -> &str {
        match self {
            &RootPathVariant::App => "app",
            &RootPathVariant::Drive => "drive",
        }
    }
}

How should I put the left side of this match, to only accept content of that enum?

let mut root_path: RootPathVariant = RootPathVariant::Drive;
match args[2].to_string() {
    "drive" => { root_path = RootPathVariant::Drive },
    "app" => { root_path = RootPathVariant::App },
    _ => { println!( "Error: root_path not [drive | app]");  return }
};

let put_body = AddServiceBody {
    long_Name: args[0],
    service_Name: args[1],
    root_Path: root_path,
    service_Home_Dir_Path: args[3]
};

Thanks for any help!

4
  • You can use string.as_ref() to get &str from a String and then use it in a match. But what is the type of args? to_string() may be unnecessary if you already have &str. Commented Oct 23, 2016 at 2:10
  • 1
    Please add the exact error message and a minimal reproducible example. Then you are more likely to get any answers :) Commented Oct 23, 2016 at 7:43
  • @Pavel Strakhov the type of args follows simple from Cargo:getopts as a Vec<String>. I missed the obvious option to match args[2].as_str() which appears to work! Commented Oct 23, 2016 at 9:45
  • I'm seconding @LukasKalbertodt's point about an MCVE. For example, this question has nothing to do with an enum. I'd bet that your code looks like this. Commented Oct 23, 2016 at 13:15

1 Answer 1

1

Noting the answer looks to be to match args[2].as_str()!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.