ordered_words.rs 968 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use std::env;
  2. use std::char;
  3. use std::fs::File;
  4. use std::io::BufReader;
  5. use std::io::BufRead;
  6. fn main() {
  7. if env::args().len() != 2 {
  8. println!("Usage: {} <path to words file>", env::args().nth(0).unwrap());
  9. return;
  10. }
  11. let filename = env::args().nth(1).unwrap();
  12. let f = File::open(filename).ok().expect("failed open words file");
  13. let reader = BufReader::new(&f);
  14. let mut longest_ordered = String::new();
  15. 'lines: for line in reader.lines() {
  16. let l = line.ok().expect("failed to read from words file");
  17. if longest_ordered.len() >= l.len() {
  18. continue;
  19. }
  20. let mut last_char: char = char::from_u32(0).unwrap();
  21. for c in l.chars() {
  22. if last_char <= c {
  23. last_char = c;
  24. } else {
  25. continue 'lines;
  26. }
  27. }
  28. longest_ordered = l;
  29. }
  30. println!("Longest ordered string: {}", longest_ordered);
  31. }