Skip to content

Chapter 10.3 example incorrectly describes target type or is incoherent #1598

@NathanielAJohnson

Description

@NathanielAJohnson
fn main() {
    let string1 = String::from("abcd");
    let string2 = "xyz";

    let result = longest(string1.as_str(), string2);
    println!("The longest string is {}", result);
}

Listing 10-20: A main function that calls the longest function to find the longer of two string slices

Note that we want the function to take string slices, which are references, because we don’t want the longest function to take ownership of its parameters. We want to allow the function to accept slices of a String (the type stored in the variable string1) as well as string literals (which is what variable string2 contains).

The bolded quote should read:

We want to allow the function to accept slices of a String (the type stored in the variable string2)

because string1 is of type String and string2 is of type str& because String Literals Are Slices

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions