你在找
   
    
     std::ops::Deref
    
   
   :
  
  
   
    除了用于(一元)的显式解引用操作之外
    
     *
    
    不可变上下文中的运算符,
    
     Deref
    
    在许多情况下编译器也隐式使用。这个机制叫做
    
     
      Deref
     
     coercion
    
    '.在可变的上下文中,
    
     
      DerefMut
     
    
    使用。
   
  
  
   进一步:
  
  
   
    如果
    
     T
    
    工具
    
     Deref<Target = U>
    
    和
    
     x
    
    是类型的值
    
     T
    
    ,然后:
   
   
    - 
     在不可变的上下文中,
     
      *x
     在非指针类型上等于
      *Deref::deref(&x)
     .
- 
     类型值
     
      &T
     被强制为类型的值
      &U
     
- 
     
      T
     隐式实现该类型的所有(不可变)方法
      U
     .
    有关详细信息,请访问
    
     the chapter in
     
      The Rust Programming Language
     
    
    以及
    
     the dereference operator
    
    ,
    
     method resolution
    
    和
    
     type coercions
    
    .
   
  
  
   通过实施
   
    德雷夫
   
   它将起作用:
  
  impl Deref for AxisX {
    type Target = i32;
    fn deref(&self) -> &i32 {
        &self.0
    }
}
x.abs() + 3
  
   你可以在
   
    Playground
   
   .
  
  
   但是,如果从基础类型调用函数(
   
    i32
   
   在这种情况下,返回类型将保留为基础类型。因此
  
  assert_eq!(AxisX(10).abs() + AxisY(20).abs(), 30);
  
   会过去的。要解决此问题,您可以覆盖一些您需要的方法:
  
  impl AxisX {
    pub fn abs(&self) -> Self {
        // *self gets you `AxisX`
        // **self dereferences to i32
        AxisX((**self).abs())
    }
}
  
   这样,上面的代码就失败了。
   
    Take a look at it in action
   
   .