← run

rs-01-rle

0.000
0/1 tests· basic

Proposed solution

```rust
// src/lib.rs

pub fn run_length_encode(input: &str) -> String {
    let mut encoded = String::new();
    let mut count = 1;

    for (i, current_char) in input.chars().enumerate().skip(1) {
        if current_char == input.chars().nth(i) {
            count += 1;
        } else {
            encoded.push_str(&format!("{}{}", input.chars().nth(i - 1).unwrap(), count));
            count = 1;
        }
    }

    encoded.push_str(&format!("{}{}", input.chars().last().unwrap(), count));

    encoded
}
```

Errors (stderr)

error[E0308]: mismatched types
 --> src/lib.rs:8:28
  |
8 |         if current_char == input.chars().nth(i) {
  |            ------------    ^^^^^^^^^^^^^^^^^^^^ expected `char`, found `Option<char>`
  |            |
  |            expected because this is `char`
  |
  = note: expected type `char`
             found enum `Option<char>`
help: consider using `Option::expect` to unwrap the `Option<char>` value, panicking if the value is an `Option::None`
  |
8 |         if current_char == input.chars().nth(i).expect("REASON") {
  |                                                +++++++++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `challenge` (lib) due to 1 previous error
error: could not compile `challenge` (lib test) due to 1 previous error
Self-repair attempts
[
  {
    "total": 1,
    "answer": "```rust\n// src/lib.rs\n\npub fn run_length_encode(input: &str) -> String {\n    let mut encoded = String::new();\n    let mut count = 1;\n\n    for (i, current_char) in input.chars().enumerate().skip(1) {\n        if current_char == input.chars().nth(i) {\n            count += 1;\n        } else {\n            encoded.push_str(&format!(\"{}{}\", input.chars().nth(i - 1).unwrap(), count));\n            count = 1;\n        }\n    }\n\n    encoded.push_str(&format!(\"{}{}\", input.chars().last().unwrap(), count));\n\n    encoded\n}\n```",
    "passed": 0,
    "test_error": "error[E0308]: mismatched types\n --> src/lib.rs:8:28\n  |\n8 |         if current_char == input.chars().nth(i) {\n  |            ------------    ^^^^^^^^^^^^^^^^^^^^ expected `char`, found `Option<char>`\n  |            |\n  |            expected because this is `char`\n  |\n  = note: expected type `char`\n             found enum `Option<char>`\nhelp: consider using `Option::expect` to unwrap the `Option<char>` value, panicking if the value is an `Option::None`\n  |\n8 |         if current_char == input.chars().nth(i).expect(\"REASON\") {\n  |                                                +++++++++++++++++\n\nFor more information about this error, try `rustc --explain E0308`.\nerror: could not compile `challenge` (lib) due to 1 previous error\nerror: could not compile `challenge` (lib test) due to 1 previous error"
  },
  {
    "total": 1,
    "answer": "```rust\n// src/lib.rs\n\npub fn run_length_encode(input: &str) -> String {\n    let mut encoded = String::new();\n    let mut count = 1;\n\n    for (i, current_char) in input.chars().enumerate().skip(1) {\n        if let Some(&next_char) = input.chars().nth(i) {\n            if current_char == next_char {\n                count += 1;\n            } else {\n                encoded.push_str(&format!(\"{}{}\", current_char, count));\n                count = 1;\n            }\n        }\n    }\n\n    encoded.push_str(&format!(\"{}{}\", input.chars().last().unwrap(), count));\n\n    encoded\n}\n```",
    "passed": 0,
    "test_error": "error[E0308]: mismatched types\n --> src/lib.rs:8:21\n  |\n8 |         if let Some(&next_char) = input.chars().nth(i) {\n  |                     ^^^^^^^^^^    -------------------- this expression has type `Option<char>`\n  |                     |\n  |                     expected `char`, found `&_`\n  |\n  = note:   expected type `char`\n          found reference `&_`\nhelp: consider removing `&` from the pattern\n  |\n8 -         if let Some(&next_char) = input.chars().nth(i) {\n8 +         if let Some(next_char) = input.chars().nth(i) {\n  |\n\nFor more information about this error, try `rustc --explain E0308`.\nerror: could not compile `challenge` (lib) due to 1 previous error\nerror: could not compile `challenge` (lib test) due to 1 previous error"
  },
  {
    "total": 3,
    "answer": "```rust\n// src/lib.rs\n\npub fn run_length_encode(input: &str) -> String {\n    let mut encoded = String::new();\n    let mut count = 1;\n\n    for (i, current_char) in input.chars().enumerate().skip(1) {\n        if let Some(next_char) = input.chars().nth(i) {\n            if current_char == next_char {\n                count += 1;\n            } else {\n                encoded.push_str(&format!(\"{}{}\", current_char, count));\n                count = 1;\n            }\n        }\n    }\n\n    encoded.push_str(&format!(\"{}{}\", input.chars().last().unwrap(), count));\n\n    encoded\n}\n```",
    "passed": 0,
    "test_error": ""
  }
]
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.