Start learning free

LESSON 78 OF 293 · STRINGS - CHALLENGES

File Extension in Rust: split and Vec<&str>

Practice Read the full explanation

Given a file name fileName, return its extension: the part of the name after the last dot.

Split the name at every dot, and the extension is the last of the parts. A name can have more than one dot, like "archive.tar.gz": only what comes after the last dot is the extension. Keep its letters exactly as they are.

 

Example 1:

Input: fileName = "photo.jpeg"
Output: "jpeg"
Explanation: The name has one dot, and "jpeg" comes after it.

Example 2:

Input: fileName = "archive.tar.gz"
Output: "gz"
Explanation: Splitting at the dots gives "archive", "tar" and "gz". The last part, "gz", is the extension.

Example 3:

Input: fileName = "README.MD"
Output: "MD"
Explanation: The extension keeps its upper-case letters.

 

Constraints:

  • fileName has at least one dot, and doesn't start or end with a dot.
  • fileName has no spaces: only English letters, digits, dots, - and _.
STEP 1 OF 2
    Ctrl + Enter to check

    RUST · STRINGS - CHALLENGES

    File Extension in Rust, explained

    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

    1. Split into parts. Call split(".") on file_name and collect() the pieces. The type annotation Vec<&str> tells collect() what to build.
    2. Take the last piece. A vector is numbered from 0, so its last item is at index parts.len() - 1.
    3. Convert it to a String. The item is a &str, and to_string() makes the owned String that 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:

    • split is lazy. It returns an iterator, and nothing is gathered until collect() 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 of parts tells it.
    • Borrowed pieces. The items of parts are slices of file_name, so no text is copied until to_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() - 1 is safe.
    • Other ways. parts.last() returns an Option<&&str>, and file_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] without to_string() doesn't compile: the function promises a String and 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".
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free