Stream: zulip

Topic: Code tips and review


view this post on Zulip gsurrel (Mar 10 2020 at 10:24):

Hi Rustaceans! After lurking years, I finally got my hands on three small projects, in Rust:

  1. Generate objects according to a probability map for a 20 y.o. game
  2. Draw an SVG image to replicate a measurement sheet for barometric readings
  3. The biggest one, where I listen dmesg for a newly connected Android device, wait until it's detected by adb, and updates the status when disconnected. It's not finished, but the structure is here.

I was wondering if you would have advice on making the code more idiomatic, even though both are very small. No need for a full review, though :sweat_smile:

view this post on Zulip bjorn3 (Mar 10 2020 at 10:31):

For 2: you could use &["..."] as &[&str] at https://gist.github.com/gsurrel/d3b3903260c619814969b9799642a129#file-barometer-rs-L27 to avoid an allocation

view this post on Zulip bjorn3 (Mar 10 2020 at 10:32):

For 3: enum variants are normally named CamelCase, not UPPER_SNAKE_CASE.

view this post on Zulip bjorn3 (Mar 10 2020 at 10:34):

For 3: you can read a file using std::fs::read_to_string

view this post on Zulip bjorn3 (Mar 10 2020 at 10:39):

Other than that it looks pretty good! You may want to use rustfmt (cargo fmt) and clippy (cargo clippy) though. Rustfmt is a code formatter, while clippy is a code linter.

view this post on Zulip gsurrel (Mar 10 2020 at 10:59):

bjorn3 said:

For 3: enum variants are normally named CamelCase, not UPPER_SNAKE_CASE.

Oh, I thought the compiler would yell at me for that in the same way it yells for CamelCase crate or variables names :)
Thanks!

bjorn3 said:

For 3: you can read a file using std::fs::read_to_string

I'll investigate that right now. I built it using the Rust Cookbook, maybe that's why I built it that way. However, it doesn't apply to my reading of the uptime, where I followed this other reciepe.

bjorn3 said:

Other than that it looks pretty good! You may want to use rustfmt (cargo fmt) and clippy (cargo clippy) though. Rustfmt is a code formatter, while clippy is a code linter.

I know their existence, I've to check how to install them and integrate them into IntelliJ IDEA (I chose this IDE as I'm very used to Android dev., and I like not to learn again shortcuts!)

view this post on Zulip gsurrel (Mar 10 2020 at 10:59):

Thanks for the tips!


Last updated: Oct 11 2021 at 22:34 UTC