簡體   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