rustbook

Toy programs and snippets for The Rust Programming Language book
Log | Files | Refs

main.rs (1012B)


      1 use std::io;
      2 
      3 fn is_vowel(c: &char) -> bool {
      4     match c {
      5         'a' => true,
      6         'e' => true,
      7         'i' => true,
      8         'o' => true,
      9         'u' => true,
     10         'A' => true,
     11         'E' => true,
     12         'I' => true,
     13         'O' => true,
     14         'U' => true,
     15         _ => false,
     16     }
     17 }
     18 
     19 fn main() {
     20 
     21     println!("Enter text to pig-latinize:");
     22     let mut text = String::new();
     23     io::stdin()
     24         .read_line(&mut text)
     25         .expect("Failed to read line");
     26     let text = text.trim();
     27     let mut p_text = String::new();
     28 
     29     for w in text.split_whitespace() {
     30         let mut pig_w;
     31         let first = w.chars().next().unwrap();
     32         if is_vowel(&first) {
     33             pig_w = w.to_string();
     34             pig_w.push_str("-tay");
     35         } else {
     36             pig_w = w[1..].to_string();
     37             pig_w.push('-');
     38             pig_w.push(first);
     39             pig_w.push_str("ay");
     40         }
     41         p_text.push_str(&pig_w[..]);
     42         p_text.push(' ');
     43     }
     44     println!("{}", p_text);
     45 }