Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.2

Type to search the documentation.

    한국어
    Xen / reference v0.1.0-demo.2

    Syntax and common problems

    Use this page to look up syntax while writing code. The linked chapters provide explanations and runnable examples.

    TaskFormChapter
    Name a valuelet name: Type = value;Values
    Mutable variablelet mut name = value;Values
    Declare a functionfn name(x: Int) -> Int { return x; }Functions
    Conditional valueif condition { yes } else { no }Control flow
    Range loopfor item in start..end { ... }Control flow
    Conditional loopwhile condition { ... }Control flow
    Ordered values[first, second], Vec<T>Collections
    Struct declaration and constructionstruct User { name: String }, User { name: "Mina" }Types
    Alternative formsenum State { Idle, Ready(Int) }Types
    Select by formmatch value { Pattern => result, ... }Types
    Unpack a tuplelet (left, right) = pair;Types
    Genericsfn identity<T>(value: T) -> T { return value; }Generics
    Reference the original&value, &mut value, *referenceOwnership
    Propagate failurefallible_call()?Errors
    Use a project filemodule app;, import folder.module as short;Modules
    Use a standard moduleuse std.string as text;Standard library
    Declare a testtest fn name() { assert(condition); }Tests

    Declare functions, structs, enums, and impl blocks at the top level. Put values and statements inside functions. Use semicolons for statements, leaving value-producing if and match branch expressions without them. Only // line comments are supported; block comments are unavailable. Names and strings are case-sensitive.

    Integer types are I8 through I64 and U8 through U64; floating types are F32 and F64. Int = I64 and Float = F64. Literals are decimal; floating forms include 1.25 and 1e3. Numeric separators such as _ and hexadecimal literal syntax are unavailable.

    Precedence, highest first: calls/fields/indices/?, unary -/!/&/*, * / %, + -, < <= > >=, == !=, &&, ||. Add parentheses when unsure. Strings support +, ==, and !=. Vector equality is available for supported numeric, boolean, string, and nested vector elements. Structs and enums do not have ==.

    ProblemFix
    Unknown empty vector typeAdd a type: let values: Vec<Int> = [];
    Immutable variableUse let mut when changes are needed
    Different numeric typesConvert explicitly, for example with i32(...)
    Missing match armAdd the missing variant or an appropriate _
    Unknown generic type argumentGive arguments known types or specify all type arguments
    Changing a borrowed valueChange it after the reference’s last use
    Using a moved valueUse the new owner or read necessary information before moving
    Missing moduleCheck the path relative to the entry and the declared module name
    Invalid indexCheck the length before access

    This deliberately incorrect program uses a value after moving it:

    fn main() {
    let original = box(42);
    let transferred = original;
    println(original.into_inner());
    }
    Terminal window
    xen check move_error.xen

    The error includes moved. Replace the last line with println(transferred.into_inner()); to print 42.

    Closures, traits, exceptions, async, FFI, and package management are unavailable. Reference returns, mutable slices, string indexing, and match guards are also unsupported. Use the functions, loops, and Result patterns described in the relevant chapter.

    The bb facility for direct raw memory and operating-system calls is unnecessary for ordinary programs. Use the standard-library APIs for files, input/output, and paths.