Live data from Hacker News

Rust Design Patterns as a Book

rust-unofficial.github.io

1–10 of 49 posts

Re: Rust Design Patterns as a Book

#2
In 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

#3
post #2

In 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...

It looks like the book is outdated in this respect

Re: Rust Design Patterns as a Book

#4
post #2

In 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...

Using private fields you can more precisely control the “private scope”. #[non_exhaustive] is “crate scoped”, it does not apply limits for use in the same crate.

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
A 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.

Re: Rust Design Patterns as a Book

#6
post #5

A 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 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

#7
post #6
post #5

A 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…

Float comparison is an anti-pattern. Use an epsilon instead. https://stackoverflow.com/questions/4915462/how-should-i-do-...

Re: Rust Design Patterns as a Book

#8
post #6
post #5

A 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…

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

#9
post #6

Earlier 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

I know, it's an example I was hoping clippy would catch in order for it to fail so I know it works. Read what I wrote...

Re: Rust Design Patterns as a Book

#10
post #7
post #6

Earlier 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-...

I know, it's an example I was hoping clippy would catch in order for it to fail so I know it works. Read what I wrote...
Post reply on HN