在Rust中,应该如何对相关结构进行分组,以便在引用方法体内的具体类型时,函数签名可以接受多个不同的类型?
以下示例是为简单起见而设计的:
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);
}编译会导致以下两个错误:
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如果我单独声明结构,并将结构包装在元组结构中(正确的术语?)枚举中的每一个都会得到预期的结果,但由于冗长和到处都是类似的类型名称,我怀疑我误解了某些东西:
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);
}运行输出:
Increasing by: 4.将结构包装在枚举类型中(术语?)共享同一个名字真的是最好的解决方案吗?Command::Increment(Increment { quantity: 7 })
发布于 2015-03-17 07:31:40
是的,这是你在这条实现路线上能做到的最好的。枚举只是一种类型;它的变体纯粹是变体,而不是类型。
另一种选择是使用特征和泛型:
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,但这是另一种可能满足您的需求的方式。就定义而言,它更纯粹。
发布于 2015-03-17 22:02:00
我可能误解了您的简单示例,但请记住,您可以直接在枚举上实现方法:
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)的冗余。
发布于 2015-03-17 16:30:31
那这个呢,我不太明白你的意思。
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);
}https://stackoverflow.com/questions/29088633
复制相似问题