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
}