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.

:man_facepalming:

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.

3 Likes

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

https://github.com/rust-lang/rust/issues/101013

4 Likes

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.