Generates comprehensive documentation for Rust traits, covering shared behavior, polymorphism, trait bounds, and implementation patterns with practical examples.
Generate comprehensive documentation for Rust traits based on The Rust Programming Language book's chapter on "Defining Shared Behavior with Traits."
This skill helps you create detailed documentation for Rust traits, covering:
Follow these steps to generate Rust trait documentation:
Determine what aspect of Rust traits needs documentation:
If documenting existing code:
Organize documentation using this structure:
**Trait Overview**
**Trait Definition**
**Implementation Guide**
**Trait Bounds and Generics**
**Best Practices**
Create clear, runnable examples:
```rust
// Trait definition
pub trait Summary {
fn summarize(&self) -> String;
}
// Implementation on custom type
pub struct NewsArticle {
pub headline: String,
pub author: String,
pub content: String,
}
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {}", self.headline, self.author)
}
}
// Using trait bounds
pub fn notify(item: &impl Summary) {
println!("Breaking news! {}", item.summarize());
}
```
If the trait has default methods, show:
Example:
```rust
pub trait Summary {
fn summarize_author(&self) -> String;
fn summarize(&self) -> String {
format!("(Read more from {}...)", self.summarize_author())
}
}
```
Document coherence constraints:
Show different syntax options:
```rust
// impl Trait syntax
pub fn notify(item: &impl Summary) { }
// Trait bound syntax
pub fn notify<T: Summary>(item: &T) { }
// Multiple trait bounds
pub fn notify<T: Summary + Display>(item: &T) { }
// Where clauses for complex bounds
pub fn notify<T>(item: &T)
where
T: Summary + Display,
{ }
```
Demonstrate practical usage:
Document common issues:
Ensure documentation:
```rust
/// # Summary Trait
///
/// The `Summary` trait defines shared behavior for types that can
/// provide a summary of their content.
///
/// ## When to Implement
///
/// Implement `Summary` for any type that represents content that can
/// be meaningfully summarized in a single string.
///
/// ## Example
///
/// ```rust
/// use aggregator::Summary;
///
/// pub struct Article {
/// pub title: String,
/// pub author: String,
/// }
///
/// impl Summary for Article {
/// fn summarize(&self) -> String {
/// format!("{} by {}", self.title, self.author)
/// }
/// }
/// ```
pub trait Summary {
/// Returns a summary string for this item.
///
/// Implementations should provide a concise, human-readable
/// summary appropriate for the type.
fn summarize(&self) -> String;
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/rust-traits-documentation-generator/raw