The error message you're encountering is due to the fact that Rust's type system requires functions with return types to be consistent and known at compile time. When using Box<dyn Trait> (or similar dynamic dispatch), you can return trait objects from a function, which allows for more flexibility in terms of what concrete types are returned.
In your case, since the function returns either a Tweet or a NewsArticle, both of which implement the Summary trait, but they have different concrete types, you need to use dynamic dispatch (i.e., returning a boxed trait object) instead of static dispatch (returning a specific type).
Here's how you can modify your code:
rust1fn returns_summarizable(flag: bool) -> Box<dyn Summary> { 2 if flag { 3 Box::new(Tweet { 4 username: String::from("horse_ebooks"), 5 content: String::from( 6 "of course, as you probably already know, people", 7 ), 8 reply: false, 9 retweet: false, 10 }) 11 } else { 12 Box::new(NewsArticle { 13 headline: String::from("Penguins win the Stanley Cup Championship!"), 14 location: String::from("Pitt 15 16[Read the full article at DEV Community](https://dev.to/someb1oody/rust-guide-104-trait-pt2-traits-as-parameters-and-return-types-trait-bounds-280a) 17 18--- 19 20**Want to create content about this topic?** [Use Nemati AI tools](https://nemati.ai) to generate articles, social posts, and more.
![[Rust Guide] 10.4. Trait Pt.2 - Traits as Parameters and Return Types, Trait Bounds](/_next/image?url=https%3A%2F%2Fmedia.nemati.ai%2Fmedia%2Fblog%2Fimages%2Farticles%2F8525759a0a924f45.webp&w=3840&q=75)
![[AINews] The Unreasonable Effectiveness of Closing the Loop](/_next/image?url=https%3A%2F%2Fmedia.nemati.ai%2Fmedia%2Fblog%2Fimages%2Farticles%2F600e22851bc7453b.webp&w=3840&q=75)



