DEV Community

loading...

Discussion on: Daily Challenge #259 - Duplicate Encoder

Collapse
gnunicorn profile image
Benjamin Kampmann

In Rust – playground link (with tests)

pub fn dub_enc(inp: String) -> String {
    let lowered = inp.to_lowercase();
    lowered
        .chars()
        .map(|c| {
            let mut finder = lowered.chars().filter(|r| *r == c);
            finder.next(); // exists
            if finder.next().is_some() { // found at least once more
                ")"
            } else {
                "("
            }
        })
        .collect()
} 
Collapse
lexlohr profile image
Alex Lohr

Why not use std::str::matches to iterate over the same chars?

Collapse
gnunicorn profile image
Benjamin Kampmann

I suppose that is a valid alternative. Just not the first thing that came to mind ...

Thread Thread
lexlohr profile image
Alex Lohr

I just love how Rust has a helpful method for about everything in its std/core libs.