Newbie: Why the commented line stops the snippet from compiling?

A newbie here.

Here is the code I'm talking about.

It works with the code on line 15, but if I comment that and uncomment the code on line 14, it stops working.
Why doesn't it work with .push_str, and how to make it work with it?

push_str mutates string in-place, it doesn't return the result. So you have to return the resulting string explicitly:

fn pluralize(s: String) -> String {
    s.push_str("s");
    s
}

For future reference, it's much better to copy-paste the code and format it according to the pinned topic and not post it as image, so that we can copy it and test locally or in the playground.

Thanks for answer.
It still does not work. Shows the following error.

error[E0596]: cannot borrow `s` as mutable, as it is not declared as mutable
  --> main.rs:10:5
   |
9  | fn pluralize(s: String) -> String {
   |              - help: consider changing this to be mutable: `mut s`
10 |     s.push_str("s");
   |     ^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.

Sorry for the mistake! I am a newbie, slowly I will learn things.
Here is the copied code.

fn main() {
    let s = String::from("book");

    let p = pluralize(s.clone());

    println!("I have one {}, you have two {}", s, p,);
}

fn pluralize(s: String) -> String {
    s.push_str("s");
    s
}

Did you try that?

It still fails.

Here is the code in the Playground.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=37da75682aa3a10df1ad897344e26622

with making it mutable and then returning the s explicitly it worked.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8e2c5bafc81d34fb12c81aef9a96a91f

Thank you both for helping!

This worked

Please do not post code as an image.

Would this have helped you @miwoja?

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.