rs-01-rle
1.000
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();
let mut first = chars.next().unwrap();
let mut count = 1;
for c in chars {
if c == first {
count += 1;
} else {
result.push(first);
result.push_str(&count.to_string());
first = c;
count = 1;
}
}
result.push(first);
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 task is to implement a run-length encoding function in Rust. The function should:
1. Take a string slice as input
2. Return a String with consecutive runs of the same character encoded as `<char><count>`
3. Even single characters get a count of 1
Let me write the 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(first) = chars.next() {
let mut count = 1;
for c in chars {
if c == first {
count += 1;
} else {
result.push(first);
result.push_str(&count.to_string());
first = c;
count = 1;
}
}
// Don't forget the last run
result.push(first);
result.push_str(&count.to_string());
}
result
}
```
Wait, there's an issue with this approach. When I do `first = c`, I'm reassigning to a variable, but I need to make sure the logic is correct. Let me trace through:
For "aaabbc":
- first = 'a', count = 1
- c = 'a', count = 2
- c = 'a', count = 3
- c = 'b', push 'a' and "3", first = 'b', count = 1
- c = 'b', count = 2
- c = 'c', push 'b' and "2", first = 'c', count = 1
- End of loop, push 'c' and "1"
Result: "a3b2c1" โ
For "abc":
- first = 'a', count = 1
- c = 'b', push 'a' and "1", first = 'b', count = 1
- c = 'c', push 'b' and "1", first = 'c', count = 1
- End of loop, push 'c' and "1"
Result: "a1b1c1" โ
For "":
- Returns empty string โ
This looks correct! Let me also consider edge cases like single character input.
For "a":
- first = 'a', count = 1
- Loop doesn't execute
- Push 'a' and "1"
Result: "a1" โ
I'll refine the implementation to handle all these cases cleanly.