Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 10.1 wrong version of listing-10-05 #2504

Closed
eliozh opened this issue Nov 14, 2020 · 2 comments
Closed

Chapter 10.1 wrong version of listing-10-05 #2504

eliozh opened this issue Nov 14, 2020 · 2 comments

Comments

@eliozh
Copy link

eliozh commented Nov 14, 2020

Listing 10-5 in Chapter 10.1:

fn largest<T>(list: &[T]) -> &T {
    let mut largest = list[0];

    for item in list {
        if item > largest {
            largest = item;
        }
    }

    largest
}

fn main() {
    let number_list = vec![34, 50, 25, 100, 65];

    let result = largest(&number_list);
    println!("The largest number is {}", result);

    let char_list = vec!['y', 'm', 'a', 'q'];

    let result = largest(&char_list);
    println!("The largest char is {}", result);
}

Actual code in listing-10-05/src/main.rs

fn largest<T>(list: &[T]) -> &T {
    let mut largest = &list[0];

    for item in list {
        if item > largest {
            largest = item;
        }
    }

    largest
}

fn main() {
    let number_list = vec![34, 50, 25, 100, 65];

    let result = largest(&number_list);
    println!("The largest number is {}", result);

    let char_list = vec!['y', 'm', 'a', 'q'];

    let result = largest(&char_list);
    println!("The largest char is {}", result);
}

let mut largest = list[0]; was changed to let mut largest = &list[0]; by a4780b9 which was forgotten by 43565d3. But the rustbook is not changed.
This would fix #2453 . And there is no need to revert changes in #2460 .

@eliozh
Copy link
Author

eliozh commented Nov 14, 2020

On I found the reason is that stable version is older than beta version and nightly version.

@steveklabnik
Copy link
Member

Ah, then this will be fixed in time. Sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants