I'm doing Rust Koans and am stuck on a this question:
#[test]
fn for_loops_two() {
let words: [&'static str; 3] = ["I", "love", "Rust"];
let space: &str = " ";
let mut sentence: String = String::new();
for word in words.iter() {
// __
}
println!("{:?}", sentence);
assert!(sentence == "I love Rust".to_string());
}
I know I need to concatenate the string but this will fail:
#[test]
fn for_loops_two() {
let words: [&'static str; 3] = ["I", "love", "Rust"];
let mut sentence: String = String::new();
for word in words.iter() {
sentence.push_str(word);
}
println!("{:?}", sentence); // "ILoveRust"
assert!(sentence == "I love Rust".to_string());
}
I can add a space after each iteration:
#[test]
fn for_loops_two() {
let words: [&'static str; 3] = ["I", "love", "Rust"];
let space: &str = " ";
let mut sentence: String = String::new();
for word in words.iter() {
sentence.push_str(word);
sentence.push_str(space);
}
println!("{:?}", sentence); // "I Love Rust "
assert!(sentence == "I love Rust".to_string());
}
This will also fail because the final iteration will add a space.
I guess I could write a conditional if we are on the last iteration, but I'm struggling to get the syntax correct. Moreover, I feel like there is a much better solution for all of this and I just can't figure out syntax.
How can I make the assertion above pass with a conditional in the loop to not add the space on the last iteration?