[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
Uh oh!
There was an error while loading. Please reload this page.
-
In Rust we have the
std::convert::Fromtrait. This trait can be used to effectively write constructors, while additionally having the benefit of having aFromimplementation, as well as the correspondingIntoblanket implementation.What do you think, should this be added to the constructors section?
Same reasoning with
std::convert::TryFromfor fallible constructors.Beta Was this translation helpful? Give feedback.
All reactions