News for hackers. Stuff that matters.
Re:Where Everything Went Wrong: Error Handling and Error Mes... by gardaani on Friday February 19, 2021 @06:03AM (#26191779)
I totally agree. I love Swift's guard let. It makes early returning [1] easy:

    guard let value = optvalue else {
        return // optvalue is none
    }
There has been several proposals [2][3] to fix it in Rust but they don't seem to go anywhere.

I'm using this in my own code now to unwrap or return (it looks stupid):

        let value = if let Some(value) = optvalue {
            value
        } else { // optvalue is none
            return;
        };

[1] https://szymonkrajewski.pl/why-should-you-return-early/

[2] https://github.com/rust-lang/rfcs/issues/2616

[3] https://github.com/rust-lang/rfcs/pull/1303

Re:Where Everything Went Wrong: Error Handling and Error Mes... by danappelxx on Friday February 19, 2021 @03:14PM (#26197508)
I missed `guard let` for a while too, but eventually stumbled upon this pattern which is almost as good:

    let value = match value {
        None => return,
        Some(value) => value
    };
Re:Where Everything Went Wrong: Error Handling and Error Mes... by papaf on Friday February 19, 2021 @06:17PM (#26199362)
I use match as an inside out 'if let' but in this particular case I prefer to:

  let value = value.ok_or(MyError)?;