Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment