Implicit conversions are not compatible with type inference. If you implicitly introduce into
everywhere, then in many cases the types will not be inferred. ?
is a good example of this: if you're using ?
inside of a closure, than you must explicitly annotate the return type of that closure, otherwise the error type cannot be inferred. This is true even in the trivial case where an obvious answer would be "don't into
at all and just use the current error type". For example, this fails to compile:
let closure = |x: Result<Foo, E>| {
let y = x?;
Ok(y.transform())
};
even though the type Result<_,E>
is what is likely expected. This has been a major stumbling block for try
expressions, which are otherwise a very desired feature.
Rust uses a lot of generic functions, and they just wouldn't work if into
conversions were inserted everywhere. Even the simple example opt.unwrap_or_else(|| 3)
wouldn't work, since the argument closure would have to be converted into something indeterminate. Even if some ambiguity resolution rules were added, it would be a major footgun, since one couldn't know which types and which impls would be used based purely on the code. One would have to run the type inference, trait resolution, and conversion resolution algorithms in the head, and they would certainly have a lot of edge cases and confusing behaviour. Choosing wrong impls and conversions can have drastically different effects.
In the languages which have implicit conversions, such as Scala or C++, they are often regarded as a misfeature. Extra compiler features, code discipline and static analysis are required to get implicit conversions under control, and they are often a cause of bugs.
Probably not, for the statement as written, but it's also not that much shorter or clearer than
let s = String::from("666");
If the type is omitted, then you would again has issues with type inference. Either because the type won't be inferred, or because it is inferred to be something undesirable.
For the specific case in your example, custom literals seem like a better solution. In fact, you have used one. This doesn't require any implicit conversions, since the literal directly constructs a value of the specified type.