3 ms·
Seems standard enough, learning that a Vector is a resizable Array was needed, the auto closing sockets and files is cool. But what on Earth is string.into() ?
by nailer 4y ago
Seems standard enough, learning that a Vector is a resizable Array was needed, the auto closing sockets and files is cool. But what on Earth is string.into() ?
Not asking for an answer just pointing out that’s not a very useful name.
- aliasxneo 4y agoIt would probably be more clear if the type was specified here instead of letting the compiler infer it.
- andrewaylett 4y agoIf it's not clear that the compiler will infer the correct type, you can be specific. From the context, there's only one possible type though so that's redundant. Unlike C, older C++, or older Java, Rust expressions aren't necessarily fully typed in isolation. It's quite normal for a function to have an independently-generic return type and for the final type to be provided solely by context. Type inference only happens within named functions though: you need to specify your function signature. Even if you find yourself wanting to know what the type actually is, the compiler (in its guise as a language server) can tell you that and let your IDE show you, rather than requiring you to type it out yourself.
- aliasxneo 4y agoAbsolutely, I would have done the same in my code, I was more referring to the fact that sometimes it's helpful to be explicit when targeting a novice audience.
- jamincan 4y agoThe only way I can figure out to make this explicit, though, seems very verbose and non-intuitive and frankly no clearer than `.into()` on its own: Err(Into::<Box<dyn Error>>::into("sha1 hash is not valid")) Is there a better way to constrain the trait implementation of Into?
- andrewaylett 4y agoYou can (although I wouldn't recommend it) be explicit about the type using 'as': Err("sha1 hash is not valid".into()) as Result<(), Box<dyn Error>> Or I'd often reach for an intermediate variable before a turbofish: let e: Box<dyn Error> = "".into(); Err(e) The 'as' trick is useful for helping the compiler to understand the return of a lambda where you need it to coerce errors to a specific type.
- sbeckeriv 4y agoI have learned to enjoy the from trait in my code. A single location of converting structures has been nice. I has also been a nice place to double down on tests. I usually call from in code. https://doc.rust-lang.org/rust-by-example/conversion/from_into.html https://doc.rust-lang.org/rust-by-example/conversion/from_in...
- kevincox 4y agoAre you referring to "sha1 hash is not valid".into()? This is a "not very useful name" because it is a very generic (and core) trait. https://doc.rust-lang.org/std/convert/trait.Into.html https://doc.rust-lang.org/std/convert/trait.Into.html This is the standard interface for converting anything into anything. The way this is often used relies on type-inference. In this case it is being used as `return Err("sha1 hash is not valid".into())` inside a function that returns `Result<(), Box<dyn Error>>`. This means that the expression is inferred to evaluate to a `Box<dyn Error>`. Luckily there is an implementation of the Into trait for &str to that type (https://doc.rust-lang.org/std/convert/trait.From.html#impl-From%3C%26str%3E-for-Box%3Cdyn%20Error%20+%20Send%20+%20Sync%20+%20%27a%3E https://doc.rust-lang.org/std/convert/trait.From.html#impl-F...). So `.into()` just does the right thing. This is a less common use case but the most common one I see is: let s: String = "foobar".into() Which converts a &'static str to a String.
- nailer 4y agoI am simply referring naming a method into(). Into… what? It’s like one of those 2010 frameworks that used .use(). The name communicates very little. It sounds like it should be called .intoType() or .fixType() (it seems to use the compiler to work out the specific type) but again I don’t care about the answer I’m merely commenting that it’s not evident from reading the code as someone that knows a few languages.
- kevincox 4y agoBut that's the thing. It is into anything. The method name can't be any more specific because the method isn't more specific.
- nailer 4y agoHence not mentioning a specific type in either suggestion. The method name can be understandable without having to resort to docs, and without having to mention specific types. With the current vague name I thought string.into() would be some kind of character converter.
- avgcorrection 4y ago“String” is not a useful name. How would a non-programmer guess what that means? It’s just another noun (like the verb “into”) that you have to learn.
- nailer 4y agoWell yes, string isn’t a good name for people new to programming, but that’s a separate topic since most programming languages use the term. Python did similar with Arrays -> Lists and Hashmaps -> Dicts. into() isn’t a verb either.
- avgcorrection 4y ago> into() isn’t a verb either. You got me there, smartypants. What my whole argument was hinging on.
- nailer 4y agoI thought it was substantive (methods should be verbs and this isn’t one) but if you prefer please ignore that sentence and focus on the rest of my reply.
- avgcorrection 4y ago`into` is just for conversion of values, something which most programming languages have to deal with, just like most languages have strings. So it’s not like Rust has invented something which needs a new word belonging to <word group>. So I agree. Or disagree with my original self.
- nailer 4y agoSure but I haven’t seen into() before in Perl, python, Ruby, C#, JS or TS. Whereas I have seen string.
- 4y ago