Search code examples
rustrefactoring

Refactoring variable assignment thru if let block with Enum


I'm working with the code below, which works, but is clearly not a very clever or efficient way to write a value to res.

let mut res = "";
if let Video(n) = res_info {    // res_info represents reference to &Settings type
    if n.pixel_width > 1920{
         res = "2160p";
    }
    else{
        res = "1080p";
    }
}

Printing res_info would yield the following:

Video(Video { pixel_width: 1920, pixel_height: 1080})

The following code seems to be close, however it's not assigning &str to res. I would much prefer a codeblock like this, in which res is only declared once.

let res = if let Video(n) = res_info {
    if n.pixel_width > 1920 {
        "2160p";
    }
    else{
        "1080p";
    }
};

Solution

  • As per the unit documentation

    The semicolon ; can be used to discard the result of an expression at the end of a block, making the expression (and thus the block) evaluate to ()

    Removing the semicolon should stop value from being discarded so the &str is resolved from the if blocks.

    let res = if let Video(n) = res_info {
        if n.pixel_width > 1920{
             "2160p"
        } else{
            "1080p"
        }
    }else{
        panic!("res_info is not a Video")
    };
    

    or with a match statement might be cleaner

    let res = match res_info {
        Video(n) if n.pixel_width > 1920 => "2160p",
        Video(n) => "1080p",
        _ => panic!("res_info is not a Video")
    };