1

Formally implement let chains by c410-f3r · Pull Request #88642 · rust-lang/rust...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/88642
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

c410-f3r commented on Sep 4, 2021

edited

Let chains

Thanks to @Centril for creating the RFC and special thanks to @matthewjasper for helping me since the beginning of this journey. In fact, @matthewjasper did much of the complicated MIR stuff so it's true to say that this feature wouldn't be possible without him. Thanks again @matthewjasper!

With the changes proposed in this PR, it will be possible to chain let expressions along side local variable declarations or ordinary conditional expressions. In other words, do much of what the if_chain crate already does.

fn u2_beautiful_day(lyrics: &str) {
    let mut lines = lyrics.trim().lines();

    if let Some(see_the_world) = lines.next().map(|el| el.trim())
        && let Some(see_china) = lines.next().map(|el| el.trim())
        && let Some(see_the_canyons) = lines.next().map(|el| el.trim())
        && let Some(see_the_tuna_fleets) = lines.next().map(|el| el.trim())
        && let Some(see_the_bedouin_fires) = lines.next().map(|el| el.trim())
        && let Some(see_the_oil_fields) = lines.next().map(|el| el.trim())
        && let Some(and_see_the_bird) = lines.next().map(|el| el.trim())
        && let Some(after_the_flood) = lines.next().map(|el| el.trim())

        && see_the_world == "See the world in green and blue"
        && see_china == "See China right in front of you"
        && see_the_canyons == "See the canyons broken by cloud"
        && see_the_tuna_fleets == "See the tuna fleets clearing the sea out"
        && see_the_bedouin_fires == "See the bedouin fires at night"
        && see_the_oil_fields == "See the oil fields at first light"
        && and_see_the_bird == "And see the bird with a leaf in her mouth"
        && after_the_flood == "After the flood all the colors came out"
    {
        println!("It was a beautiful day, don't let it get away");
    }
}

Other considerations

  • if let guard and let ... else features need special care and should be handled in a following PR.

  • Irrefutable patterns are allowed within a let chain context

  • Three Clippy lints were already converted to start dogfooding and help detect possible corner cases

cc #53667


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK