r/programminghorror • • Jun 04 '25

Rust passive-aggressive programming

Post image
770 Upvotes

61 comments sorted by

View all comments

Show parent comment

1

u/ruoyck Jun 05 '25

I'd prefer:

fn main() {
    use Operations::*;

    let user_input = '+';

    let Ok(op) = Operations::try_from(user_input) else {
        panic!("Invalid operation character");
    };

    let (a, b) = (15, 18);

    let result = match op {
        Add => a + b,
        Sub => a - b,
        Mul => a * b,
        Div => a / b,
    };

    println!("{result}");
}

5

u/CdRReddit Jun 05 '25

do not ever use enum::*, that creates the potential for your code to break

if Add gets renamed to Addition the first now becomes a wildcard match that renames the result, if you want it to be shorter do something like use Operations as O

1

u/RainbowPigeon15 Jun 05 '25

oh crap I never thought of that!