Start learning free

LESSON 76 OF 293 · STRINGS - CHALLENGES

Repeat Last Char in Rust: Slicing and str::repeat

Practice Read the full explanation

Given a string s and a number n, return a new string made of the last character of s, written n times in a row.

 

Example 1:

Input: s = "banana", n = 3
Output: "aaa"
Explanation: The last character of "banana" is "a". Written 3 times, it gives "aaa".

Example 2:

Input: s = "Wow!", n = 2
Output: "!!"
Explanation: The last character can be any character: here it is the exclamation mark.

Example 3:

Input: s = "z", n = 1
Output: "z"
Explanation: A string of one character: its last character is "z", written once.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
  • n is a whole number from 1 to 10.
STEP 1 OF 2
    Ctrl + Enter to check

    RUST · STRINGS - CHALLENGES

    Repeat Last Char in Rust, explained

    Problem overview

    The function repeat_last_char takes a string s and a number n, and returns a new String made of the last character of s written n times in a row. The string has at least one character, and n is between 1 and 10.

    For "banana" and n = 3 the last character is "a", so the result is "aaa". For "Wow!" and n = 2 it is "!!": the last character can be any character. For "z" and n = 1 the string has one character, which is also its last one, so the result is "z".

    Approach

    There are two jobs: get the last character, then repeat it. Rust can't index a string with a single number, so the last character comes from a slice that starts at s.len() - 1 and runs to the end. Repeating it needs no loop, because string slices have a repeat method: "ab".repeat(3) returns the new String "ababab".

    The only subtlety is the type of the count. repeat expects a usize, the type Rust uses for lengths and counts, while the function receives n as an i32. Rust never converts between number types silently, so the call needs n as usize.

    Step by step

    1. Store the last character in last. Borrow the slice of s from s.len() - 1 to the end. Leaving out the end of the range means "up to the end of the string", which here is a single character.
    2. Repeat it n times. Call repeat on last, passing n as usize, and return the resulting String.

    The solution in Rust

    impl Solution {
        fn repeat_last_char(s: String, n: i32) -> String {
            let last = &s[s.len() - 1..];
            return last.repeat(n as usize);
        }
    }
    

    Some details worth knowing:

    • last is a &str. It borrows from s and copies nothing. repeat is the call that allocates a new String.
    • as converts numbers. n as usize turns the i32 into a usize. For a value from 1 to 10 the conversion is exact. A negative i32 would not be: as doesn't check the sign, so the value would become a huge usize and the call would fail. The lesson keeps n positive.
    • A count of 0. "a".repeat(0) is the empty string, so n = 0 would be fine if the lesson allowed it.
    • Bytes, not characters. The slice starts at a byte position. For the English letters, digits and punctuation here, that is the same as a character position. If the last character took several bytes, such as é, starting one byte before the end would cut it in half and panic.

    Edge cases and common mistakes

    • One-character strings. For "z", s.len() - 1 is 0, so the slice is the whole string. This works because the string isn't empty.
    • Taking the first character instead of the last. Using &s[0..1] gives "bbb" for "banana".
    • Starting at s.len(). The slice s.len().. is empty, so the result would be an empty string.
    • Passing n directly. last.repeat(n) doesn't compile, because n is an i32 where a usize is required.
    • Empty strings. The lesson guarantees at least one character. For an empty string, s.len() - 1 would subtract from zero in a usize.

    Testing

    • "banana", 3 gives "aaa".
    • "Wow!", 2 gives "!!".
    • "z", 1 gives "z".
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free