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
- Split the name into
words.full_name.split(" ")followed bycollect(), with the typeVec<&str>tellingcollect()what to build. - Take the first letter of the first word with
words[0][0..1]and make it aStringwithto_string(). - Append the first letter of the second word with
+ &words[1][0..1]. - 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 aStringon the left and a&stron 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..1is 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_uppercasereturns a newString. Strings are not changed in place. The same method exists onchar, 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 slice0..1is the whole word. - More than one space.
"grace hopper"with two spaces would split into["grace", "", "hopper"], andwords[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".
