How to push suffix to string?

fn main() {
    let inp_str = "alfa bravo charlie delta echo foxtrot golf hotel india juliett kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whiskey x-ray yankee zulu";
    let vowels = "aeiouy";

    for word in inp_str.split_whitespace() {
        let w_vec: Vec<char> = word.chars().collect();
        if vowels.contains(w_vec[0]) {
            let w = word.to_string().push_str("-hay");
        } else {
            //first -> irst-fay

result is:

    eyegor@twdevel:~/devel/rust/study/exercises/chapter1/exercise2$ cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/exercise2`

but i need to somesing like:


Can you describe what you actually want to achieve, please? Am I correct that you want to append -hay to each word that contains one of the letters in aeiouy?

push_str does what you want, but you are not using it correctly. It modifies the string in place, and returns unit. You will need something like:

let mut w = string();

Also, note you can be more efficient about memory if you use word.first() instead of creating a Vec<char> of the whole string.

yes, but not contains, word must start with that letter

its not same?

No, it modifies the string it is called on, but does not return anything.

thank You. I will check it in a few minutes

I mean, the two versions you just posted are identical, but they are not the same as the code I posted.

Yours creates a temporary string that never gets assigned anywhere, and pushes -hay to it, and then assigns the return value of that call, which is (), or the unit value, to the variable w. Mine creates a String, assigns it to w, and pushes -hay to it. So you still have access to the string.

Thank You. The solution is work :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.