cross-posted from: https://lemmy.world/post/13508677

Hello All:

I am working on rust problems here. In the second question I solved it by simply adding the return statement for the function to modify. I am not sure what the lesson in ownership to walk away with in this problem. Any guidance?

  • JayjaderM
    link
    fedilink
    English
    arrow-up
    1
    ·
    9 months ago

    I agree with @maegul that this is probably just a poorly built exercise and/or overlooked by the creators on their “final” pass. Especially given that printing out the unit type () (as the code in main() effectively does) makes very little sense.

    However, I do see a way to “fix” the ownership “issues” that makes the rest of main() make sense, without touching main() itself:

    fn take_ownership(s: String) -> String {
        println!("{}", s);
        s
    }
    

    Basically what you stated you did to solve it, though I wanted to highlight that you might want/need to explicit the return type as well as actually returning the s parameter.