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

后端 未结 2 1985
执念已碎
执念已碎 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:47

    The method all() is a method of the Iterator trait, so you can only call it on types that implement that trait. The type Range does not implement the Iterator trait, since a range of Unicode characters is not well-defined in the general case. The set of valid Unicode code points has gaps, and building a range of code points is not in general considered useful. The type Range on the other does implement Iterator, since iterating over a range of bytes has a well-defined meaning.

    More generally, the error message tells you that Rust has found a method with the correct name, but that method does not apply to the type you call it for.

提交回复
热议问题