Skip to content

Instantly share code, notes, and snippets.

@sassman
Created September 8, 2019 22:28
Show Gist options
  • Save sassman/30c0afc5865529a5e9c7a1c0a6aea1ac to your computer and use it in GitHub Desktop.
Save sassman/30c0afc5865529a5e9c7a1c0a6aea1ac to your computer and use it in GitHub Desktop.
rust tdd: src/lib.rs
// src/lib.rs
pub fn alphabet_position(s: &str) -> String {
s.chars() // <-- get an iterator over all chars
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 }) // <-- substract the ascii value of 'a'
.map(|x| -> String { x.to_string() }) // <-- convert the char to a String
.collect::<Vec<String>>() // <-- collect a Vector of String
.join(" ") // <-- join the Strings by whitespace
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment