Into trait is one of those most frequently used traits in Rust. In many cases it works well with type inference. However in cases it does not work, usually we have to write
let converted = <MyType>::from(v);
In cases that
Into was not derived from
From, it is more ugly:
let converted = <_ as Into<MyType>>::into(v);
The problem of above is that
- It is verbose;
- The way to call this and its sementic is like
from, but the verb suggest otherwise, a code reader can be confused.
My proposal is to write
let converted = v into Mytype;
and let de-sugaring happen here.
The proposed way the new
into keyword should work exactly like
as, except that it is pure syntax sugar.
Another way is to introduce a mechanism that allow annotate the
into call to specify the type.
let converted = v<as Into<MyType>>.into();
(v as Into<MyType>).into() technically requires
#![feature(unsized_local)], an I am not sure whether it have hidden costs)