How to implement traits into separate files?

I know this is a dumb question but I had a difficulties with traits. I followed the tutorial about Traits and its implementation from Rust tutorial here.
The problem starts when I need to separate each functions and traits into separate files. Wherever I need to call fn summary(), I also had to include the summary_traits file before I can use it

Why do I need to include this summary file into the main.rs?

// summary_traits.rs
pub trait Summary {
    fn summarize() -> String;
}

// tweet.rs
use summary_traits::Summary;
pub struct Tweet {
    pub username: String,
    pub content: String,
    pub reply: bool,
    pub retweet: bool,
}

impl Summary for Tweet {
    fn summarize() -> String {
        format!("THIS IS A STATIC FUNCTION FOR TWEET")
    }
}

// main.rs
use tweet::Tweet; // call the Tweet struct
use summary_traits::Summary; // Why do I need this ? 
fn main() {
    println!("{}", Tweet::summarize());
    // if I do not add the summary_traits, this will cause an error:
    //  ^^^^^^^^^^^^^^ function or associated item not found in `tweet::Tweet
}

There's a few reasons. One is so that the compiler doesn't have to gather information from the entirety of your crate (or your crate and all your dependencies) to compile a single module.

Another is that different traits can have items with the same name, but be implemented for the same struct. If every trait was present by default, the chance of conflict would go up. (You can use qualified paths to disambiguate, but it's much less ergonomic.) This includes traits in the standard library, for example.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.