How to move the first word to the end of a string in Rust

1 Answer

0 votes
fn move_first_word_to_end_of_string(s: &str) -> String {
    let mut parts: Vec<&str> = s.split_whitespace().collect();

    if parts.len() <= 1 {
        return s.to_string();
    }

    let first = parts.remove(0);
    parts.push(first);

    parts.join(" ")
}

fn main() {
    let s = "Would you like to know more? (Explore and learn)";
    let result = move_first_word_to_end_of_string(s);

    println!("{}", result);
}




/*
run:

you like to know more? (Explore and learn) Would

*/

 



answered Feb 5 by avibootz
edited Feb 5 by avibootz
...