Rust Design Patterns as a Book
rust-unofficial.github.io
Rust Design Patterns as a Book
1–10 of 49 posts
Re: Rust Design Patterns as a Book
#2https://doc.rust-lang.org/reference/attributes/type_system.h...
Re: Rust Design Patterns as a Book
#3In section 2.10, "Privacy for extensibility", are there any pros and cons to this approach over using the #[non_exhaustive] attribute? The latter works on both enums and structs, and doesn't require extra fields to be included. https://doc.rust-lang.org/reference/attributes/type_system.h...
Re: Rust Design Patterns as a Book
#4In section 2.10, "Privacy for extensibility", are there any pros and cons to this approach over using the #[non_exhaustive] attribute? The latter works on both enums and structs, and doesn't require extra fields to be included. https://doc.rust-lang.org/reference/attributes/type_system.h...
Private fields are by default module scoped, and can be tweaked. So you can limit the use even in the same crate.
Re: Rust Design Patterns as a Book
#5 #![warn(clippy::all)]
at the top of your crate’s entrypoint. This enables Rust’s default linter. It’s a lot more friendly and focused on good design than you might expect, often suggesting more elegant alternatives. Plus, many of its suggestions can be applied automatically in an environment like VS Code + rust-analyzer plugin.Re: Rust Design Patterns as a Book
#6A very low-effort way to learn good Rust patterns is to put #![warn(clippy::all)] at the top of your crate’s entrypoint. This enables Rust’s default linter. It’s a lot more friendly and focused on good design than you might expect, often suggesting more elegant alternatives. Plus, many of its suggestions can be applied automatically in an environment like VS Code + rust-analyzer plugin.
I added this to the top of https://github.com/etesync/etebase-rs/blob/master/src/lib.rs and then ran `cargo clippy`
#![warn(clippy::all)]
// Should fail https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp
pub fn bool_test(x: f32, y: f32) -> bool {
x == y
}
Any idea what's missing? Why is it not failing?Edit: I know the above example is bad code, that's the point. I want clippy to complain about it but it doesn't.
Re: Rust Design Patterns as a Book
#7A very low-effort way to learn good Rust patterns is to put #![warn(clippy::all)] at the top of your crate’s entrypoint. This enables Rust’s default linter. It’s a lot more friendly and focused on good design than you might expect, often suggesting more elegant alternatives. Plus, many of its suggestions can be applied automatically in an environment like VS Code + rust-analyzer plugin.
Thanks a lot for the suggestion, I can't believe I didn't know about this. However I just tried it and I can't get it to work. I added this to the top of https://github.com/etesync/etebase-rs/blob/master/src/lib.rs and then ran `cargo clippy` #![warn(clippy::all)] // Should fail https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp pub fn bool_test(x: f32, y: f32) -> bool { x == y } Any idea what's miss…
Re: Rust Design Patterns as a Book
#8A very low-effort way to learn good Rust patterns is to put #![warn(clippy::all)] at the top of your crate’s entrypoint. This enables Rust’s default linter. It’s a lot more friendly and focused on good design than you might expect, often suggesting more elegant alternatives. Plus, many of its suggestions can be applied automatically in an environment like VS Code + rust-analyzer plugin.
Thanks a lot for the suggestion, I can't believe I didn't know about this. However I just tried it and I can't get it to work. I added this to the top of https://github.com/etesync/etebase-rs/blob/master/src/lib.rs and then ran `cargo clippy` #![warn(clippy::all)] // Should fail https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp pub fn bool_test(x: f32, y: f32) -> bool { x == y } Any idea what's miss…
Re: Rust Design Patterns as a Book
#9Earlier quoted context omitted.
Thanks a lot for the suggestion, I can't believe I didn't know about this. However I just tried it and I can't get it to work. I added this to the top of https://github.com/etesync/etebase-rs/blob/master/src/lib.rs and then ran `cargo clippy` #![warn(clippy::all)] // Should fail https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp pub fn bool_test(x: f32, y: f32) -> bool { x == y } Any idea what's miss…
Comparing floats by equality is a dangerous pattern. It's easy for small precision errors to occur. You should instead check that they are close enough to each other, using an epsilon that you find appropriate, perhaps 1e-10. `(x - y).abs() < epsilon` should do the trick
Re: Rust Design Patterns as a Book
#10Earlier quoted context omitted.
Thanks a lot for the suggestion, I can't believe I didn't know about this. However I just tried it and I can't get it to work. I added this to the top of https://github.com/etesync/etebase-rs/blob/master/src/lib.rs and then ran `cargo clippy` #![warn(clippy::all)] // Should fail https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp pub fn bool_test(x: f32, y: f32) -> bool { x == y } Any idea what's miss…
Float comparison is an anti-pattern. Use an epsilon instead. https://stackoverflow.com/questions/4915462/how-should-i-do-...