Closed
Description
I meet a compile error can be demonstrated by the following code, and think it's irrational:
trait Train {
fn knots(&self) -> usize;
}
impl Train for str {
fn knots(&self) -> usize {
self.chars().count()
}
}
fn train_length<T:Train>(text: &'static T) -> usize {
text.knots()
}
fn str_length(text: &'static str) -> usize {
text.knots()
}
fn main() {
println!("{:?}", str_length("asdfas")); // works
println!("{:?}", train_length("asdfas")); // compile error
}
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Trait systemCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: An error or lint that needs small tweaks.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
kevinmehall commentedon Jan 9, 2017
Use
fn train_length<T:Train + ?Sized>(text: &'static T) -> usize
.Bare
str
is an unsized type and generics assume that types are sized by default. Here's another post explaining unsized types.J-F-Liu commentedon Jan 9, 2017
Yes, add
+?Sized
can solve the problem, the compile message could be more helpful.[-]irrational compile error[/-][+]Suggest ?Sized on generics where applicable[/+]estebank commentedon Oct 11, 2019
Current output:
It should be
estebank commentedon Feb 5, 2020
Current output:
Fixed in #68377.