Start learning free

LESSON 79 OF 293 · STRINGS - CHALLENGES

Initials in Rust: split, Slices and to_uppercase

Practice Read the full explanation

Given a person's full name fullName, made of a first name and a last name, return their initials: the first letter of each name, in upper case.

The two names are separated by a single space. The initials are always upper case, even when the name is written in lower case.

 

Example 1:

Input: fullName = "grace hopper"
Output: "GH"
Explanation: The first name starts with g and the last name with h. In upper case, they give "GH".

Example 2:

Input: fullName = "Ada Lovelace"
Output: "AL"
Explanation: A and L are already upper case, so they stay as they are.

Example 3:

Input: fullName = "a b"
Output: "AB"
Explanation: A name can be a single letter: that letter is its initial.

 

Constraints:

  • fullName has exactly two words, the first name and the last name, separated by one space.
  • Both names are made of English letters only, lower case or upper case.
STEP 1 OF 2
    Ctrl + Enter to check

    RUST · STRINGS - CHALLENGES

    Initials in Rust, explained

    Problem overview

    The function initials takes a person's full name, full_name, made of a first name and a last name separated by one space. It returns the first letter of each name, in upper case, as a String. Both names are English letters only, in lower or upper case.

    For "grace hopper" the result is "GH": the initials are upper case even though the name is written in lower case. For "Ada Lovelace" it is "AL", whose letters were already capitals. For "a b" it is "AB", because a name can be a single letter.

    Approach

    There are three small jobs: separate the two names, take one letter from each, and turn the letters into upper case. In Rust, split(" ") cuts a string at every space, and collect() gathers the pieces into a Vec<&str>. With exactly one space in the name, the vector holds exactly two words.

    A word can't be indexed with a single number, but a range works: words[0][0..1] first picks the word with [0], then slices its first letter with [0..1]. The result is a str, and to join two of them with + the left side must be an owned String. That is why the first letter goes through to_string(), while the second is only borrowed with &: + appends a &str to a String.

    Finally, to_uppercase() returns a new String with every letter in upper case. It has to be applied to the joined letters, so the sum goes in parentheses first.

    Step by step

    1. Split the name into words. full_name.split(" ") followed by collect(), with the type Vec<&str> telling collect() what to build.
    2. Take the first letter of the first word with words[0][0..1] and make it a String with to_string().
    3. Append the first letter of the second word with + &words[1][0..1].
    4. Put the sum in parentheses and call to_uppercase() on it, then return the result.

    The solution in Rust

    impl Solution {
        fn initials(full_name: String) -> String {
            let words: Vec<&str> = full_name.split(" ").collect();
            return (words[0][0..1].to_string() + &words[1][0..1]).to_uppercase();
        }
    }
    

    Some details worth knowing:

    • The parentheses matter. A method call binds tighter than +. Without them, to_uppercase() would apply only to the second letter: for "grace hopper" the result would be "gH".
    • Why to_string() on the left only. The + operator takes a String on the left and a &str on the right. The left value is consumed and reused as the result, so no extra copy of it is needed. The right value is only read, so a borrow is enough.
    • Slices count bytes. 0..1 is the first byte, which is a whole letter for English text. A first letter that takes several bytes, like é, would make the slice panic.
    • to_uppercase returns a new String. Strings are not changed in place. The same method exists on char, where it returns an iterator, because one character can have a longer upper-case form.

    Edge cases and common mistakes

    • Already upper case. For "Ada Lovelace", to_uppercase() changes nothing, and the result is "AL".
    • Single-letter names. "a b" splits into ["a", "b"], and each slice 0..1 is the whole word.
    • More than one space. "grace hopper" with two spaces would split into ["grace", "", "hopper"], and words[1][0..1] would panic on the empty piece. The lesson guarantees a single space; split_whitespace() is the method that skips extra spaces.
    • Forgetting the borrow. + words[1][0..1] doesn't compile: the right side must be a &str, so it needs the &.
    • Missing parentheses. This gives the wrong case without any error, so it is worth testing a lower-case name.

    Testing

    • "grace hopper" gives "GH".
    • "Ada Lovelace" gives "AL".
    • "a b" gives "AB".
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free