Sha256: f08575948d24b2c7c615836202e54ea68d4ae2a2a8731d60f326aa0b7f94050f

Contents?: true

Size: 891 Bytes

Versions: 180

Compression:

Stored size: 891 Bytes

Contents

#[macro_use] extern crate lazy_static;
extern crate regex;

use regex::Regex;

// Regular expressions from Python version of exercism

pub fn translate_word(word: &str) -> String {
    // Prevent creation and compilation at every call.
    // These are compiled exactly once
    lazy_static! {
        // Detects if it starts with a vowel
        static ref VOWEL: Regex = Regex::new(r"^([aeiou]|y[^aeiou]|xr)[a-z]*").unwrap();
        // Detects splits for initial consonants
        static ref CONSONANTS: Regex = Regex::new(r"^([^aeiou]?qu|[^aeiou]+)([a-z]*)").unwrap();
    }

    if VOWEL.is_match(word) {
        String::from(word) + "ay"
    } else {
        let caps = CONSONANTS.captures(word).unwrap();
        String::from(&caps[2]) + &caps[1] + "ay"
    }
}

pub fn translate(text: &str) -> String {
    text.split(" ").map(|w| translate_word(w)).collect::<Vec<_>>().join(" ")
}

Version data entries

180 entries across 180 versions & 1 rubygems

Version Path
trackler-2.2.1.179 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.178 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.177 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.176 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.175 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.174 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.173 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.172 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.171 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.170 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.169 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.167 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.166 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.165 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.164 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.163 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.162 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.161 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.160 tracks/rust/exercises/pig-latin/example.rs
trackler-2.2.1.159 tracks/rust/exercises/pig-latin/example.rs