在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);
}
Run Code Online (Sandbox Code Playgroud)
编译导致以下两个错误:
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
Run Code Online (Sandbox Code Playgroud)
如果我单独声明结构,并将结构包装在元组内的元组结构(正确的术语?)中,那么我得到了预期的结果,但是在整个地方都有冗长和类似的类型名称我怀疑我被误解了成才:
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);
}
Run Code Online (Sandbox Code Playgroud)
运行输出:
Increasing by: 4.
Run Code Online (Sandbox Code Playgroud)
将结构包装在枚举类型(术语?)中,共享同一个名称真的是最好的解决方案吗? Command::Increment(Increment { quantity: 7 })
是的,这是您将在此实施方案中获得的最佳结果.枚举只是一种类型; 它的变体纯粹是变种,而不是类型.
另一种选择是使用特征和泛型:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当然,它不是直接并行的; 如果你想存储一些command可能不同的类型,你需要改变它们process,self: Box<Self>或者&self你需要使用或者,Box<Command>或者&Command它是另一种方法,可以满足你的要求.就定义而言,它更纯粹.
我可能误解了您的简单示例,但请记住,您可以直接在枚举上实现方法:
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();
}
Run Code Online (Sandbox Code Playgroud)
我碰巧喜欢这个版本,因为它消除了process_command(command).
| 归档时间: |
|
| 查看次数: |
4952 次 |
| 最近记录: |