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-annotate

You will see a single Rust source file at src/main.rs. It already contains:

  • The Annotate trait definition
  • Three types: a struct GenomicInterval, an enum Strand, and a struct Sequence
  • Three impl Annotate for ... blocks, each with a // TODO body that currently returns an empty string
  • A main that builds one of each and prints their descriptions
  • A #[cfg(test)] mod tests block with seven unit tests that pin down what each description should 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 types

When 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: a format! call using self.chrom, self.start, self.end, and the length self.end - self.start.
  • Strand: a match self with one arm per variant, each returning a string literal converted with .to_string().
  • Sequence: an if self.bases.is_empty() returning "(empty sequence)"; otherwise count GC bytes with .iter().filter(...).count(), compute an integer percentage, decode the bytes with std::str::from_utf8, and format! 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.