Closed
Description
https://play.rust-lang.org/?gist=089ca2d7214f6761051c&version=stable
let s = "abc";
let t = if true { s[..2] } else { s }; // confusing error message
let u: &str = if true { s[..2] } else { s }; // clear error message
let v = s[..2]; // strange error message
let w: &str = s[..2]; // clear error message
These are four attempts to take a slice of a &str
, in two pairs. They are all wrong because indexing a &str
returns a str
, so you need an extra &
. That's not the point. The point is without type annotations, the error diagnostics seem like they could use some improvement.
Especially the first one -- it sounds like it is saying my expression ought to be a str
, but it already is. I think it's actually saying the associated type Index::Output
ought to be a str
, but the way the diagnostic is written, it sounds backwards.
Can either of these diagnostics be improved?