Dot operator

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
    }        

要查看或添加评论,请登录

Migo Lee的更多文章

  • Redis as distributed system

    Redis as distributed system

    Transaction handling with Multi Transactions are used to perfrom operations across multiple types or dataset. They're…

  • Raft - idempotency

    Raft - idempotency

    What would happen if the leader fails before they returns response to a client? The client may retry its request. But…

  • Raft - commitIndex and lastApplied

    Raft - commitIndex and lastApplied

    Read this post first if you haven't already. Changes in Raft Now, we learned that in Raft we expect two different…

  • Raft Leader Election and up-to-date leader guarantee

    Raft Leader Election and up-to-date leader guarantee

    Read this if you haven't already. Why Leader Election? In Raft, every write operation comes to the leader first.

  • Raft Replication

    Raft Replication

    Periodic heartbeat The leader sends periodic heartbeat messages approximately every 300–500 milliseconds. Each…

  • Case for layered architecture over hexagonal architecture

    Case for layered architecture over hexagonal architecture

    Why hexagonal in most of web applications? When building applications for web services, we tend to think less about…

  • Flexible Input Parameters: From, AsRef

    Flexible Input Parameters: From, AsRef

    In this article, I’ll be covering two built-in traits that make your life in Rust easier: AsRef From AsRef Firstly…

  • Making Back Reference Using Weak Reference

    Making Back Reference Using Weak Reference

    Ownership - What’s the owner? This may sound weird but really, in Rust, when you declare ‘variable’ what it means is…

  • Retry Logic By Returning Consumed Argument On Error(And Never Type)

    Retry Logic By Returning Consumed Argument On Error(And Never Type)

    In Rust, a function may consume an argument like the following function: pub fn send(connection_uri: a…

  • Changing Enum In Place

    Changing Enum In Place

    When dealing with Enum, you may find it difficult to change values because of the borrow checking system in Rust…

社区洞察

其他会员也浏览了