-1

I have the following code:

pub mod str {
    // http://stackoverflow.com/questions/27996430/reversing-a-string-in-rustlang-1-0-0/27996791#27996791
    pub fn reverse() -> String {
    let wordy: &str = "lowks";
    let wordy_reverse: String = wordy
        .graphemes(true)
        .rev()
        // .flat_map(|g| g.chars())
        .collect();
        // for word in wordy.words().rev() {
            //      print!("{}", word);
        // }
    wordy_reverse
    }

    pub fn sort() {
    let wordy: &'static str = "I am a hello world example"; //'
        let mut chars: Vec<char> = wordy.chars().collect();     
        chars.sort();
        for char in chars.iter() {
            println!("{}", char);
        }   
    }

    pub fn replace() -> String {
        let string_to_replace = String::from_str("Hello World!");
        let new_string: String = string_to_replace.replace("Hello", "Goodbye");
        format!("{} -> {}", string_to_replace, new_string)

    }
}

#[test]

fn test_replace() {
    assert_eq!("Hello World! -> Goodbye World!", str::replace());
    let reverse: String = str::reverse();
    assert_eq!("skwol", &*reverse);
}

I can't figure out how to change the sort function to expose it so that I can add test for it like the other two. Can someone please help ?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Muhammad Lukman Low
  • 8,177
  • 11
  • 44
  • 54
  • You have a lot of redundant type annotations. I think that they are useful while learning, but eventually you want to remove them to reduce visual clutter, keeping them only where needed. For example, your `reverse` method could [look much shorter](http://is.gd/I815RY). – Shepmaster Jan 18 '15 at 15:18

1 Answers1

2

Just make sort return the Vec<char>:

pub fn sort() -> Vec<char> {
    let wordy: &'static str = "I am a hello world example";
    let mut chars: Vec<char> = wordy.chars().collect();     
    chars.sort();
    chars   
}
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155