我写了以下代码:
use std::io::{IoResult, Writer};
use std::io::stdio;
fn main() {
let h = |&: w: &mut Writer| -> IoResult<()> {
writeln!(w, "foo")
};
let _ = h.handle(&mut stdio::stdout());
}
trait Handler<W> where W: Writer {
fn handle(&self, &mut W) -> IoResult<()>;
}
impl<W, F> Handler<W> for F
where W: Writer, F: Fn(&mut W) -> IoResult<()> {
fn handle(&self, w: &mut W) -> IoResult<()> { (*self)(w) }
}
Run Code Online (Sandbox Code Playgroud)
然后rustc在我的终端:
$ rustc writer_handler.rs
writer_handler.rs:8:15: 8:43 error: the trait `core::marker::Sized` is not …Run Code Online (Sandbox Code Playgroud) 我正在使用一个用于多态的枚举,类似于以下内容:
enum MyType {
Variant1 { a: i32, b: i32 },
Variant2 { a: bool, b: bool },
}
Run Code Online (Sandbox Code Playgroud)
是否有干净的方法将现有结构用于Variant1和Variant2?我已经完成以下工作:
struct Variant1 {
a: i32,
b: i32,
}
struct Variant2 {
a: bool,
b: bool,
}
enum MyType {
Variant1(Variant1),
Variant2(Variant2),
}
Run Code Online (Sandbox Code Playgroud)
但是感觉很笨重。我想知道是否有更好的方法来完成类似的事情。
rust ×2