Result<...> and?

I have

pub fn with_feature(mut self, key: String, val: String) -> Result<License, LicenseError> {
        self.user_data.features.insert(key, val);

and License, LicenseError are both defined structs.
What do I need to do to have ? work? I have a compilation error with this

            .with_feature("debug".to_string(), "parts1".to_string())?;

The error being
the ? operator can only be used in a function that returns Result

but doesn't with_feature return Result?
Of course doing

            .with_feature("debug".to_string(), "parts1".to_string())

works, but the snippets above are part of a larger builder sequence and I'd prefer to not insert .unwrap() between each with_XXX call.

Can you create a playground example that reproduces the error? That would help greatly in aiding those that could help.

@banditpig, the error isn't talking about with_feature. It's talking about the function you're calling with_feature from. Remember that the ? operator is (mostly) equivalent to

match License::new().with_feature(...) {
    Ok(license) => license,
    Err(err) => return Err(err),

Any function using ? internally must itself return a Result so that the Err can bubble up. Otherwise, what would you expect to happen to the error?

Keep in mind that main can return a Result as well.


Yes! Obvious once it is pointed out!

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.