且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个方法的多种返回类型

更新时间:2023-02-14 17:18:56

您可以围绕 trait 设计一个解决方案,但这可能比简单地稍微调整您当前的解决方案需要更多的工作:>

You could design a solution around trait, but it would probably be much more work than simply adapting your current solution slightly:

struct DateBased{
    series: String,
    date: Date
}

struct SeasonBased{
    series: String,
    season: i32,
    episode: i32
}

enum ParsedFile{
    Date(DateBased),
    Season(SeasonBased),
    // etc
}

fn _populate_datebased(file: DateBased) -> Result<PopulatedFile, TvdbError>;

fn _populate_seasonbased(file: SeasonBased) -> Result<PopulatedFile, TvdbError>;

fn populate(f: ParsedFile) -> Result<PopulatedFile, TvdbError> {
    return match f {
        ParsedFile::Date(d) => _populate_datebased(d),
        // ...
    }
}

你可以在 Rust 中结合 enumstruct,我个人认为给事物命名是值得的,因为它可以更容易地操作它们.

You can combine enum and struct in Rust, and I personally find it worth it to put name on things specifically because it allows manipulating them more easily.