Unit Structs
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:
pub struct Database;Your Task
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.
Requirements
-
Define a unit struct named
Logger. -
Implement a
Logger::log_messagemethod that:- Takes a
&strmessage as input. - Prints the message to the console in the format:
[LOG]: {message}.
- Takes a
-
Use the unit struct
Loggerto call this method.
Example Test
Logger::log_message("This is a test log.");
// Output: [LOG]: This is a test log.Hints
Click here to reveal hints
- Define the unit struct as
pub struct Logger;. - Implement an associated function for the unit struct using the
impl Loggerblock. - Use the
println!macro to format and print the message. - Remember, unit structs do not require any fields and are defined with empty braces.
Run to execute your code and see test results here.