Targets · Rust · Example

Example

The whole language in one short program — a module with a record, a tagged union, a function with per-platform native bodies, and a function that wires up state, a write-back and a return — and the complete Rust it renders to. It is the same program shown on the language page.

Contents

How a Program becomes Rust, in parts. Start with the introduction, then follow each part into the rendered output and the runtime it links against.

Every keyword, in one program

Each keyword has its own page under keywords; here they are all together.

MODULE App

  TYPE Profile
    FIELD Text name
    FIELD Maths.Integer age

  TYPE Status
    OPTION App.Profile active
    OPTION Text pending

  FUNCTION Text Greet
    INPUT Text name
    NATIVE js    "return 'Hi ' + name;"
    NATIVE dart  "return 'Hi ' + name;"
    NATIVE rust  "format!(\"Hi {}\", name)"

  FUNCTION Text Welcome
    INPUT BINDING App.Profile profile
    HOLE Text row
      INPUT Text line
    STATE Maths.Integer count = 0
    STATE LOCKED Network.Web.Response saved = null
    REVERSE App.Profile submit
      LET Network.Web.Response response = Network.Web.Client.Post profile
      SET saved = response
    LET Text label = Maths.Integer.ToString count
    = label

(The function's swift row is replaced with a rust one here, since Rust needs its own NATIVE body.)

Rendered Rust

The module is a mod; the record is a struct and the union an enum, both keeping their :: path; state uses generics; the function's rust body is spliced in; closures are boxed; and the trailing = is the final expression.

pub mod app {

    #[derive(Clone)]
    pub struct Profile {
        pub name: String,
        pub age: i64,
    }

    pub enum Status {
        Active(Profile),
        Pending(String),
    }

    pub fn Greet(name: String) -> String {
        format!("Hi {}", name)               // the rust NATIVE body
    }

    pub fn Welcome(profile: Profile, row: Box<dyn Fn(String) -> ReactiveValue<String>>) -> ReactiveValue<String> {
        let count = Program::_Runtime::Reactive::state::<i64>(0);
        let saved = Program::_Runtime::Reactive::state_locked::<network::web::Response>(None);
        Program::_Runtime::Reactive::reverse(submit, Box::new(move || {
            let response = network::web::client::Post(profile);
            Program::_Runtime::Reactive::set(saved, response);  // SET
        }));
        let label = maths::integer::ToString(count);
        label
    }
}

Reading it

Indentation becomes nested boxed closures — the REVERSE body is a Box::new(move || { … }). Dotted names stay real through nested modules, with :: for the dots (network::web::client::Post) — see naming considerations. The per-keyword mapping is on keywords.