Greg KH: Untrusted data in Linux – How Rust is going to save us [video]
youtube.com2 pointsby NobodyNada0 comments
if fooOK
if barOK {
if bazOK {
// do something
}
}
}
can be written as: guard fooOK else { return }
guard barOK else { return }
guard bazOK else { return }
// do something
Obviously there are other options (like writing a negated if), but sometimes guard is more readable. It's a style thing. guard let foo = someOptional else { return }
print(foo);
This is useful enough that Rust copied it in the form of 'let ... else {}' statements (but did not bring over boolean guard statements). enum Foo {
A(bool),
B
}
Variant A uses the valid bool values 0 and 1, whereas variant B uses some other bit pattern (maybe 2). enum Foo {
A(bool),
B(bool)
}
...because bool always has bit patterns 0 and 1, so it's not possible for an invalid value for A's fields to hold a valid value for B's fields.