Rust without caring about memory management
Python-like but typed
No GC, no RC.
Kitchen sink:
fn main() {
var x = 12;
if x > 0 {
debug("above 12");
} else if x < 0 {
debug(12);
}
while x < 20 {
x = x + 1;
if x > 15 {
continue;
}
debug(x);
if x < 18 {
break;
}
}
var a = [1, 2, 3];
push(@a, 4);
assert(a == [1, 2, 3, 4]);
}
Note: now with type inference.
- Unbounded integers:
5
of typeint
- Mutable strings:
"hello"
of typestr
- Booleans:
true
andfalse
of typebool
- Unit/void value:
()
of type()
- Structures:
struct Person {
x: int,
y: str,
}
- Arrays
[a, b, c]
of type[T]
- Enumerated types (pattern matching is WIP):
enum Person {
Test(int),
Test2(int),
}
- Hashmaps (WIP)
- Sets (WIP)
Motto: as if “Every use of a variable clones the value of the variable”
Implies:
- Following code will print "4 2"
n = 2
x = n
n = n + 2
debug(n, x) // 4 2
- To pass mutable references, you need to use the
@
operator
n = 2
x = @n
n = n + 2
debug(n, x) // 4 4
Under the hood, optimizations are done on the output code to clone as little as possible while respecting the semantics.
Outputs Rust code that relies heavily on Cow
s and the big_num
crate.