3 ms·
So "foo".into() could be expressed as String::from("foo") I would personally use the latter form as it is easier to parse and reason about. Thanks f
by 1_player 11y ago
So
"foo".into()
could be expressed as
String::from("foo")
I would personally use the latter form as it is easier to parse and reason about. Thanks for the explanation!
- steveklabnik 11y agoYup! 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.
- Veedrac 11y ago> 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()`?
- steveklabnik 11y agoWell, 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.