From 7311b7638de76ba68f197f891248b426e656e730 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 Jul 2026 15:07:18 +0100 Subject: Add constructor functions for Rust enum variants --- .../03-inductive-data-types/output/rust/src/arithmetic.rs | 10 ++++++++++ examples/03-inductive-data-types/output/rust/src/main.rs | 14 +++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'examples/03-inductive-data-types') 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 }), + }); } -- cgit v1.2.3