In object-oriented programming, a singletonclass is a class that can only have one instance. This is typically done by making the constructor private and providing a static method that returns the singleton instance. Singleton classes are useful in situations where you need to ensure that there is only one instance of a class, such as for logging, caching, or database connections.
Here is an example of a singleton class in Rust:
Rust
struct Logger {
// The singleton instance of the Logger class.
static mut INSTANCE: Option<Logger> = None;
// The name of the logger.
name: String,
}
impl Logger {
// The private constructor.
fn new(name: String) -> Logger {
Logger { name }
}
// The static method that returns the singleton instance.
fn get_instance() -> &'static Logger {
// If the singleton instance has not been created yet, create it.
if Logger::INSTANCE.is_none() {
let logger = Logger::new("MyLogger");
Logger::INSTANCE = Some(logger);
}
// Return the singleton instance.
Logger::INSTANCE.as_ref().unwrap()
}
// The method that logs a message.
fn log(&self, message: String) {
println!("Log message from {}: {}", self.name, message);
}
}
fn main() {
// Get the singleton instance of the Logger class.
let logger = Logger::get_instance();
// Log a message.
logger.log("Hello, world!");
}
This code defines a singleton class called Logger. The class has a private constructor and a static method called
get_instance(). The get_instance() method returns the singleton instance of the class. The class also has a method called
log() that logs a message.
The main() function gets the singleton instance of the Logger class and logs a message. The message is logged to the console.
As you can see, the singleton class ensures that there is only one instance of the
Logger class. This is useful because it allows us to share the logger across the entire program.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In object-oriented programming, a singleton class is a class that can only have one instance. This is typically done by making the constructor private and providing a static method that returns the singleton instance. Singleton classes are useful in situations where you need to ensure that there is only one instance of a class, such as for logging, caching, or database connections.
Here is an example of a singleton class in Rust:
Rust
This code defines a singleton class called
Logger. The class has a private constructor and a static method calledget_instance(). Theget_instance()method returns the singleton instance of the class. The class also has a method calledlog()that logs a message.The
main()function gets the singleton instance of theLoggerclass and logs a message. The message is logged to the console.As you can see, the singleton class ensures that there is only one instance of the
Loggerclass. This is useful because it allows us to share the logger across the entire program.