Building structs instead of objects
In Python, we use a lot of objects. In fact, everything you work with in Python is an object. In Rust, the closest thing we can get to objects is structs. To demonstrate this, let's build an object in Python, and then replicate the behavior in Rust. For our example, we will build a basic stock object as seen in the following code:
class Stock: def __init__(self, name: str, open_price: float,\ stop_loss: float = 0.0, take_profit: float = 0.0) \ -> None: self.name: str = name self.open_price: float = open_price self.stop_loss: float = stop_loss self.take_profit: float = take_profit self.current_price: float = open_price def update_price(self, new_price: float) -> None: self.current_price = new_price
Here, we can see that we have two mandatory fields, which are the name and price of the stock. We can also have an optional stop loss and an optional take profit. This means that if the stock crosses one of these thresholds, it forces a sale, so we don't continue to lose more money or keep letting the stock rise to the point where it crashes. We then have a function that merely updates the current price of the stock. We could add extra logic here on the thresholds for it to return a bool for whether the stock should be sold or not if needed. For Rust, we define the fields with the following code:
struct Stock { name: String, open_price: f32, stop_loss: f32, take_profit: f32, current_price: f32 }
Now we have our fields for the struct, we need to build the constructor. We can build functions that belong to our struct with an impl
block. We build our constructor with the following code:
impl Stock { fn new(stock_name: &str, price: f32) -> Stock { return Stock{ name: String::from(stock_name), open_price: price, stop_loss: 0.0, take_profit: 0.0, current_price: price } } }
Here, we can see that we have defined some default values for some of the attributes. To build an instance, we use the following code:
let stock: Stock = Stock::new("MonolithAi", 95.0);
However, we have not exactly replicated our Python object. In the Python object __init__
, there were some optional parameters. We can do this by adding the following functions to our impl
block:
fn with_stop_loss(mut self, value: f32) -> Stock { self.stop_loss = value; return self } fn with_take_profit(mut self, value: f32) -> Stock { self.take_profit = value; return self }
What we do here is take in our struct, mutate the field, and then return it. Building a new stock with a stop loss involves calling our constructor followed by the with_stop_loss
function as seen here:
let stock_two: Stock = Stock::new("RIMES",\ 150.4).with_stop_loss(55.0);
With this, our RIMES stock will have an open price of 150.4
, current price of 150.4
, and a stop loss of 55.0
. We can chain multiple functions as they return the stock struct. We can create a stock struct with a stop loss and a take profit with the following code:
let stock_three: Stock = Stock::new("BUMPER (former known \ as ASF)", 120.0).with_take_profit(100.0).\ with_stop_loss(50.0);
We can continue chaining with as many optional variables as we want. This also enables us to encapsulate the logic behind defining these attributes. Now that we have all our constructor needs sorted, we need to edit the update_price
attribute. This can be done by implementing the following function in the impl
block:
fn update_price(&mut self, value: f32) { self.current_price = value; }
This can be implemented with the following code:
let mut stock: Stock = Stock::new("MonolithAi", 95.0); stock.update_price(128.4); println!("here is the stock: {}", stock.current_price);
It has to be noted that the stock needs to be mutable. The preceding code gives us the following printout:
here is the stock: 128.4
There is only one concept left to explore for structs and this is traits. As we have stated before, traits are like Python mixins. However, traits can also act as a data type because we know that a struct that has the trait has those functions housed in the trait. To demonstrate this, we can create a CanTransfer
trait in the following code:
trait CanTransfer { fn transfer_stock(&self) -> (); fn print(&self) -> () { println!("a transfer is happening"); } }
If we implement the trait for a struct, the instance of the struct can utilize the print
function. However, the transfer_stock
function doesn't have a body. This means that we must define our own function if it has the same return value. We can implement the trait for our struct using the following code:
impl CanTransfer for Stock { fn transfer_stock(&self) -> () { println!("the stock {} is being transferred for \ £{}", self.name, self.current_price); } }
We can now use our trait with the following code:
let stock: Stock = Stock::new("MonolithAi", 95.0); stock.print(); stock.transfer_stock();
This gives us the following output:
a transfer is happening the stock MonolithAi is being transferred for £95
We can make our own function that will print and transfer the stock. It will accept all structs that implement our CanTransfer
trait and we can use all the trait's functions in it, as seen here:
fn process_transfer(stock: impl CanTransfer) -> () { stock.print(); stock.transfer_stock(); }
We can see that traits are a powerful alternative to object inheritance; they reduce the amount of repeated code for structs that fit in the same group. There is no limit to the number of traits that a struct can implement. This enables us to plug traits in and out, adding a lot of flexibility to our structs when maintaining code.
Traits are not the only way by which we can manage how structs interact with the rest of the program; we can achieve metaprogramming with macros, which we will explore in the next section.