我希望这可行:
trait Task<R, E> {
fn run(&self) -> Result<R, E>;
}
mod test {
use super::Task;
struct Foo;
impl<uint, uint> Task<uint, uint> for Foo {
fn run(&self) -> Result<uint, uint> {
return Err(0);
}
}
fn can_have_task_trait() {
Foo;
}
}
fn main() {
test::can_have_task_trait();
}
Run Code Online (Sandbox Code Playgroud)
......但它没有:
<anon>:10:3: 14:4 error: the trait `core::kinds::Sized` is not implemented for the type `<generic #0>`
<anon>:10 impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11 fn run(&self) -> Result<uint, uint> {
<anon>:12 return Err(0);
<anon>:13 }
<anon>:14 }
<anon>:10:3: 14:4 note: the trait `core::kinds::Sized` must be implemented because it is required by `Task`
<anon>:10 impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11 fn run(&self) -> Result<uint, uint> {
<anon>:12 return Err(0);
<anon>:13 }
<anon>:14 }
<anon>:10:3: 14:4 error: the trait `core::kinds::Sized` is not implemented for the type `<generic #1>`
<anon>:10 impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11 fn run(&self) -> Result<uint, uint> {
<anon>:12 return Err(0);
<anon>:13 }
<anon>:14 }
<anon>:10:3: 14:4 note: the trait `core::kinds::Sized` must be implemented because it is required by `Task`
<anon>:10 impl<uint, uint> Task<uint, uint> for Foo {
<anon>:11 fn run(&self) -> Result<uint, uint> {
<anon>:12 return Err(0);
<anon>:13 }
<anon>:14 }
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
Program ended.
Run Code Online (Sandbox Code Playgroud)
发生什么了?
我不知道这个错误意味着什么.
是我正在使用Result并且要求U,V不是大小的吗?在哪种情况下,为什么它们大小?我没写:
Task<Sized? R, Sized? E>
Run Code Online (Sandbox Code Playgroud)
现在所有仿制药都是动态尺寸还是什么?(在这种情况下,什么是大小?甚至是什么意思?)
这是怎么回事?
你只需要删除<uint, uint>
in impl<uint, uint>
,因为类型参数去那里而不是具体类型:
impl Task<uint, uint> for Foo { ... }
Run Code Online (Sandbox Code Playgroud)
我认为你得到的错误是编译器对未使用的类型参数感到困惑.它也出现在这个超级缩减版本中:
trait Tr {}
impl<X> Tr for () {}
Run Code Online (Sandbox Code Playgroud)