Skip to content

Instantly share code, notes, and snippets.

@cuevasclemente
Last active August 29, 2015 14:08
Show Gist options
  • Save cuevasclemente/91f7dc1bd784502aa4fd to your computer and use it in GitHub Desktop.
Save cuevasclemente/91f7dc1bd784502aa4fd to your computer and use it in GitHub Desktop.
Simple reverse words commandline program
use std::io;
fn main() {
// Readline, make sure that the line is not bad, raise error otherwise
let input = io::stdin().read_line().ok().expect("Failed to read line");
// Take the input as a string slice, rather than a String
let input_slice: &str = input.as_slice(); 4
// Get words in a vector, call reverse on it, returning an iterator,
// then collect the objects in the iterator into another string slice
let reversed_input_it: Vec<&str> = input_slice.words().rev().collect();
// Connect the string elements of the iterator (this function took the longest to find)
let reversed_string = reversed_input_it.connect(" ");
//Print that ish
println!("{}", reversed_string);
// main returns void
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment