Reading high quality code

I am new to learning Rust and am about halfway through The Rust Book. I try to follow good programming practices from my Java experience, but I currently don't have a good way of finding idiomatic Rust code in practice (that is, not just examples from the book).

Should I try to find open source projects to see how everything is put into practice, or is my Rust code which is currently compiling good enough for this stage? How do y'all know what you are typing is good practice?

Though I haven't personally read through it, I've often heard people say that the standard library itself has very idiomatic and readable code. Just make sure to skip through any extra complicated macros or compiler intrinsics, if they confuse you.

You can access the source code by going to std - Rust and clicking "Source" on the top of any page.

4 Likes

With Rust you get the hangover first and you learn to write idiomatic code last.

Essentially: there are lots of ways to write a better code in Rust. like in any other language, but because language literally forces one to think about various corner cases (at the penalty of refusing to compile your code) even “non-ideomatic” code usually works just fine.

There was even myth that “if it compiles it works”, at one point, but LLMs have shown us the fallacy of that: it's not easy for a human to write Rust code that's so convoluted that it compiles yet doesn't work… but LLMs can do that easily.

4 Likes

At least to me, I don't know. And when I read code I wrote 1 or 2 years ago, sometimes I think: Hmm, why? Why did I think that would be a good idea?

I would postulate it doesn't matter in the end what code you're reading. What brings insight is reading different code from different authors and different ages. Code written 10 years ago looks different from code written today.

The more you are confronted with different input for a topic, the more you learn. You find similarities, differences, ideas and concepts.

So don't theorize, just do it. Read foreign code, write your own code.

2 Likes

Check out egui. For example the fractal clock demo. All the demos have a link "Source code" on them somewhere. The individual demos are short and somewhat understandable.
The hard part with these example is there is a lot of closure (lamda?) use and sometimes they are hard to read. But if you are learning you probably should learn how to read closures too. (they still confuse my eyes)
I think egui would classify as idiomatic as it is a library used by many others. What is idiomatic?

I second that. Code I write is always just my best guess at the moment of writing.
Considering that, especially in very specialized domains, my understanding of the domain evolves over time, so does my assessment of my code.
Hence, I regularly refactor stuff after a certain amount of time, or, when the development of a project has evolved to a certain extent, because my past considerations when I wrote said code, don't fully apply any longer, or there are new, established best-practices.

There are certain indicators, however, that I consider indicators of code that sucks less ( :wink:), namely the absence of anti-patterns and the presence of established programming patterns. Imho this entails:

  • Extensive documentation of external interfaces, especially in libraries.
  • Few to no comments in the code - reading the code is self-explanatory.
  • No magic numbers / strings / values. Special stuff is placed in well-named constants.
  • Appropriate programming patterns for the domain have been applied. E.g.:
    • Open-Closed Principle
    • Don't repeat yourself
    • Single-responsibility principle
    • etc.
  • Short, concise methods and functions.
  • etc.

Not entirely. I have not written any code for three months. As an experiment I've been getting LLM's to produce some tools to support our team's work. They are 2d and 3d real-time animated data visualisations using slint and WGPU. They on Linux, Mac and Windows and in the browser using WASM. They work just fine.

I have hardly looked at the code, I'd need to spend a lot of time learning about the WGPU API, writing GPU shaders and generally doing 3d graphics before understanding if it is a mess or not.

I do worry that things would get out of hand if those apps were intended to grow over time but there are no such plans....yet.

Actually I feel more confident getting LLMs to write Rust knowing that the Rust compiler won't let them sneak any silly type or memory misuse errors in there.

I slept.
Another good "high quality code" is walkdir. It isn't too big. Very clean, and a good example of modules, documentation, lib vs bin, etc. And of course ripgrep by the same but it is kind of complicated with all those regex and stuff.

If you have no goal and just want to explore, type this in your terminal.

% rustup doc
Opening docs in your browser

You will be surprised.

If you are in a directory that has Cargo.toml in it, type this

cargo doc --open 

If you are really bored, try this:

cd /tmp
cargo new reading-high-quality-code
cd reading-high-quality-code
cargo add tokio
cargo doc --open

After you are done, clean up

cd ..
rm  -r reading-high-quality-code

Have fun and happy Friday.

(BTW, reading rustdoc blocks in the source code is also educational because it allows you to get into the mind of the author when they wrote it. Imagine what you would write and how to write them if it were you. And of cause you can write your own version in main.rs or lib.rs.)

1 Like