Skip to main content

Example Programs

These examples show the current public surface of Metel as documented in the wiki.

Hello world

fun main() {
println("hello, Metel");
}

A small struct

struct Point {
x: Float,
y: Float,
}

impl Point {
fun magnitude(self) -> Float {
return (self.x * self.x) + (self.y * self.y);
}
}

fun main() {
let p = Point { x: 3.0, y: 4.0 };
println(p.magnitude());
}

Error handling with ?

fun parse_and_double(input: String) -> Result<Int, String> {
let value = parse_int(input)?;
return Result::Ok { value: value * 2 };
}

Match and Perhaps

fun describe(value: Perhaps<Int>) -> String {
match value {
Perhaps::Some { value } => "value: " + value.to_string(),
Perhaps::None => "no value",
}
}