Xen / examples v0.1.0-demo.2
Structs, enums, tuples
This topic is not available in the selected language or version. Showing the start of this section.
Structs, enums, tuples
Run from the repository or distribution root. Put xen on PATH, or substitute ./bin/xen.
xen check examples/types.xenxen run examples/types.xenExpected output
Section titled “Expected output”todo: read the guidedue in 2answer: 42Complete source
Section titled “Complete source”// SPDX-License-Identifier: MIT OR Apache-2.0struct Task { title: String, done: Bool }enum Priority { Normal, Urgent(Int) }
impl Task { fn label(&self) -> String { return if self.done { "done: " + self.title } else { "todo: " + self.title }; }}
fn main() { let task = Task { title: "read the guide", done: false }; println(task.label()); let priority = Priority.Urgent(2); println(match priority { Priority.Normal => "normal", Priority.Urgent(days) => "due in " + int_to_str(days), }); let (number, label) = (42, "answer"); println(label + ": " + int_to_str(number));}Build an executable
Section titled “Build an executable”xen build examples/types.xen -o appRun the generated executable on a compatible Linux machine.