Xen / examples v0.1.0-demo.2
Functions and loops
This topic is not available in the selected language or version. Showing the start of this section.
Functions and loops
Run from the repository or distribution root. Put xen on PATH, or substitute ./bin/xen.
xen check examples/control_flow.xenxen run examples/control_flow.xenExpected output
Section titled “Expected output”20enough21Complete source
Section titled “Complete source”// SPDX-License-Identifier: MIT OR Apache-2.0fn doubled(value: Int) -> Int { return value * 2;}
fn main() { let mut total = 0; for number in 1..5 { total = total + doubled(number); } let label = if total >= 20 { "enough" } else { "more" }; println(total); println(label); let mut remaining = 2; while remaining > 0 { println(remaining); remaining = remaining - 1; }}Build an executable
Section titled “Build an executable”xen build examples/control_flow.xen -o appRun the generated executable on a compatible Linux machine.