const std = @import("std");
const mem = std.mem;

pub fn toRna(allocator: mem.Allocator, dna: []const u8) mem.Allocator.Error![]const u8 {
    var rna_slice = try allocator.alloc(u8, dna.len);
    for (dna, 0..) |dna_nucleotide, i| {
        switch (dna_nucleotide) {
            'A' => rna_slice[i] = 'U',
            'C' => rna_slice[i] = 'G',
            'G' => rna_slice[i] = 'C',
            'T' => rna_slice[i] = 'A',
            else => unreachable,
        }
    }
    return rna_slice;
}

A switch maps each of the four DNA bases to its RNA complement.

The else => unreachable prong states that no other byte can occur. unreachable is an assertion: in safe build modes reaching it panics, and in ReleaseFast/ReleaseSmall it is undefined behavior the optimizer may assume never happens. The exercise's inputs only ever contain A, C, G, and T, so the prong is never taken.

23e Sep 2026 · Tu l'as trouvée utile ?