Author: David Beazley (http://www.dabeaz.com)
June 27, 2025
If you're looking for a good "nuts and bolts" project for learning some Rust basics, Peter Shirley's excellent Ray Tracing in One Weekend project might fit the bill. Not only do you get to make some pretty pictures, you'll get to scratch your head as you ponder how to port various parts of the associated C++ code. Plus, there is a lot of math!
I have coded the Ray Tracer project from scratch about six times in the context of teaching. What you'll find here is my latest "best effort" human-written solution to the Ray Tracer project. Every line of this code has been written with purposeful intent and can be explained further (by me). No LLMs or other coding assistants were used. In addition, no line of code was written by blindly accepting a suggestion from the Rust compiler without conducting further research.
I thought about writing a blog post about all of this, but then I decided I'd flip the script around. Instead of doing that, I present all of this code without commentary and await YOUR questions. In fact, I'd love to answer questions about this code or Rust in general!
Thus, view this repository as a kind of "Ask Me Anything" project. If you want to ask a question, please direct your attention to GitHub discussions. I will make a best effort to respond.
If you've never attempted the Ray Tracer project, I highly recommend that you go to the above link and try to independently come up with your own Rust solution first. In doing that, I wouldn't even look at the code here--if you're serious about learning, I think it's important for you to fumble around, make mistakes, and scratch your head. When you're done, come back here, compare code, and ask questions.
In writing this code, my goal was to do more than simply port the existing C++ code to Rust. I wanted to write the code in an idiomatic manner that was faithful to both Rust and some principles generally associated with well written software such as making proper use of abstraction and encapsulation. There is also an ergonomic aspect to the whole affair--I wanted the final code to look reasonably nice without getting get too bogged down by low-level details. Although I didn't make any effort to optimize the code, it runs faster than the original C++ code when I time it.
The code does not cover every possible Rust concept, but it does contain practical examples of the following:
- Mathematical operations and conversions.
- Operator overloading.
- User-defined structs and traits.
- Programming in an object-oriented style.
- Dynamic dispatch via
dynattributes. - Use of
Option<T>andBox<T>. - Lifetimes (especially as applied to traits).
- Macros.
The code has been formatted using rustfmt and should compile
without warning messages. There is a third-party dependency on
the "fastrand" crate. Ray tracing is slow--everything will run
a lot faster if you run things using release mode.
I have also recorded a two-part video wherein I live-code the entire project from start-to-end with no cuts or edits. This might provide a bit of insight about some of my coding choices and can also serve as a guide if you want to try and code the project yourself. Of course, if you just want to watch me code Rust for seven hours, that's fine too!
Note: The video presentation code may differ slightly from what you see here--there's always a bit of randomness in live coding!
I don't accept pull requests, but if you think you've found a bug or have a suggestion for improvement, please submit a GitHub issue for discussion.
I teach courses related to a variety of advanced programming and computer science related topics. If this sounds interesting, please go here.
Cheers,
David Beazley (http://dabeaz.com)