โ† run

rs-01-rle

1.000
3/3 testsยท basic
Challenge ยท difficulty 1/5
# Run-length encoding

Implement the library file **`src/lib.rs`** exposing:

```rust
pub fn run_length_encode(input: &str) -> String
```

Encode consecutive runs of the same character as `<char><count>`. A single character still
gets a count of `1`.

- `run_length_encode("aaabbc")` โ†’ `"a3b2c1"`
- `run_length_encode("")` โ†’ `""`
- `run_length_encode("abc")` โ†’ `"a1b1c1"`

Tests live in `tests/` and call it as `challenge::run_length_encode`.
tests/rle.rs
use challenge::run_length_encode;

#[test]
fn basic_runs() {
    assert_eq!(run_length_encode("aaabbc"), "a3b2c1");
}

#[test]
fn empty() {
    assert_eq!(run_length_encode(""), "");
}

#[test]
fn all_singles() {
    assert_eq!(run_length_encode("abc"), "a1b1c1");
}
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```rust
pub fn run_length_encode(input: &str) -> String {
    if input.is_empty() {
        return String::new();
    }

    let mut result = String::new();
    let mut chars = input.chars();

    if let Some(mut current_char) = chars.next() {
        let mut count = 1;

        for next_char in chars {
            if next_char == current_char {
                count += 1;
            } else {
                result.push(current_char);
                result.push_str(&count.to_string());
                current_char = next_char;
                count = 1;
            }
        }

        // Append the last run
        result.push(current_char);
        result.push_str(&count.to_string());
    }

    result
}
```

Test output (stdout)


running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 3 tests
...
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Reasoning (chain-of-thought)
The user wants me to implement a run-length encoding function in Rust. The function should:

1. Take a string slice as input
2. Return a String where consecutive runs of the same character are encoded as `<char><count>`
3. Even single characters get a count of 1

Examples:
- "aaabbc" โ†’ "a3b2c1"
- "" โ†’ ""
- "abc" โ†’ "a1b1c1"

I need to create `src/lib.rs` with the function `pub fn run_length_encode(input: &str) -> String`.

Let me think about the algorithm:
1. If input is empty, return empty string
2. Iterate through characters, tracking the current character and its count
3. When we encounter a different character, append the previous character and its count to the result
4. Don't forget to append the last run after the loop

Here's my implementation:

```rust
pub fn run_length_encode(input: &str) -> String {
    if input.is_empty() {
        return String::new();
    }

    let mut result = String::new();
    let mut chars = input.chars();
    
    if let Some(mut current_char) = chars.next() {
        let mut count = 1;
        
        for next_char in chars {
            if next_char == current_char {
                count += 1;
            } else {
                result.push(current_char);
                result.push_str(&count.to_string());
                current_char = next_char;
                count = 1;
            }
        }
        
        // Append the last run
        result.push(current_char);
        result.push_str(&count.to_string());
    }
    
    result
}
```

This should handle all the cases correctly. Let me verify:
- "aaabbc": a(3), b(2), c(1) โ†’ "a3b2c1" โœ“
- "": returns "" โœ“
- "abc": a(1), b(1), c(1) โ†’ "a1b1c1" โœ“

This looks correct.