Hi, on the std::convert::Into trait page, it says:
Library authors should not directly implement this trait, but should prefer implementing the From trait, which offers greater flexibility and provides an equivalent
Into
implementation for free
The “greater flexibility” statement is hand-wavy to me (i.e. “believe me! it’s true” instead of “this is the rationale”).
In one of my projects, I’ve got:
- a
StandardRecord
struct - a varying number of custom record structs, e.g.
Custom1Record
,Custom2Record
Since the custom record structs can keep growing, it seems to make sense that every time I add another CustomXRecord
struct, I should also impl Into<StandardRecord> for CustomXRecord
. But the advice above turns it around and says, actually you should impl From<CustomXRecord> for StandardRecord
.
So there’s two questions here:
- What’s the magic behind prefer
From
overInto
? - Should I put the
impl From<CustomXRecord> for StandardRecord
inside theCustomXRecord
module? TheCustomXRecord
andStandardRecord
s currently all live in the same crate.