The situation
The coastal antenna at Kestrel Ridge writes everything it hears to one long log, and it is not fussy about what "everything" means. Squelch noise gets logged as tildes. Half the operators pad their transmissions with spaces. Somewhere in the last four hours there is a boat asking for help.
The watch officer wants the fields pulled apart: who transmitted, and what they said. What she does not want is a program that copies four hours of log into new Strings to do it — the log is the log, and your functions should hand back views into it.
Every function here returns a &str that borrows from its argument. No String, no .to_string(), no allocation.
The log format
A line looks like this, tildes and all:
~~KR7 [SOS drifting, need tow] 14:22~~- Leading and trailing
~and spaces are squelch noise. Not part of the message. - The first word after the noise is the callsign.
- The text between the first
[and the next]is the payload. - Everything else (timestamps, junk) is not your problem.
Your mission
1. strip_noise(line: &str) -> &str
Trim ~ and spaces from both ends. Nothing in the middle changes.
| Input | Output |
|---|---|
"~~KR7 [SOS] 14:22~~" | "KR7 [SOS] 14:22" |
" MV Halcyon [all well]" | "MV Halcyon [all well]" |
"~~~~" | "" |
"KR7" | "KR7" |
2. callsign(line: &str) -> &str
The first whitespace-separated word of the de-noised line. Empty when there is nothing left.
| Input | Output |
|---|---|
"~~KR7 [SOS] 14:22~~" | "KR7" |
" MV Halcyon [all well]" | "MV" |
"~~~~" | "" |
3. payload(line: &str) -> &str
What sits between the first [ and the next ] after it. Empty when either bracket is missing, and empty when the brackets are empty.
| Input | Output |
|---|---|
"KR7 [SOS drifting, need tow] 14:22" | "SOS drifting, need tow" |
"KR7 no brackets here" | "" |
"KR7 []" | "" |
"KR7 [unterminated" | "" |
4. is_distress(line: &str) -> bool
True when the payload starts with SOS (exactly those three uppercase letters). "SOS drifting" counts, "sos" does not, "NO SOS HERE" does not — it has to be at the start.
How you're graded
Hidden tests, named for what they check:
strip_noise_trims_both_endsstrip_noise_leaves_the_middle_alonecallsign_takes_the_first_wordpayload_reads_between_the_bracketspayload_is_empty_when_brackets_are_missingis_distress_only_at_the_starteverything_borrows_nothing_is_copied← this one compares pointers, not text
That last one is the interesting one. It checks that your return value points into the input string, not at a fresh copy — which is a thing you can only pass by slicing rather than building. If it fails, look for a to_string(), a format!, or a String you snuck in.
Stretch goals
- Make
mainprint only the distress calls, with their callsign. - Add
fn timestamp(line: &str) -> &strreturning the trailingHH:MM, empty if absent. - Handle a payload that contains a nested
]. Which bracket should win, and why is "the first one" the cheaper rule?