Skip to content

Chapter 10.3: longest_with_an_announcement is anticlimactic #2878

@jruderman

Description

@jruderman
  • I have checked the latest main branch to see if this has already been fixed
  • I have searched existing issues and pull requests for duplicates

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#generic-type-parameters-trait-bounds-and-lifetimes-together

Description of the problem:

Let’s briefly look at the syntax of specifying generic type parameters, trait bounds, and lifetimes all in one function!

But it doesn't really combine these features. It just uses each of them separately, so it doesn't feel like there's anything new here (besides putting 'a and T together in the angle brackets).

Suggested fix:

Use PartialOrd as the trait bound to create a generic larger function:

fn larger<'a, T>(
    x: &'a T,
    y: &'a T,
) -> &'a T
where
    T: PartialOrd,
{
    if *x > *y {
        x
    } else {
        y
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions