Good error messages are crucial to Rust's usability. Therefore, I'd like to highlight an instance of an error message which I found most unhelpful, so that we may improve Rust.
Here is the function:
// Gets the ASCII character represented by a string of binary digits.
//
// `s` must contain exactly 8 binary digits, such as "01001111".
fn bin_to_char(s: &str) -> char {
s.char_indices()
.map(|(pos, c)| c.to_digit(2).unwrap() as u8 * (1 << (7 - pos)))
.sum()
.into()
}
and this is the output of the compiler:
rustc 1.14.0-nightly (5665bdf3e 2016-11-02)
error: the type of this value must be known in this context
--> <anon>:11:5
|
11 | s.char_indices()
| ^
error: aborting due to previous error
Here's the Playground link if you want to have a try at fixing this function.
The error message points at s
, as if the compiler can't figure out what type s
has. But s
is the function's parameter with type specified to be &str
, so this isn't logical. It seems the compiler needs to blame a piece of code, but can't locate exactly where the problem is, so it just picks something.
I'm interested in hearing about similar issues others have had. I'm also curious what causes the current behavior relating to both why this code doesn't compile, and why the error message is so vague.
Related Github issues:
Misplaced "type of this value must be known ..." error
The compiler could have been more precise about missing type annotation.