Friday, September 13, 2024

Should you learn Rust NOW? My contribution to the Rust community - State Pattern in Rust...

Key Reasons Why Now Is the Right Time to Learn Rust:

1. Growing Popularity and Industry Adoption

According to the 2024 Stack Overflow Developer Survey, Rust is the most admired programming language, with 83% of developers saying they want to use it again. This is the eighth year in a row that Rust has topped this chart. Major Companies Use Rust: Big tech companies like Microsoft, Amazon, Google, Facebook, Dropbox, and Mozilla use Rust in production systems. Learning Rust can open doors to career opportunities at companies that have adopted the language.

2. Memory Safety Without Garbage Collection

Rust’s borrow checker ensures that you manage memory safely without relying on a garbage collector. This makes Rust an ideal language for systems programming, embedded systems and performance-critical applications. If you want to work on low-level programming without common pitfalls like segmentation faults or data races, Rust is an excellent choice.

3. Concurrency Without Fear

Rust’s ownership model allows you to write highly concurrent programs while ensuring thread safety at compile-time. This is a game-changer for developers working on multi-threaded or parallel applications, and it's a good reason to pick up Rust now as more modern software is built with concurrency in mind.

4.Growing Ecosystem and Libraries

Rust's ecosystem is maturing. The package manager and build tool, Cargo, is widely praised for its ease of use.

5. Community and Support

The Rust community is known for being friendly, helpful, and inclusive. The language is also supported by an active working group and regular updates, making it a language that's constantly evolving but staying stable.

Rust is the right choice for Systems programmers. It is a great alternative to C/C++ for writing system-level code (operating systems, device drivers, embedded systems).

So, here we go...

My contribution to the developers' community - State Design Pattern in Rust.

The State Design Pattern is a behavioral design pattern that allows an object to change its behavior when its internal state changes. In other words, the object behaves differently depending on its current state, and the state transitions are handled internally. This pattern helps avoid complex conditional statements (like if-else or match in Rust) and organizes the code to make it more maintainable.

Here is the source code of the implementation of State Pattern in Rust.

use std::thread;
use std::time::Duration;

struct Mamma {
state: Option<Box<dyn State>>,
}
trait State {
fn wash(self: Box<Self>) -> Box<dyn State>;
fn marinate(self: Box<Self>) -> Box<dyn State>;
fn cook( self : Box<Self>)-> Box<dyn State>;
fn serve(self : Box <Self>) -> Box<dyn State>;
}

impl Mamma {
fn new() -> Mamma {
let mamma = Mamma {
state: Some(Box::new(UncleanedState)),
};
mamma
}

fn startcooking(self) -> () {
self.state.unwrap().wash().marinate().cook().serve();
}
}

struct UncleanedState;
struct CleanedState;

struct MarinatedState;
struct CookState;


impl State for UncleanedState {
fn wash(self: Box<Self>) -> Box<dyn State>{
println!("The Chicken is in Uncleaned state. It's being washed...");
thread::sleep(Duration::from_secs(5)); // Pause for 2 seconds
Box::new(CleanedState{})
}
fn marinate(self: Box<Self>)->Box<dyn State>{
self
}

fn cook(self: Box<Self>)->Box<dyn State>{
self
}

fn serve(self : Box<Self>) ->Box<dyn State>{
self
}

}

impl State for CleanedState {
fn wash(self: Box<Self>) -> Box<dyn State> {
self
}
fn marinate(self: Box<Self>) -> Box<dyn State> {
println!("The chicken is in the Cleanedstate. It's being marinated...");
thread::sleep(Duration::from_secs(5));
Box::new(MarinatedState {})
}

fn cook(self: Box<Self>) -> Box<dyn State> {
self
}

fn serve(self: Box<Self>) -> Box<dyn State> {
self
}
}


impl State for MarinatedState {
fn wash(self: Box<Self>) -> Box<dyn State> {
self
}
fn marinate(self: Box<Self>) -> Box<dyn State> {
self
}

fn cook(self: Box<Self>) -> Box<dyn State> {
println!("The chicken is in the Marinatedstate state. It will be cooked now...");
thread::sleep(Duration::from_secs(5));
Box::new(CookState {})
}

fn serve(self: Box<Self>) -> Box<dyn State> {
self
}
}

impl State for CookState {
fn wash(self: Box<Self>) -> Box<dyn State> {
self
}
fn marinate(self: Box<Self>) -> Box<dyn State> {
self
}

fn cook(self: Box<Self>) -> Box<dyn State> {
self

}

fn serve(self: Box<Self>) -> Box<dyn State> {
println!("The chicken is in the Cookedstate");
println!("This is the last state. I m sure the guests will enjoy the chicken...");
self
}
}

fn main() {
let mamma = Mamma::new();
mamma.startcooking();
}

And here's the video for the output of the above source code.


No comments: