Skip to content

Ch04-03 Slices - String is coerced to &str #2298

@GeeF

Description

@GeeF

Regarding listing-04-09: &String is actually coerced to &str as of rust-lang/rust#21351.
Should this be mentioned at this point in the book?

fn first_word(s: &str) -> &str {
..

fn main() {
    let my_string = String::from("hello world");
    // first_word works on slices of `String`s
    let word = first_word(&my_string[..]);
    let word = first_word(&my_string); // this is also valid now
    ..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions