toString vs fromString

I thought the defacto way to convert an &str to String in Rust > 1.9.0 was using toString(), but in a simple unit test verifying if a Vec<String> contains something, I get:

assert!(vec!["test"].into_iter().all(|i| labels.contains(i.toString())));

method not found in &str

However, this works just fine, why is that?:

assert!(vec!["test"].into_iter().all(|i| labels.contains(&String::from(i))));

The method you are looking for is to_string(), rather than toString(). Rust normally names functions with snake_case.


I'm sorry - is it friday yet? And thank you!

1 Like

The ToString trait is in the prelude, so it isn't necessary to import it.


No worries! I'm actually surprised that the error message didn't help you out here, so I filed


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.