Patterns for Defensive Programming in Rust
corrode.dev
Patterns for Defensive Programming in Rust
1–10 of 99 posts
Re: Patterns for Defensive Programming in Rust
#2So many rust articles are focused on people doing dark sorcery with "unsafe", and this is just normal every day api design, which is far more practical for most people.
Re: Patterns for Defensive Programming in Rust
#3Actually the From trait documentation is now extremely clear about when to implement it (https://doc.rust-lang.org/std/convert/trait.From.html#when-t...)
Re: Patterns for Defensive Programming in Rust
#4The very useful TryFrom trait landed only in 1.34, so hopefully the code using unwrap_or_else() in From impl predates that... Actually the From trait documentation is now extremely clear about when to implement it ( https://doc.rust-lang.org/std/convert/trait.From.html#when-t... )
Re: Patterns for Defensive Programming in Rust
#5The very useful TryFrom trait landed only in 1.34, so hopefully the code using unwrap_or_else() in From impl predates that... Actually the From trait documentation is now extremely clear about when to implement it ( https://doc.rust-lang.org/std/convert/trait.From.html#when-t... )
As someone unfamiliar with Rust (yet! it's on my ever growing list of things I'd like to absorb into my brain), unwrap_or_else() sounds like part of the "What You See Is What I Threatened the Computer To Do" paradigm.
Re: Patterns for Defensive Programming in Rust
#6One question about avoiding boolean parameters, I’ve just been using structs wrapping bools. But you can’t treat them like bools… you have to index into them like wrapper.0.
Is there a way to treat the enum style replacement for bools like normal bools, or is just done with matches! Or match statements?
It’s probably not too important but if we could treat them like normal bools it’d feel nicer.
Re: Patterns for Defensive Programming in Rust
#7Earlier quoted context omitted.
As someone unfamiliar with Rust (yet! it's on my ever growing list of things I'd like to absorb into my brain), unwrap_or_else() sounds like part of the "What You See Is What I Threatened the Computer To Do" paradigm.
> INTERCAL has many other features designed to make it even more aesthetically unpleasing to the programmer: it uses statements such as "READ OUT", "IGNORE", "FORGET", and modifiers such as "PLEASE". This last keyword provides two reasons for the program's rejection by the compiler: if "PLEASE" does not appear often enough, the program is considered insufficiently polite, and the error message says this; if it appear…
Re: Patterns for Defensive Programming in Rust
#8Re: Patterns for Defensive Programming in Rust
#9Wow that’s amazing. The partial equality implementation is really surprising. One question about avoiding boolean parameters, I’ve just been using structs wrapping bools. But you can’t treat them like bools… you have to index into them like wrapper.0. Is there a way to treat the enum style replacement for bools like normal bools, or is just done with matches! Or match statements? It’s probably not too important but i…
enum MyType{
...
}
impl MyType{
pub fn is_useable_in_this_way(&self) -> bool{
// possibly ...
match self {...}
}
}
and later: pub fn use_in_that_way(e: MyType) {
if e.is_useable_in_this_way() {...}
}
Or if you hate all that there's always: if let MyType::Member(x) = e {
...
}Re: Patterns for Defensive Programming in Rust
#10The very useful TryFrom trait landed only in 1.34, so hopefully the code using unwrap_or_else() in From impl predates that... Actually the From trait documentation is now extremely clear about when to implement it ( https://doc.rust-lang.org/std/convert/trait.From.html#when-t... )
As someone unfamiliar with Rust (yet! it's on my ever growing list of things I'd like to absorb into my brain), unwrap_or_else() sounds like part of the "What You See Is What I Threatened the Computer To Do" paradigm.