Characteristics of Object-Oriented Languages
Rust implements OOP concepts differently than traditional languages. Here’s how Rust handles the core OOP characteristics:
Objects: Data + Behavior
Rust structs and enums with impl
blocks provide the same functionality as objects - they package data and methods together:
pub struct AveragedCollection {
list: Vec<i32>,
average: f64,
}
Encapsulation
Rust uses pub
keyword for visibility control. By default, everything is private:
pub struct AveragedCollection {
list: Vec<i32>,
average: f64,
}
Public methods control access to private data:
pub struct AveragedCollection {
list: Vec<i32>,
average: f64,
}
impl AveragedCollection {
pub fn add(&mut self, value: i32) {
self.list.push(value);
self.update_average();
}
pub fn remove(&mut self) -> Option<i32> {
let result = self.list.pop();
match result {
Some(value) => {
self.update_average();
Some(value)
}
None => None,
}
}
pub fn average(&self) -> f64 {
self.average
}
fn update_average(&mut self) {
let total: i32 = self.list.iter().sum();
self.average = total as f64 / self.list.len() as f64;
}
}
Private fields prevent direct manipulation, ensuring data consistency. The internal implementation can change (e.g., from Vec<i32>
to HashSet<i32>
) without breaking external code.
Inheritance: Not Supported
Rust has no traditional inheritance. Instead:
For code reuse: Use default trait implementations:
trait Summary { fn summarize(&self) -> String { "Default implementation".to_string() } }
For polymorphism: Use trait objects or generics with trait bounds:
// Trait objects (dynamic dispatch) fn process(item: &dyn Summary) { } // Generics (static dispatch) fn process<T: Summary>(item: &T) { }
Rust’s approach avoids inheritance issues like tight coupling and fragile base class problems. Trait objects enable polymorphism where different types can be treated uniformly if they implement the same trait.