Exercise: repeat a motif
Goal
Build a Vec<u8> (a growable byte array on the heap) by appending a motif N times. This is your first contact with mutable variables (let mut, Rust’s way of saying “this binding can change”), for loops over a range (0..n), and the extend_from_slice method (which appends every element of a slice to the end of a Vec).
Background
The two ways you usually create an empty Vec are Vec::new(), which allocates nothing up front, and Vec::with_capacity(n), which pre-allocates room for n elements so subsequent pushes do not have to reallocate (grow the backing buffer). Both produce a correct answer here — Vec::with_capacity is purely an optimisation. A plain Vec::new() would also work; the Vec would grow itself as you append, doubling its backing buffer a handful of times. Pre-allocating when you already know the final size — and here you do, it’s motif.len() * n — just skips those re-allocations, which can matter inside a hot loop (a loop that runs millions of times). To actually append bytes, extend_from_slice copies every byte of a &[u8] onto the end of the Vec in one call.
One detail: because we modify the same Vec on every iteration of the loop, the binding has to be declared with let mut (not just let). Plain let in Rust makes the binding immutable by default.
The exercise
Open the starter project:
cd day1/ex-repeat-motifYou will see a single Rust source file at src/main.rs. It already contains:
- A function
fn repeat(motif: &[u8], n: usize) -> Vec<u8>with a// TODObody - A
mainfunction that reads a motif and a count from the command line and callsrepeat - A
#[cfg(test)] mod testsblock with four unit tests (empty motif,n == 0,ATthree times, andGfive times)
Your job: replace the TODO body so that all four tests pass.
How to run
From inside day1/ex-repeat-motif/:
cargo test # run the tests — these should be failing at first
cargo run -- AT 5 # run the program and see ATATATATATWhen the function is correct, cargo test will print:
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured
and your program should print:
$ cargo run -- AT 5
ATATATATAT
Hint
Pre-allocate, append in a loop, return:
let mut out = Vec::with_capacity(motif.len() * n);
for _ in 0..n {
out.extend_from_slice(motif);
}
outThe pieces:
Vec::with_capacitymakes an emptyVecwith room formotif.len() * nbytes already reserved.let mutis required becauseextend_from_slicemutatesout.0..nis aRange, andfor _ in 0..niterates exactlyntimes — the_says we do not care about the index.extend_from_slicecopies every byte ofmotifonto the end ofout.- The final
out(no semicolon) is the function’s return value.
Solution
fn repeat(motif: &[u8], n: usize) -> Vec<u8> {
let mut out = Vec::with_capacity(motif.len() * n);
for _ in 0..n {
out.extend_from_slice(motif);
}
out
}The full reference implementation lives in day1/ex-repeat-motif-solution/src/main.rs.
Where this leads
The upcoming exercises (GC content, complement, base counts, …) all build on these basics: a function over &[u8], a for loop, and — for the ones that produce a new sequence — a Vec<u8> built up with extend_from_slice or push.