Start learning free

LESSON 77 OF 293 · STRINGS - CHALLENGES

Burrrrp in Rust: Building a String With format! and repeat

Practice Read the full explanation

Given a number n, return the sound of a burp with n letters r: "bu", then n letters r, then "p".

 

Example 1:

Input: n = 4
Output: "burrrrp"
Explanation: "bu", then 4 letters r, then "p". That's the burp in this lesson's title.

Example 2:

Input: n = 1
Output: "burp"
Explanation: The shortest burp has a single r.

Example 3:

Input: n = 7
Output: "burrrrrrrp"
Explanation: "bu", then 7 letters r, then "p".

 

Constraints:

  • n is a whole number from 1 to 20.
  • The result is all lowercase.
STEP 1 OF 1
    Ctrl + Enter to check

    RUST · STRINGS - CHALLENGES

    Burrrrp in Rust, explained

    Problem overview

    The function burrrrp takes a number n and returns the sound of a burp: "bu", then n letters r, then "p". The number is between 1 and 20, and the result is all lowercase.

    For n = 4 the result is "burrrrp". For n = 1 it is "burp", the shortest burp. For n = 7 it is "burrrrrrrp".

    Approach

    Only one part of the answer changes with the input: the run of r letters. The start, "bu", and the end, "p", are always the same. That suggests a template with a hole in the middle: format!("bu{}p", ...). The hole is filled with "r" repeated n times.

    The repeat method of a string slice does exactly that: "r".repeat(4) gives the new String "rrrr". It takes its count as a usize, while the function receives n as an i32, so the count is converted with n as usize.

    The middle part grows with n while the rest stays put:

    Another way to get the same result is a loop that pushes one r at a time onto a growing String. It works, but it needs a mutable variable, a loop and a counter, while repeat expresses "this many copies" directly. Reaching for a method that already does the job keeps the function to one line.

    It also helps to see what {} does. Each {} in a format! template is replaced by the text form of the matching argument, in order. With one {} and one argument there is nothing to line up: the string produced by repeat is dropped into the middle of bu and p.

    n"r".repeat(n as usize)Result
    1rburp
    2rrburrp
    4rrrrburrrrp
    10rrrrrrrrrrburrrrrrrrrrp

    Step by step

    1. Build the middle. Write "r".repeat(n as usize). For n = 4 it is "rrrr".
    2. Put it in the template. "bu{}p" has the fixed start, a {} for the middle and the fixed end.
    3. Return the formatted string. format! works like println!, but returns a String instead of printing.

    The solution in Rust

    impl Solution {
        fn burrrrp(n: i32) -> String {
            return format!("bu{}p", "r".repeat(n as usize));
        }
    }
    

    Some details worth knowing:

    • Letters, not words. The template text bu and p sits right next to the {}, with no spaces. Any space inside the quotes would end up in the result.
    • as usize. Rust won't pass an i32 where a usize is expected, so "r".repeat(n) doesn't compile. The cast is exact for 1 to 20. A negative n would become a huge usize, because as doesn't check the sign.
    • An expression inside the arguments. The arguments of format! can be any expressions, so the call to repeat goes straight in. The String it returns is a temporary that lives until the formatting is done.

    Edge cases and common mistakes

    • Off-by-one in the count. Writing (n + 1) or (n - 1) gives one r too many or too few. For n = 1, the correct middle is a single r.
    • Forgetting the cast. The compiler rejects the call and says it expected a usize but found an i32.
    • Using the character 'r'. Single quotes make a char, which has no repeat method. The method belongs to strings, so use "r".
    • Stray spaces. "bu {} p" would put spaces in the burp.

    Testing

    • n = 4 gives "burrrrp".
    • n = 1 gives "burp".
    • n = 7 gives "burrrrrrrp".
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free