繁体   English   中英

什么是特性`core :: types :: Sized`没有实现生锈中的类型`<generic#0>`?

[英]What is the trait `core::kinds::Sized` is not implemented for the type `<generic #0>` in rust?

我希望这可行:

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.

围栏: http//is.gd/kxDt0P

发生什么了?

我不知道这个错误意味着什么。

是我正在使用Result并且要求U,V不是大小的吗? 在哪种情况下,为什么它们大小? 我没写:

Task<Sized? R, Sized? E>

现在所有仿制药都是动态尺寸还是什么? (在这种情况下,什么是大小?甚至是什么意思?)

这是怎么回事?

你只需要删除<uint, uint>impl<uint, uint>因为类型参数去那里,而不是具体的类型:

impl Task<uint, uint> for Foo { ... }

我认为你得到的错误是编译器对未使用的类型参数感到困惑。 它也出现在这个超级缩减版本中:

trait Tr {}
impl<X> Tr for () {}

Rust没有提供«Generics specialization»:你将你的特性定义为Task<R, E> ,你必须使用泛型类型( RE )来实现它。

不可能只使用像<uint, uint>那样的特定类型来实现它。

例如,如果您写道:

trait Foo<R> {}

struct Bar;

struct Baz;

impl<Baz> Foo<Baz> for Bar {}

应该指望Baz在你实现集团是相同的struct Baz :它是被遮蔽的,就像一个var函数的参数将阴影var全局变量。

然而,当你用原始类型执行此操作时,却得到这个神秘的错误消息而不是简单的阴影, 可能是一个错误,要么你应该有阴影或更清晰的错误信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM