From2? or just From<(A, B)>

Suppose we have some object C which can be constructed from a pair (A, B).

Is there a From2 in Rust, or is the standard way to just use From<(A, B)> ?

From<(A, B)> is the way to go.

fn from((a, b): (A, B)) -> C {
    /// ...

I personally prefer just fn new(a: A, b: B) -> Self unless the conversion from tuple is really obvious and natural, like 2D coord


but then we might need new0, new1, new2, new3, for different pairs …

The naming convention for constructors is:

  • Only one constructor, or one where the arguments are obvious: new
  • Multiple equally valid constructors: from_*


enum Anchor { TopLeft, Center }

impl Rectangle {
    pub fn from_corners(first: Point, second: Point) -> Self { ... }
    pub fn from_dims(dims: (f64, f64), point: Point, anchor: Anchor) -> Self { ... }

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.