numeronym/
lib.rs

1pub fn numeronym(word: &str) -> String {
2    if word.chars().count() < 2 {
3        String::from(word)
4    } else {
5        let len = word.chars().count() - 2;
6        let (_, first) = word.char_indices().nth(0).unwrap_or((0, ' '));
7        let (_, last) = word.char_indices().last().unwrap_or((0, ' '));
8        format!("{}{}{}", first, len, last)
9    }
10}
11
12#[cfg(test)]
13mod test {
14    use super::numeronym;
15
16    #[test]
17    fn kubernetes() {
18        assert_eq!("k8s", &numeronym("kubernetes")[..]);
19    }
20
21    #[test]
22    fn おはようございます() {
23        assert_eq!("お7す", &numeronym("おはようございます")[..]);
24    }
25
26    #[test]
27    fn zero() {
28        assert_eq!("h0i", &numeronym("hi")[..]);
29    }
30}