No requirement to import a trait for using an implemented public method from it

I think it’s rather easy to forget to import a trait just to use it for a sake of one or two methods.

Example: is_empty method for Collection trait. If you use a lot of collections in your code, I think it looks confusing if you are required to import one more thing while you even can’t see its name anywhere else in a code.

mod test {
    
    pub struct MyStruct { field: int }
    
    pub trait MyTrait {
        fn test_method(&self);
        fn other_method(&self) { } // I want this method to be accessible w/o special import
    }
    
    impl MyStruct {
        pub fn new() -> MyStruct { MyStruct{field: 5} }
    }
    
    impl MyTrait for MyStruct { 
        fn test_method(&self) { }
    }
    
}

fn main() {
    use test::MyTrait; // this is required to see other_method now

    let test_struct = test::MyStruct::new();
    test_struct.other_method();
}

Another example (may be not so Rust-y, but anyway) is chainable methods implemented in a trait (or a number of traits) to use in several structs—if you are required to import a trait (or even traits) to use some part of them, doesn’t it looks inconvenient?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.