What is the same
fn f(x: Int) -> Int { }, let (immutable by default), match, enum and struct, Result and ?, if let x = y { }, and for i in 0..n all have the same shape.
fn half(n: Int) -> Result<Int, String> {
if n % 2 != 0 { return Err(f"{n} is odd") }
return Ok(n.div(2))
}
fn quarter(n: Int) -> Result<Int, String> {
let h = half(n)?
return half(h)
}
print(quarter(8))
print(quarter(6))outputOk(2)
Err("3 is odd")What is different
var count = 0replaceslet mut count = 0;x = 1creates a local variable withoutletwhere natural.print(x)replacesprintln!("{}", x), and f-strings interpolate:f"a {x}".- A missing value is
nilwith an optional type likeInt?, andif let x = y { }unwraps it. - Whole numbers are
Intand decimals areFloat: noi32,u64,f64orusize. Numbers mix like Python:7 / 2is3.5, whole-number division is7.div(2). - Methods go inside the class, or in
extend Foo { ... }, notimpl Foo { }. Paths use.rather than::. [1, 2]replacesvec![1, 2];while true { }replacesloop { }; text isStringwithout lifetimes or borrow checker ceremony.
let mut count = 0
println!("{}", count)compiler sayserror [E0101]: `let mut` is Rust — Mycel writes `var` --> habit_two.myc:1:5 | 1 | let mut count = 0 | ^^^ why: `let` is fixed once set; `var` can change fix: write `var count = ...` 1 error, 0 warnings
Every Rust habit and what Mycel says
Mycel's compiler recognises words and symbols from other languages. Instead of a generic syntax error you get the language it came from and the Mycel spelling. The full list for Rust is below; rows marked works are valid Mycel as you already write them.
| You write | Mycel says |
|---|---|
fn f(x: Int) -> Int { } | works: the same shape |
let x = 5 (immutable by default) | works: the same |
match x { A => .. } | works: the same |
enum and struct | works: the same |
Result and ? | works: the same |
for i in 0..n | works: the same |
if let Some(x) = y | works: if let x = y { } |
let mut x = 5 | let mut` is Rust — Mycel writes `var x = 5` |
println!("{}", x) | println! is Rust — Mycel writes print(x) |
None | None is Python/Rust — Mycel writes nil |
i32 / u64 / f64 / usize | i32 is Rust — Mycel's whole-number type is Int |
impl Foo { } | impl is Rust — Mycel puts methods inside the class, or extend Foo { ... } |
vec![1, 2] | vec! is Rust — Mycel writes [1, 2] |
Type::method | :: is Rust — Mycel writes . |
loop { } | loop is Rust — Mycel writes while true { ... } |
let x: &str | no references: Mycel's text type is String |
Every row is tested in the compiler, and mycel help habits prints the same list. See also the overview on the Mycel page.
Using Rust packages: planned (coming in Phase D)
A compatibility engine so a Mycel program can use Rust packages is planned, and the package manager ply already recognises the install flags. The planned flag is --cargo (or --rust). Installing packages from other languages is marked as coming until Phase B lands, and Mycel itself is not released: there is nothing to download yet. The playground runs the real compiler in your browser with no packages.
Run the first example yourself
The Mycel playground opens in your browser. It runs the real compiler in a sandbox with no file, network or storage access, and nothing is sent to a server. For a guided walk through the language, read Learn Mycel.
Other starting points
Mycel is written so a program reads the same whichever language you learned first. Pick the guide closest to yours, or take the full tour.