Exercise: a custom trait with multiple impls
Goal
Define your own trait (Rust’s name for an interface — a named bundle of methods a type can implement) called Annotate with a single method fn description(&self) -> String. Implement it for three different biological types: GenomicInterval, an enum (a type with a fixed set of variants) Strand, and a sequence newtype (a struct wrapping a single field) Sequence.
Background
A trait declares a set of method signatures; an impl Trait for Type block defines those methods for a specific type. Once you have one trait with several implementations, the same calling code can use any of them interchangeably: x.description() compiles for any x whose type implements Annotate, and on day 8’s next exercise we will write a single function that is generic (works for many types) over exactly that bound. This is the basic mechanism Rust uses in place of class inheritance — instead of “type B is-a type A”, we say “types A and B both implement trait T”, and the language guarantees at compile time that every method on T is provided.
The syntax looks like:
trait Annotate {
fn description(&self) -> String;
}
impl Annotate for GenomicInterval {
fn description(&self) -> String { /* ... */ }
}The exercise
Open the starter project:
cd day8/ex-annotateYou will see a single Rust source file at src/main.rs. It already contains:
- The
Annotatetrait definition - Three types: a
structGenomicInterval, anenumStrand, and astructSequence - Three
impl Annotate for ...blocks, each with a// TODObody that currently returns an empty string - A
mainthat builds one of each and prints their descriptions - A
#[cfg(test)] mod testsblock with seven unit tests that pin down what eachdescriptionshould return
Your job: fill in the three description method bodies so that all seven tests pass. The expected output strings are spelled out in the tests; read them first.
You may not change the trait, the type definitions, or the tests. You may add helper functions if you want.
How to run
From inside day8/ex-annotate/:
cargo build # compile
cargo test # run the tests — these should be failing at first
cargo run # print a sample description of each of the three typesWhen all seven tests pass, cargo test will print:
test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured
and cargo run should print three lines, one per type.
Hint
Each description can be a single expression:
GenomicInterval: aformat!call usingself.chrom,self.start,self.end, and the lengthself.end - self.start.Strand: amatch selfwith one arm per variant, each returning a string literal converted with.to_string().Sequence: anif self.bases.is_empty()returning"(empty sequence)"; otherwise count GC bytes with.iter().filter(...).count(), compute an integer percentage, decode the bytes withstd::str::from_utf8, andformat!the result.
Solution
The full reference implementation lives in day8/ex-annotate-solution/src/main.rs. Try not to peek at it until you have your own version passing all seven tests — the point of the exercise is the practice, not the final code.
Where this leads
The next exercise, 03-generic-function.qmd, writes a single function that is generic over any type implementing Annotate — so you can pass it a GenomicInterval, a Strand, or a Sequence and it works for all three with no extra code. That is the payoff for defining a trait in the first place.