Compress Repeated Characters with Run-Length Encoding

Encode repeated characters with counts and decode the result back, rejecting zero counts and dangling numeric prefixes.

RustData Integrity

Code

rustsnippet
56 lines
fn encode(input: &str) -> String {
    let mut output = String::new();
    let mut characters = input.chars().peekable();
    while let Some(current) = characters.next() {
        let mut count = 1usize;
        while characters.peek() == Some(&current) {
            characters.next();
            count += 1;
        }
        if count > 1 {
            output.push_str(&count.to_string());
        }
        output.push(current);
    }
    output
}

fn decode(input: &str) -> Result<String, String> {
    let mut output = String::new();
    let mut digits = String::new();
    for character in input.chars() {
        if character.is_ascii_digit() {
            digits.push(character);
            continue;
        }
        let count = if digits.is_empty() {
            1
        } else {
            digits
                .parse::<usize>()
                .map_err(|_| format!("invalid count: {digits}"))?
        };
        if count == 0 {
            return Err("count must be positive".to_string());
        }
        output.extend(std::iter::repeat(character).take(count));
        digits.clear();
    }
    if !digits.is_empty() {
        return Err("trailing count without a character".to_string());
    }
    Ok(output)
}

fn main() {
    for sample in ["aaabccdddd", "abc", "wwwwwwwwwwww"] {
        let encoded = encode(sample);
        let decoded = decode(&encoded).expect("valid encoding");
        println!("{sample} -> {encoded} -> {decoded}");
        assert_eq!(decoded, sample);
    }
    assert_eq!(encode("aaabccdddd"), "3ab2c4d");
    assert!(decode("0a").is_err());
    assert!(decode("12").is_err());
}

What the code does

Compress Repeated Characters with Run-Length Encoding reduces to the functions encode, decode and main.

A single loop drives the file — while let Some(current) = characters.next(). Fallible operations return Result, forcing the caller to handle both arms explicitly.

What to watch for

  • Errors travel through Result; if you drop the ? operators while adapting, each call site needs its own match.

Applying it

The main signature reads fn encode(input: &str) -> String

Start the program and main runs encode and decode one after another.

Statements that matter

return Err("trailing count without a character".to_string());
return Err("count must be positive".to_string());
while let Some(current) = characters.next() {
let mut count = 1usize;
while characters.peek() == Some(&current) {
characters.next();
count += 1;
}
if count > 1 {
output.push_str(&count.to_string());
}
output.push(current);
}

Interactive Tools for This Topic

FAQ

Is there exception handling?
No panics — `Result` values carry the failure.
How do you run Compress Repeated Characters with Run-Length Encoding?
Just run the file; everything it needs ships with the runtime itself.