What does it mean when “method exists but trait bounds not satisfied”?

后端 未结 2 2015
执念已碎
执念已碎 2021-01-23 07:28

I\'m new to Rust and observed something that I couldn\'t reason against.

When I write

fn main() {
    (\'a\'..\'z\').all(|_| true);
}

2条回答
  •  半阙折子戏
    2021-01-23 07:51

    What this means is that there is a trait in scope that has a function with that name, but that the object you are using does not implement such a trait.

    In your particular case, the trait that contains the all method is std::iter::Iterator, but your object ('a'..'z') if of type Range that does not implement it.

    Curiously enough, your second example compiles because (b'a'..b'z') is of type Range that does implement Iterator.

    You are probably wondering why Range does not implement iterator. That is because there are invalid char values between valid ones, so these ranges just cannot be iterated. In particular, the only valid chars are those in the ranges [0x0, 0xD7FF] and [0xE000, 0x10FFFF], IIRC.

提交回复
热议问题