Skip to content

Why can't I use push_str to resolve strings3 ? #2210

Closed Answered by temanmd
RHSNOW asked this question in Q&A
Discussion options

You must be logged in to vote

When you do to_string() here:

fn compose_me(input: &str) -> String {
    ...
    input.to_string().push_str(a);
    ...
}

then you call push_str on a new String (returned from to_string() function call), not on input.

input already has the String type, you don't have to convert it again, just remove to_string() to directly modify input.

Note that the type returned here is already a String:

let input = String::from(input);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@RHSNOW
Comment options

Answer selected by mo8it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2209 on February 19, 2025 14:25.