简体   繁体   中英

Generic placeholders or default values in Rust

I'm trying to write a generic command line parser. I'm having trouble with "default" values for generic types. cmd.invoke() returns a Result<K, E> , so there's no problem there, but how do I represent a placeholder or default value for E when cmd_to_invoke is a None ? In C#, I could use default(E) . Is there a construct like this in Rust?

pub struct Cmd<K, E> {
    cmds: Vec<Cmd<K, E>>,
}

impl<K, E> Cmd<K, E> {
    pub fn invoke(&mut self, cmd_name: &str) -> Result<K, E> {
        let cmd_to_invoke = self.cmds.iter_mut().find(|cmd| cmd.name == cmd_name);
        if let Some(cmd) = cmd_to_invoke {
            cmd.invoke()
        } else {
            // Some default / placeholder value for E
            Err(/* ? */)
        }
    }
}

You probably are looking for Default .

For example:

pub struct Cmd<K, E> {}

impl<K, E: Default> Cmd<K, E> {
    pub fn invoke(&mut self, cmd_name: &str) -> Result<K, E> {
        let cmd_to_invoke = self.cmds.iter_mut().find(|cmd| cmd.name == cmd_name);
        if let Some(cmd) = cmd_to_invoke {
            cmd.invoke()
        } else {
            // Some default / placeholder value for E
            Err(Default::default())
        }
    }
}

It's worth noting though that most error types in std and popular crates do not implement Default .

The idiomatic way of dealing with multiple error types in Rust is to define your own error enum for your application, library or component, which implements From for each possible underlying error type. This generally makes code simpler and easier to read, and works very nicely with the ? operator. Here is an example .

If you are writing a library which really must work with any error type, then this won't work. In that case, you are probably left with constraining E to the std::error::Error trait and then making a custom error type which can be converted from that, possibly keeping reference to the underlying error as a trait object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM