In Rust, a unit struct is a type of struct that doesn't have any fields. It is essentially a "marker" or "tag" type and is represented by an empty pair of curly braces (struct Name;
).
Unit structs are particularly useful in scenarios where you need to define types without associated data. Here's an example:
In this challenge, you'll implement a unit struct named Logger
. The struct will serve as a type marker that enables certain logging functionality via a method log_message
. You'll also implement a log_message
function that prints a message to the console using the unit struct Logger
as a marker. The goal is to familiarize yourself with unit structs and their usage as markers.
Define a unit struct named Logger
.
Implement a Logger::log_message
method that:
&str
message as input.[LOG]: {message}
.Use the unit struct Logger
to call this method.
pub struct Logger;
.impl Logger
block.println!
macro to format and print the message.