Rust: todo! macro
One of the features of Rust that I’ve found really helpful when getting started on a project is the todo! macro.
The todo! macro tells the compiler that you know that some section of code isn’t complete yet and that it should compile the rest of it anyway.
An example of where I’ve used this is when I get started on a project I like protoyping out the signatures for the primary functions I think I’m going to need before I start implemententing them. For example, if I’m working on functions that do math operations on a custom type, I’ll start by thinking about what functions I need and what their signatures are going to be, type that out, and then fill them in. With todo!, that means I may type out something like:
struct Point {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Point {
pub fn add(&self, other: &Point) -> Point {
todo!()
}
pub fn subtract(&self, other: &Point) -> Point {
todo!()
}
}and that compiles, as long as there are not other mistakes.
The reason that’s useful is let’s say I made a mistake and type Pint instead of Point somewhere. I can build the code, have the compiler point that out, and fix it before needing to implement everything.
The official documentation about todo! can be found here.