首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用枚举对结构进行分组

使用枚举对结构进行分组
EN

Stack Overflow用户
提问于 2015-03-17 07:17:33
回答 3查看 12K关注 0票数 12

在Rust中,应该如何对相关结构进行分组,以便在引用方法体内的具体类型时,函数签名可以接受多个不同的类型?

以下示例是为简单起见而设计的:

代码语言:javascript
复制
enum Command {
    Increment {quantity: u32},
    Decrement {quantity: u32},
}

fn process_command(command: Command) {
    match command {
        Command::Increment => increase(command),
        Command::Decrement => decrease(command),
    };
}

fn increase(increment: Command::Increment) {
    println!("Increasing by: {}.", increment.quantity);
}

fn decrease(decrement: Command::Decrement) {
    println!("Decreasing by: {}.", decrement.quantity);
}

fn main() {
    let input = "Add";
    let quantity = 4;

    let command: Command = match input {
        "Add" => Command::Increment { quantity: quantity },
        "Subtract" => Command::Decrement { quantity: quantity },
        _ => unreachable!(),
    };

    process_command(command);
}

编译会导致以下两个错误:

代码语言:javascript
复制
src/main.rs:13:24: 13:42 error: found value name used as a type: DefVariant(DefId { krate: 0, node: 4 }, DefId { krate: 0, node: 5 }, true) [E0248]
src/main.rs:13 fn increase(increment: Command::Increment) {
                                      ^~~~~~~~~~~~~~~~~~
src/main.rs:17:24: 17:42 error: found value name used as a type: DefVariant(DefId { krate: 0, node: 4 }, DefId { krate: 0, node: 8 }, true) [E0248]
src/main.rs:17 fn decrease(decrement: Command::Decrement) {
                                      ^~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors

如果我单独声明结构,并将结构包装在元组结构中(正确的术语?)枚举中的每一个都会得到预期的结果,但由于冗长和到处都是类似的类型名称,我怀疑我误解了某些东西:

代码语言:javascript
复制
struct Increment {
    quantity: u32,
}

struct Decrement {
    quantity: u32,
}

enum Command {
    Increment(Increment),
    Decrement(Decrement),
}

fn process_command(command: Command) {
    match command {
        Command::Increment(increment) => increase(increment),
        Command::Decrement(decrement) => decrease(decrement),
    };
}

fn increase(increment: Increment) {
    println!("Increasing by: {}.", increment.quantity);
}

fn decrease(decrement: Decrement) {
    println!("Decreasing by: {}.", decrement.quantity);
}

fn main() {
    let input = "Add";
    let quantity = 4;

    let command: Command = match input {
        "Add" => Command::Increment(Increment { quantity: quantity }),
        "Subtract" => Command::Decrement(Decrement { quantity: quantity }),
        _ => unreachable!(),
    };

    process_command(command);
}

运行输出:

代码语言:javascript
复制
Increasing by: 4.

将结构包装在枚举类型中(术语?)共享同一个名字真的是最好的解决方案吗?Command::Increment(Increment { quantity: 7 })

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-17 07:31:40

是的,这是你在这条实现路线上能做到的最好的。枚举只是一种类型;它的变体纯粹是变体,而不是类型。

另一种选择是使用特征和泛型:

代码语言:javascript
复制
struct Increment {
    quantity: u32,
}

struct Decrement {
    quantity: u32,
}

trait Command {
    fn process(self);
}

impl Command for Increment {
    fn process(self) {
        println!("Increasing by {}", self.quantity);
    }
}

impl Command for Decrement {
    fn process(self) {
        println!("Decreasing by {}", self.quantity);
    }
}

当然,这不是直接的比较;如果您想存储可能不同类型的command,则需要将process更改为使用self: Box<Self>&self,并且需要使用Box<Command>&Command,但这是另一种可能满足您的需求的方式。就定义而言,它更纯粹。

票数 10
EN

Stack Overflow用户

发布于 2015-03-17 22:02:00

我可能误解了您的简单示例,但请记住,您可以直接在枚举上实现方法:

代码语言:javascript
复制
enum Command {
    Increment {quantity: u32},
    Decrement {quantity: u32},
}

impl Command {
    fn process(self) {
        match self {
            Command::Increment { quantity } => {
                println!("Increasing by: {}.", quantity)
            },
            Command::Decrement { quantity } => {
                println!("Decreasing by: {}.", quantity)
            },
        };
    }
}

fn main() {
    let input = "Add";
    let quantity = 4;

    let command: Command = match input {
        "Add" => Command::Increment { quantity: quantity },
        "Subtract" => Command::Decrement { quantity: quantity },
        _ => unreachable!(),
    };

    command.process();
}

我碰巧喜欢这个版本,因为它消除了process_command(command)的冗余。

票数 6
EN

Stack Overflow用户

发布于 2015-03-17 16:30:31

那这个呢,我不太明白你的意思。

代码语言:javascript
复制
enum Command {
    Increment (u32),
    Decrement (u32),
}

fn process_command(command: Command) {
    match command {
        Command::Increment(quantity) => increase(quantity),
        Command::Decrement(quantity) => decrease(quantity),
    };
}

fn increase(quantity: u32) {
    println!("Increasing by: {}.", quantity);
}

fn decrease(quantity: u32) {
    println!("Decreasing by: {}.", quantity);
}

fn main() {
    let input = "Add";
    let quantity = 4;

    let command: Command = match input {
        "Add" => Command::Increment (quantity),
        "Subtract" => Command::Decrement (quantity),
        _ => unreachable!(),
    };

    process_command(command);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29088633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档