:timing
:sccache 1
Rust's design is different from traditional OOP programming languages. There is no class in Rust, however, you can achieve it using
struct
+impl
.Rust does not have the concept of constructor (since Rust even does not have the concept of class). A simple way is to directly create a struct of initialized values. If complicated intermediate operations are required, you can implement a static
new
method for the struct.
Sized vs ?Sized¶
Sync vs !Sync¶
struct Person {
name: String,
age: u8,
}
Construct Instances of a Struct¶
There is only one true way to create an instance of a struct. Taking the struct
Person
as an example.Person { name: "Jane Doe", age: 30 }
However, this way won't work when a struct contains private fields and you have to initialize an instance of the struct in a different module. The convention in Rust is to create a public
new
method for constructing instances of a struct.The
Default
trait has adefault()
method for constructing a default instance of a struct. It is suggested that you implement theDefault
trait if you want to provide a default constructor for a struct.
For more discussions, please refer to the YouTube tutorial Idiomatic Rust - Constructors .
Create an Instance of Person¶
let p = Person {name: String::from("Ben Du"), age: 30,};
p.name
p.age
Implement a new
Method for Constructing Instances¶
impl Person {
fn new (name: &str, age: u8) -> Person {
Person {
name: name.to_string(),
age: age,
}
}
}
let p2 = Person::new("Ben Du", 30);
p2.name
p2.age
Associated Constants¶
impl Person {
const DENOMINATOR: u32 = 4567891;
}
let p = Person::new("Ben Du", 31);
p.name
p.age
Person::DENOMINATOR
impl Person {
fn get_deno() -> u32 {
return Person::DENOMINATOR
}
}
let p = Person::new("Ben Du", 32);
p.name
p.age
Person::get_deno()
Hash and Eq¶
Please refer to Hash in Rust for more discussions.
Copy and Clone¶
Please refer to The Copy and Clone Traits in Rust for more discussions.
Shared Method in Different Struct¶
There are a few ways to achieve this.
Use a trait and define a shared method as a default implementation in the trait. This is convenient if the shared method does not refer to fields of struct (or only read fields of struct).
Use macro to generate common methods. This way is more complicated and it is not recommended unless the share method is a generic useful method.
Define a base struct with the shared method and include the base struct as a field in other structs. This is called composition (in contrast to inheritance). Composition is preferred over inheritance nowadays.
Functions¶
Please refer to Functions in Rust for more discussions.