Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yup! Yeah, it really depends on your style. Rust tends to prefer methods to free functions, due to multi-dispatch, but there's a good argument for either style.

Furthermore, there's a non-generic version of this particular conversion:

    "foo".to_string()
And, a generic version that expresses a different semantic:

    "foo".to_owned()
Strings are a very common type, and so implement a _lot_ of these things, even when this kind of usecase may overlap.


> Strings are a very common type, and so implement a _lot_ of these things, even when this kind of usecase may overlap.

I can't say this is very helpful; it's hard enough learning the one way. Why have `to_string()` if we have `into()`?


Well, one reason is that to_string() is ancient, but into() is fairly new. Another is that 'to' and 'into' are two different things: to_string() takes its caller by reference, and produces a copy. into() takes its caller by value, and so consumes it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: