3 ms·
Are 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
by kevincox 4y ago
Are 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.