diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-03 15:07:18 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-03 15:07:18 +0100 |
| commit | 7311b7638de76ba68f197f891248b426e656e730 (patch) | |
| tree | 2dee49ed55134676fbcd8a41ae3598c0ed5261c2 /examples/03-inductive-data-types | |
| parent | 139dc93bb10daea87eba99e8f28d8a668c94e13b (diff) | |
Add constructor functions for Rust enum variants
Diffstat (limited to 'examples/03-inductive-data-types')
| -rw-r--r-- | examples/03-inductive-data-types/output/rust/src/arithmetic.rs | 10 | ||||
| -rw-r--r-- | examples/03-inductive-data-types/output/rust/src/main.rs | 14 |
2 files changed, 17 insertions, 7 deletions
diff --git a/examples/03-inductive-data-types/output/rust/src/arithmetic.rs b/examples/03-inductive-data-types/output/rust/src/arithmetic.rs index a9679dc..d6944a9 100644 --- a/examples/03-inductive-data-types/output/rust/src/arithmetic.rs +++ b/examples/03-inductive-data-types/output/rust/src/arithmetic.rs @@ -5,6 +5,16 @@ pub enum Expression { Const(crate::arithmetic::Const), } +impl Expression { + pub fn add(value: crate::arithmetic::Add) -> Self { + Self::Add(::std::boxed::Box::new(value)) + } + + pub fn r#const(value: crate::arithmetic::Const) -> Self { + Self::Const(value) + } +} + pub struct Add { pub left: crate::arithmetic::Expression, pub right: crate::arithmetic::Expression, diff --git a/examples/03-inductive-data-types/output/rust/src/main.rs b/examples/03-inductive-data-types/output/rust/src/main.rs index 8c1f750..28f7cec 100644 --- a/examples/03-inductive-data-types/output/rust/src/main.rs +++ b/examples/03-inductive-data-types/output/rust/src/main.rs @@ -3,11 +3,11 @@ mod arithmetic; use arithmetic::{Add, Const, Expression}; fn main() { - let expression = Expression::Add(Box::new(Add { - left: Expression::Add(Box::new(Add { - left: Expression::Const(Const { value: 42 }), - right: Expression::Const(Const { value: 47 }), - })), - right: Expression::Const(Const { value: 52 }), - })); + let expression = Expression::add(Add { + left: Expression::add(Add { + left: Expression::r#const(Const { value: 42 }), + right: Expression::r#const(Const { value: 47 }), + }), + right: Expression::r#const(Const { value: 52 }), + }); } |
