【问题标题】:How can I use associated constants in a where clause?如何在 where 子句中使用关联常量?
【发布时间】:2017-09-22 15:20:33
【问题描述】:

以下内容对我来说似乎是一个错误。

struct Seq([u8; 8]);

impl From<[u8; 8]> for Seq {
    fn from(data: [u8; 8]) -> Seq {
        Seq(data)
    }
}

trait Sequence {
    const LEN: usize;
}

impl Sequence for Seq {
    const LEN: usize = 8;
}

trait ByteSequence {
    fn check();
}

impl<S> ByteSequence for S
where
    S: Sequence + From<[u8; <S as Sequence>::LEN]>,
{
    fn check() {}
}

导致以下错误:playground

error[E0277]: the trait bound `S: Sequence` is not satisfied
  --> src/main.rs:23:29
   |
23 |     S: Sequence + From<[u8; <S as Sequence>::LEN]>,
   |                             ^^^^^^^^^^^^^^^^^^^^ the trait `Sequence` is not implemented for `S`
   |
   = help: consider adding a `where S: Sequence` bound
   = note: required by `Sequence::LEN`

我还尝试在额外的行中或直接在 impl 声明中添加边界。

【问题讨论】:

    标签: rust


    【解决方案1】:

    这不是一个错误,只是还没有实现。 Generic associated consts can't currently be used to parameterize fixed array lengths。您也许可以针对不太有用的错误消息提出另一个问题(至少有 one other poor error message)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多