[Idioms/Constructors] use From to implement constructors #277
wookietreiber
started this conversation in
Ideas
Replies: 1 comment
-
Imo, implementing On that note, I often find myself implementing Value {
Int(i64),
Float(f64),
String(String),
}
impl From<i64> for Var {
fn from(v: i64) -> Self {
Self::Int(v)
}
}
impl From<f64> for Var {
fn from(v: f64) -> Self {
Self::Float(v)
}
}
impl From<String> for Var {
fn from(v: String) -> Self {
Self::String(v)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Rust we have the
std::convert::From
trait. This trait can be used to effectively write constructors, while additionally having the benefit of having aFrom
implementation, as well as the correspondingInto
blanket implementation.What do you think, should this be added to the constructors section?
Same reasoning with
std::convert::TryFrom
for fallible constructors.Beta Was this translation helpful? Give feedback.
All reactions