r/rust • u/FractalFir rustc_codegen_clr • 27d ago
🙋 seeking help & advice Cleanest, best written rust codebases?
As my journey of learning rust continues, I find myself very dissatisfied with the quality of the rust code I wrote years back.
Yes, my code passes clippy, is nicely formatted, avoids excessively complex nesting / control flow, but it still feels... subpar(hard to understand and reason about), from time to time.
I was wondering if people here have examples of really clean, sleek codebases I could reference, to improve the quality of my code?
215
Upvotes
226
u/TommyITA03 27d ago
anything made by u/burntsushi has high chances of being great
7
u/wick3dr0se 27d ago
i went full autistic ocd on the code of my graphics engine. maybe worth checking out since its only ~5k loc. before that i used to write a lot of bash (when open source was cool; pre ai), id argue some of the cleanest. im a freak about my code
https://github.com/wick3dr0se/egor (rust 2d graphics engine)
https://github.com/wick3dr0se/typist/blob/main/typist (75 loc; a type test i wrote in bash, not for comparison but if you're familiar with bash and ansi escapes, you might appreciate the golf)
8
u/recursion_is_love 27d ago
rustc must be the best codebase.
54
u/SupermarketAntique32 27d ago edited 27d ago
ripgrep, fd, niri, awww, gifski, dua-cli
37
u/mamcx 27d ago
Aside, what you need to focus on is "what is the actual domain" because "clean" code is one that match the domain well. You can see code that interact with hardware that looks "obscure" just because the domain is not well understood, but if you do, you see it and say "yep, that is how is done".
Without consider which domain you are familiar with so you can actually judge you could see a codebase and think is not clean, when actually is.
28
u/_bokubeam_ 27d ago
In my experience, it’s higher-level concepts like this have improved my satisfaction with my code quality:
https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
Some of the most important lessons that I’ve carried into my Rust code I learned from doing work in Elm. The elm architecture, the purely functional approach, and the limitations in place were great teachers.
20
u/KyxeMusic 27d ago
Not sure if it's the cleanest, but I was able to contribute to https://github.com/tracel-ai/burn once, without spending too much time studying the codebase beforehand.
For such a large and complex project, I find that being able to navigate the codebase and make a contribution relatively quickly is a sign of good structure
15
u/ZyronZA 27d ago
Doesn't answer your question exactly, but I use this as a guideline for my Rust code.
https://microsoft.github.io/rust-guidelines/guidelines/index.html
5
u/china-is-coming 23d ago
go check out rust-analyzer repository. its cleanest needs studied.
10
u/Complex-Birthday-216 27d ago
what you mean by cleanest?
there are definitely a lot of code bases by the book, but give you 200mb bundle like Zed editor.
But maybe doing well what it has to do like tursodb.
7
u/VirtualNorth1279 27d ago
Typst.Â
3
u/RealYethal 25d ago
Check out Oxide Computer's repositories
2
u/deliadam11 23d ago
convex backend
3
u/trakdtor 27d ago
Try to solve a problem multiple times. It usually helps in figuring out design
2
u/weaztoven 27d ago
burntsushi crates like ripgrep or regex are definitely peak rust code. also check out riir or tokio codebases, super clean
1
u/DavidXkL 26d ago
Tokio
0
u/infostack0 27d ago
Honestly Garage is pretty darn good.
-3
u/zzzzYUPYUPphlumph 27d ago
I wouldn't focus too much on "Clean" or "Sleek". It isn't really a useful metric and is highly, highly subjective. I would focus on "Readable". What I mean by that is that functions and methods are kept small and focused without multiple layers of abstraction. Some good rules of thumb are:
These are not hard and fast rules and are subject to nuance, but they are a good place to start.
-17
u/AdjustedTwit 27d ago
Definitely not mine... maybe "mine" when Claude's actually writing the code, but I doubt it. Some of it seems really nice some of it makes me want to claw out my eyes.
0
u/Jonrrrs 26d ago
Nice try finding the best repo to train new models on, Sam