Stream: zulip

Topic: Code tips and review


gsurrel (Mar 10 2020 at 10:24, on Zulip):

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:

bjorn3 (Mar 10 2020 at 10:31, on Zulip):

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

bjorn3 (Mar 10 2020 at 10:32, on Zulip):

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

bjorn3 (Mar 10 2020 at 10:34, on Zulip):

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

bjorn3 (Mar 10 2020 at 10:39, on Zulip):

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.

gsurrel (Mar 10 2020 at 10:59, on Zulip):

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.

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!)

gsurrel (Mar 10 2020 at 10:59, on Zulip):

Thanks for the tips!

Last update: Apr 03 2020 at 17:40UTC