Hone logo
Hone
Problems

Implementing a Summarizable Trait in Rust

Traits in Rust allow you to define shared behavior that can be implemented by different types. This challenge will test your understanding of defining and implementing traits, enabling polymorphism and code reusability. You will create a trait that defines a method for generating a summary and then implement it for different data structures.

Problem Description

Your task is to define a trait called Summarizable in Rust. This trait should have a single method, summary(), which takes an immutable reference to self and returns a String representing a summary of the object.

You will then implement this Summarizable trait for two different types:

  1. NewsArticle: A struct representing a news article, containing a headline (String), author (String), and content (String). The summary for a news article should be its headline, followed by a hyphen, and then the first 50 characters of its content. If the content is shorter than 50 characters, the entire content should be used.
  2. Tweet: A struct representing a tweet, containing a username (String) and message (String). The summary for a tweet should be the username, followed by a colon and a space, and then the entire message.

Finally, you should demonstrate the usage of your trait by creating instances of NewsArticle and Tweet and calling their respective summary() methods, printing the results.

Examples

Example 1:

Input:
let article = NewsArticle {
    headline: String::from("Rust 1.70 Released!"),
    author: String::from("Rust Team"),
    content: String::from("The Rust programming language team is thrilled to announce the release of Rust 1.70. This release brings a host of new features, performance improvements, and bug fixes. Key highlights include better compile times for certain code patterns and enhanced tooling support. Developers can expect a smoother and more efficient coding experience. We encourage everyone to upgrade and explore the new capabilities. Happy coding!"),
};

let tweet = Tweet {
    username: String::from("rustacean_dev"),
    message: String::from("Just learned about Rust's ownership system. Mind blown! 🤯"),
};

println!("{}", article.summary());
println!("{}", tweet.summary());

Output:
Rust 1.70 Released! - The Rust programming language team is thrilled to ann
rustacean_dev: Just learned about Rust's ownership system. Mind blown! 🤯

Explanation: The NewsArticle summary truncates the content after 50 characters. The Tweet summary simply concatenates the username and message with a colon and space.

Example 2:

Input:
let short_article = NewsArticle {
    headline: String::from("Local Bird Spotted"),
    author: String::from("Jane Doe"),
    content: String::from("A rare blue jay was seen in Central Park today."),
};

println!("{}", short_article.summary());

Output:
Local Bird Spotted - A rare blue jay was seen in Central Park today.

Explanation: Since the content of short_article is less than 50 characters, the entire content is included in the summary.

Constraints

  • The summary() method must return a String.
  • The content truncation for NewsArticle should be exactly 50 characters, or the full content if shorter.
  • The provided structs (NewsArticle, Tweet) must be used as is.

Notes

  • Consider how to efficiently handle string slicing for the NewsArticle content.
  • This challenge focuses on the fundamental usage of traits for polymorphism.
  • You can define the Summarizable trait and implement it within the same file or separate modules as you see fit.
  • The goal is to write clean, idiomatic Rust code.
Loading editor...
rust