DEV Community

Discussion on: Daily Challenge #188 - Break camelCase

Collapse
 
pmkroeker profile image
Peter • Edited

In rust!

pub fn ccbreaker (value: &str) -> String {
    value.chars()
        .into_iter()
        .map(|c| 
            if c.is_uppercase() {
                format!(" {}", c.to_lowercase())
            } else { 
                c.to_string()
            })
        .collect::<String>()
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn example_1() {
        assert_eq!(ccbreaker("camelCasing"), "camel casing".to_string());
    }
    #[test]
    fn example_2() {
        assert_eq!(ccbreaker("garbageTruck"), "garbage truck".to_string());
    }
    #[test]
    fn test_1() {
        assert_eq!(ccbreaker("policeSiren"), "police siren".to_string());
    }
    #[test]
    fn test_2() {
        assert_eq!(ccbreaker("camelCasingTest"), "camel casing test".to_string());
    }
}

Rust Playground
GitHub Gist