为什么特质不在范围内?

时间:2018-07-07 20:59:52

标签: rust

我正在学习Rust,并且已经在模块“图形”中定义了特征“形状”和子类“圆”。我的问题是,我是否真的必须在main内部提供Shape的使用声明以及Shape.rs的完整路径?如果删除它,则会出现编译错误

error[E0599]: no method named `draw` found for type `figures::circle::Circle` in the current scope                 
 --> src/main.rs:8:7
  |
8 |     c.draw()
  |       ^^^^
  | 
 ::: src/figures/circle.rs:3:1
  |
3 | pub struct Circle {}
  | ----------------- method `draw` not found for this
  |
  = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
  |
1 | use figures::shape::Shape;
  |

error: aborting due to previous error

文件结构

src/
├── figures
│   ├── circle.rs
│   ├── mod.rs
│   └── shape.rs
└── main.rs

--- main.rc ---

mod figures;                                                                                                       

// Why is this needed when Shape is exported and redeclared in mod.rs?                                                                                             
use figures::shape::Shape;                                                                                         

fn main() {                                                                                                        
    let c = figures::Circle::new();                                                                                
    c.draw()                                                                                                       
}   

---数字/圆---

use figures::shape::Shape;                                                                                         

pub struct Circle {}                                                                                               

impl Circle {                                                                                                      
    pub fn new() -> Circle {                                                                                       
        println!("Circle new");                                                                                    

        Circle {}                                                                                                  
    }                                                                                                              
}                                                                                                                  

impl Shape for Circle {                                                                                            
    fn draw(&self) {                                                                                               
        println!("Circle draw")                                                                                    
    }                                                                                                              
}                                                                                                                  

impl Drop for Circle {                                                                                             
    fn drop(&mut self) {                                                                                           
        println!("Circle drop");                                                                                   
    }                                                                                                              
}   

--- mod.rs ---

// Expose modules to caller                                                                                        
pub mod circle;                                                                                                    
pub mod shape;                                                                                                     

// Redeclare circle::Circle -> Circle and shape::Shape -> Shape                                                    
pub use self::circle::Circle;                                                                                      
pub use self::shape::Shape;

--- circle.rs ---

pub trait Shape {                                                                                                  
    fn draw(&self);                                                                                                
} 

0 个答案:

没有答案