#Converting &str into String

7 messages · Page 1 of 1 (latest)

fringe onyx
#

What is the better approach, to_owned or to_string?

please ping on reply, thanks in advance.

pallid sierra
#

for me: .to_owned()

tall stratus
#

these are all semantically different functions:

  1. to_owned is for conversion of borrowed types to owned types where both the borrowed and owned type are different
  2. to_string is for invocation of the Display implementation to be shown to the user
  3. into is for infallible conversion of data from one type to another
fringe onyx
#

For context, I have a dependencies.txt file where each line is a dependency.

#
let dependencies = 
            fs::read_to_string(
                format!("{}/dependencies.txt", path)
            )?.split("\n").map(|it: &str| it.to_string()).collect();
#

this is how i parsed it

tall stratus
#

semantically, to_owned makes the most sense here as it is meant to somehow take ownership of the strings via clone