Dot operator
As stated in Rustonomicon, the Dot operator in Rush does a lot of magic for you. Namely, those are auto-referencing, auto-dereferencing, and coercion UNTIL types match.
This becomes really powerful when you implement Deref trait or the kind.
For example, suppose we have the following structs:
struct Wrapped;
struct Wrapper1(Wrapped);
struct Wrapper2(Wrapper1);
Just to stay on the point, I created two fairly simple tuple structs and a unit-like one. And let’s implement Deref trait on both Wrapper1 and Wrapper2 as follows:
领英推荐
impl Deref for Wrapper2{
type Target = Wrapper1;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Deref for Wrapper1{
type Target = Wrapped;
fn deref(&self) -> &Self::Target {
&self.0
}
}
Lastly, let’s implement the method on each struct:
impl Wrapper2{
fn from_wrapper2(&self){
println!("Wrapper2")
}
}
impl Wrapper1{
fn from_wrapper1(&self){
println!("Wrapper1")
}
}
impl Wrapped{
fn from_wrapped(&self){
println!("Wrapped")
}
}
In the main function, let’s create a Wrapper2 object wrapping Wrapper1 that will also wrap Wrapped:
fn main (){
let test = Wrapper2(Wrapper1(Wrapped));
At this point, what will happen if you put dot(.) after the test? That is to say, what will appear in your IDE? The answer is that you have access to all of the methods implemented above!
fn main (){
let test = Wrapper2(Wrapper1(Wrapped));
test.from_wrapper2(); // Ok
test.from_wrapper1(); // Ok
test.from_wrapped(); // Ok
}