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

丶灬走出姿态 提交于 2020-08-20 08:24:11

问题


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

When I write

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

The compiler reports an error:

error[E0599]: no method named `all` found for type `std::ops::Range<char>` in the current scope
 --> src/main.rs:2:16
  |
2 |     ('a'..'z').all(|_| true)
  |                ^^^
  |
  = note: the method `all` exists but the following trait bounds were not satisfied:
          `std::ops::Range<char> : std::iter::Iterator`

When I change it to

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

it compiles.

What's happening here? What does Rust mean when it says the method ... exists but the following trait bounds were not satisfied?


回答1:


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<char> 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<u8> 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.




回答2:


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<char> that does not implement it.

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

You are probably wondering why Range<char> 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.



来源:https://stackoverflow.com/questions/56296864/what-does-it-mean-when-method-exists-but-trait-bounds-not-satisfied

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!