Problem overview
The real cat takes several files and prints them one after another. Version 4 models that with concat_cat(all_lines: Vec<Vec<String>>) -> String. Each inner vector is one "file" of lines. The function flattens all lines in order and joins them with newlines.
For [["hello", "world"], ["foo", "bar", "baz"]] the result is "hello\nworld\nfoo\nbar\nbaz". A file can be empty and adds no lines. If there are no files at all, the result is the empty string.
Approach: flatten first, then join
Earlier versions built a string while looping and used i > 0 to decide where newlines go. Here the lines come from nested loops, so there is no single position to test. A simpler plan is to split the work in two. First, collect every line into one flat Vec<String>. Second, join that vector with "\n". The standard library's join puts the separator between elements only, which is the "between, not after" rule the earlier loops implemented by hand.
Step by step
- The flat list.
let mut flat: Vec<String> = Vec::new();starts empty. The type annotation is written out, though Rust could also infer it from the laterpush. - Outer loop over the files.
for file_lines in all_lines. Eachfile_linesis aVec<String>. - Inner loop over its lines.
for line in file_lines. An empty file never enters this loop, so it adds nothing, with no special case. - Collect.
flat.push(line);adds the line at the end, keeping the order: files in order, lines in order within each file. - Join. After both loops,
flat.join("\n")is the result. With no semicolon, it is what the function returns.
The solution in Rust
impl Solution {
fn concat_cat(all_lines: Vec<Vec<String>>) -> String {
let mut flat: Vec<String> = Vec::new();
for file_lines in all_lines {
for line in file_lines {
flat.push(line);
}
}
flat.join("\n")
}
}
Ownership is the Rust detail here. Looping with for file_lines in all_lines consumes the outer vector: each inner vector is moved into file_lines, and each string is moved out of it into line and then into flat. No strings are copied, and all_lines can't be used after the loop, which is fine because the function doesn't need it again. The final join is the one place where the text is copied into a single new String.
Joining a Vec<String> with a &str separator returns a String, matching the declared return type. Each line is touched once, so the time is proportional to the total size of the input, and flat holds the same lines again, so the extra space is proportional to the number of lines.
Edge cases and common mistakes
- Empty files in the middle.
[["a"], [], ["b"]]gives"a\nb": no blank line appears for the empty file. - Blank lines are real lines.
[["a", ""], ["", "b"]]gives"a\n\n\nb", because two empty strings sit betweenaandband they produce three newlines. - All files empty, or no files.
[[], []]and[]both give"". - Joining each file separately. Calling
joinper file and gluing the results would need extra care at the file boundaries, and an empty file would leave a stray separator. Flattening first avoids both. - Using a vector after moving it. Code that tries to read
all_linesafter the loop is a compile error, since the loop took ownership.
Testing
[["hello", "world"], ["foo", "bar", "baz"]] -> "hello\nworld\nfoo\nbar\nbaz"
[["a"], [], ["b"]] -> "a\nb"
[["a", ""], ["", "b"]] -> "a\n\n\nb"
[[], []] -> ""
[] -> ""
