Skip to content

Suggest ?Sized on generics where applicable #38936

Closed
@J-F-Liu

Description

@J-F-Liu

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

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions