问题描述
我希望这能奏效:
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();
}
...但它没有:
<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.
那么,怎么回事?
我不知道这个错误是什么意思.
I have no idea what this error means.
是不是我在使用 Result 并且要求 U、V 没有大小?在这种情况下,为什么要调整大小?我没有写:
Is it that I'm using Result and that requires that U, V are not Sized? In which case, why are they Sized? I didn't write:
Task<Sized? R, Sized? E>
现在所有的泛型都是动态调整大小的吗?(在这种情况下,Sized 是什么意思?甚至是什么意思?)
Are all generics now dynamically sized or something? (in which case, what does Sized? even mean?)
怎么回事?
推荐答案
你只需要去掉impl
中的,因为类型参数在那里而不是具体类型:
You just need to remove the <uint, uint>
in impl<uint, uint>
, because type parameters go there and not concrete types:
impl Task<uint, uint> for Foo { ... }
我认为您遇到的错误是编译器对未使用的类型参数感到困惑.这个超精简版也会出现这种情况:
I think the errors you're getting are the compiler getting confused over unused type parameters. It also occurs with this super-reduced version:
trait Tr {}
impl<X> Tr for () {}
这篇关于rust 中没有为类型 `<generic #0>` 实现的特性 `core::kinds::Sized` 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!