Sage-Code Laboratory
index<--

Rust Objects

Object-Oriented Programming (OOP) is a programming paradigm centered around the idea of objects, which are a combination of data and related methods. OOP allows developers to write code that is modular, re-usable, and easy to maintain.

Page Bookmarks



Abstraction

Rust's implementation of Object-Oriented concepts provides a way for developers to define data structures that are encapsulated and have their own behavior (methods) and traits can be used as interfaces to define behaviors, similar to inheritance.

Generics are used to create polymorphic functions and data structures, while trait objects provide dynamic dispatch, allowing for object-oriented code to run in Rust safely and effectively.

Encapsulation

Encapsulation is a fundamental principle of Object-Oriented Programming that refers to the bundling of data and methods into a single unit. In Rust, this is achieved using a struct which combines data and methods. The struct keyword in Rust is used to define a struct. Here's an example:

rust
struct Person {
    name: String,
    age: u32,
}

In this example, we define a struct called "Person" with two fields: "name" of type "String" and "age" of type "u32". These fields are encapsulated within the "Person" struct.

Inheritance

Inheritance is another principle of Object-Oriented Programming that allows for the creation of a new class, called a subclass, that is a modified version of an existing class, called a superclass. Rust does not natively support inheritance, but this can be achieved using traits. Traits are similar to interfaces in other languages and define a set of methods that a type must implement. Here's an example:


trait Animal {
    fn speak(&self) -> &str;
}

struct Dog {
    name: String,
}

impl Animal for Dog {
    fn speak(&self) -> &str {
        "woof"
    }
}

In this example, we define a trait called "Animal" with a method called "speak". We then define a struct called "Dog" with a field called "name". We implement the "Animal" trait for the "Dog" struct, which means we must implement the "speak" method for the "Dog" struct. In this case, the "speak" method returns the string "woof".

Polymorphism

Polymorphism is a principle of Object-Oriented Programming that allows different objects to be treated as if they were the same type. Rust supports polymorphism through both static and dynamic dispatch.

Static dispatch is accomplished through the use of generics. Here is an example:


struct Pair<T> {
    x: T,
    y: T,
}

impl<T> Pair<T> {
    fn new(x: T, y: T) -> Self {
        Self { x, y }
    }
    
    fn get_x(&self) -> &T {
        &self.x
    }
    
    fn get_y(&self) -> &T {
        &self.y
    }
}

In this example, we define a struct called "Pair" that takes a type parameter "T". We have methods that allow us to retrieve the "x" and "y" fields of the "Pair" struct. Because we defined our methods on the "Pair<T>" type, we can create a "Pair" of any type "T" and access the same methods.

Dynamic dispatch is accomplished through the use of trait objects. Here is an example:


trait Shape {
    fn area(&self) -> f64;
}

struct Circle {
    radius: f64,
}

impl Shape for Circle {
    fn area(&self) -> f64 {
        self.radius * self.radius * std::f64::consts::PI
    }
}

struct Square {
    length: f64,
}

impl Shape for Square {
    fn area(&self) -> f64 {
        self.length * self.length
    }
}

fn get_area(shape: &dyn Shape) -> f64 {
    shape.area()
}

In this example, we define a trait called "Shape" with a method called "area". We then define two structs: "Circle" and "Square", both of which implement the "Shape" trait. We define a function called "get_area" that takes a reference to a value that implements the "Shape" trait. We can then pass in a "Circle" or "Square" to the "get_area" function, and it will call the "area" method based on the type of the object passed in, providing dynamic dispatch.

Conclusion

Overall, while Rust is not a pure Object-Oriented Programming language, it does provide support for many of the fundamental principles of Object-Oriented Programming through features such as structs, traits, generics, and trait objects. By leveraging these language features, Rust programmers can create code that is modular, reusable, and easy to read and maintain.


Read next: Modules