Build a tiny version of the Unix sort command. Given a list of lines, return them sorted in ascending order by character code (like LC_ALL=C sort: uppercase letters come before lowercase ones, "Zebra" before "apple", and an empty line comes first), joined by newline characters (\n). Duplicates are all kept. If the list is empty, return the empty string.
Function signature:
fn sort_lines(lines: Vec<String>) -> String
Example:
Input: lines = ["banana", "apple", "cherry"] Output: "apple\nbanana\ncherry" When printed: apple banana cherry
You already own the input vector, so bind it as mutable, sort it with
sort(), then combine the lines with the slice join("\n") method.
The full solution:
impl Solution {
fn sort_lines(lines: Vec<String>) -> String {
let mut sorted_lines = lines;
sorted_lines.sort();
return sorted_lines.join("\n");
}
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
