Skip to content

Feature request: Automatically convert String to &str when required. #17300

Closed
@sandeep-datta

Description

@sandeep-datta

Please consider automatically converting Strings to string slices when a string is passed into a string slice context. Please pardon my total lack of experience with rust but I think this shouldn't introduce any bugs in the program since a string slice is a more restricted type than a String. Any thoughts?

Example:

fn takes_slice(slice: &str) {
    println!("Got: {}", slice);
}

fn main() {
    let s = "Hello".to_string();
    takes_slice(s);
}

instead of ...

fn main() {
    let s = "Hello".to_string();
    takes_slice(s.as_slice());
}

May be we can provide a conversion operator like C++ does?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions