What is a Factory Method in Rust?
In Rust, the Factory Method is a design pattern that allows you to create objects without specifying the exact class of object that will be created. It defines an interface (or a trait in Rust) for creating objects, but the specific implementation of the object creation is left to the subclasses. This pattern promotes loose […]
State Pattern in Rust
The “State” pattern is a behavioural design pattern that allows an object to alter its behaviour when its internal state changes. In Rust, like in other programming languages, you can implement the State pattern to encapsulate different states of an object and manage the transitions between those states. Ok what does that actually mean? Fast […]
What is the difference between a raw pointer and a function pointer in Rust?
A raw pointer and a function pointer are both types of pointers in programming, but they have different purposes and use cases: Raw Pointer: Function Pointer: In summary, the key differences are that raw pointers are used for general memory address manipulation and don’t carry type information, while function pointers specifically point to functions and […]
Closure as struct member
The following is an example with a closure and a struct that uses it: In this example, we have a Calculator struct that takes a generic type parameter T, which is expected to be a closure. The closure should take two f32 arguments and return an f32. The struct has a single field named operation […]
Modify structs – test with assert_eq! macro
This Rust code defines a simple struct called Coord and implements some methods for it. The Coord struct has two fields, x and y, representing coordinates in a two-dimensional space. Create the struct Make an implementation The Coord::new() function is a constructor method that creates a new Coord instance with initial values of x set […]