【发布时间】:2015-09-07 21:53:43
【问题描述】:
我有这个相当简单的 Rust 程序:
use std::ops::Deref;
trait Foo {
fn foo(&self);
}
impl Foo for () {
fn foo(&self) {
println!("hello world");
}
}
impl<F> Foo for Box<F> where F: Foo {
fn foo(&self) {
let f: &F = self.deref();
f.foo()
}
}
fn call_foo<F>(foo: &F) where F: Foo {
foo.foo()
}
fn main() {
let foo: Box<Foo> = Box::new(());
call_foo(&foo);
}
但我得到一个编译错误:
$ rustc main.rs
main.rs:26:3: 26:11 error: the trait `core::marker::Sized` is not implemented for the type `Foo` [E0277]
main.rs:26 call_foo(&foo);
^~~~~~~~
main.rs:26:3: 26:11 help: run `rustc --explain E0277` to see a detailed explanation
main.rs:26:3: 26:11 note: `Foo` does not have a constant size known at compile-time
main.rs:26 call_foo(&foo);
^~~~~~~~
main.rs:26:3: 26:11 note: required by `call_foo`
main.rs:26 call_foo(&foo);
^~~~~~~~
error: aborting due to previous error
E0277 的错误解释似乎无关。我该如何解决这个问题?
【问题讨论】:
-
供将来参考:这个问题解决了我试图将盒装闭包从盒装闭包向量作为参数传递给函数的问题。在这个 rust 操场上找到我的代码:play.rust-lang.org/…