Problem overview
The function file_extension takes a file name, file_name, and returns its extension: the part after the last dot, with its letters exactly as written. The name has at least one dot, doesn't start or end with a dot, and contains no spaces.
For "photo.jpeg" the result is "jpeg". For "archive.tar.gz" it is "gz": the name has two dots, and only what comes after the last one counts. For "README.MD" it is "MD", with its capitals.
Approach
If you cut the name at every dot, the extension is the last of the pieces, however many dots there are. "archive.tar.gz" splits into "archive", "tar" and "gz", and the last piece is the answer.
In Rust, split(".") cuts a string at every dot and gives the pieces one by one, as an iterator. collect() gathers them into a vector. The pieces are slices that borrow from the original name, so the vector has the type Vec<&str>. The function must return a String, so the last piece is turned into an owned string at the end.
Step by step
- Split into
parts. Callsplit(".")onfile_nameandcollect()the pieces. The type annotationVec<&str>tellscollect()what to build. - Take the last piece. A vector is numbered from 0, so its last item is at index
parts.len() - 1. - Convert it to a
String. The item is a&str, andto_string()makes the ownedStringthat the function returns.
The solution in Rust
impl Solution {
fn file_extension(file_name: String) -> String {
let parts: Vec<&str> = file_name.split(".").collect();
return parts[parts.len() - 1].to_string();
}
}
Some details worth knowing:
splitis lazy. It returns an iterator, and nothing is gathered untilcollect()runs. For"archive.tar.gz"the vector holds["archive", "tar", "gz"], with a length of 3.- The annotation is needed.
collect()can build many kinds of collections, so Rust needs to know which one you want. Here the type ofpartstells it. - Borrowed pieces. The items of
partsare slices offile_name, so no text is copied untilto_string(). The vector can't outlive the name it borrows from, which is no problem inside this function. - Indexing panics when out of range. The vector always has at least one item, because splitting any string gives at least one piece, so
parts.len() - 1is safe. - Other ways.
parts.last()returns anOption<&&str>, andfile_name.rsplit(".").next()returns the last piece without building a vector. The lesson's version spells out each step.
Edge cases and common mistakes
- Several dots. Taking the piece at index 1 works for
"photo.jpeg"but returns"tar"for"archive.tar.gz". The last piece is the right one. - Changing the case. The extension is returned as it is, so
"README.MD"gives"MD". - Forgetting the conversion. Returning
parts[parts.len() - 1]withoutto_string()doesn't compile: the function promises aStringand the item is a&str. - A name without a dot. The lesson rules it out. If it happened, the split would give one piece, the whole name, and the function would return it.
- Dots at the ends. The lesson also excludes them. A trailing dot would make the last piece an empty string.
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD".
