- Bit operators & | ^ ~ << >> and their op= forms (Go precedence),
plus hex, binary and octal literals (0xff, 0b1010, 0o755)
- defer stmt / defer { ... }: runs when the block ends, last first,
also on return, ?, break and continue
- List methods insert, remove, clear, contains, index_of, reverse,
sort and sort_by (stable merge sort)
- rand module: int(a, b), float(), seed(n)
- assert(cond[, msg]) and `rex test`, which runs each test_ function
in its own process
- rex.toml marks a project's top folder: imports count from there
- Docs, formatter, LSP completions and editor grammars updated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
52 lines
2.4 KiB
Markdown
52 lines
2.4 KiB
Markdown
# Rex: learn the language
|
|
|
|
Rex is a small, fast, modern language. It looks like Go, runs like C, and has no
|
|
garbage collector, no pointers, and nothing low-level to write. You write plain code;
|
|
the compiler works out the memory.
|
|
|
|
These notes teach it from zero. Read them in order, or jump to the
|
|
[cheatsheet](11-cheatsheet.md) when you just need to look something up.
|
|
|
|
| # | Note | What you learn |
|
|
|---|------|----------------|
|
|
| 1 | [First steps](01-first-steps.md) | Running a program, `print`, `main` |
|
|
| 2 | [Values and types](02-values-and-types.md) | `var`, `const`, numbers, strings |
|
|
| 3 | [Control flow](03-control-flow.md) | `if`, `for`, `match` |
|
|
| 4 | [Functions](04-functions.md) | Parameters, results, closures, `defer` |
|
|
| 5 | [Lists and maps](05-lists-and-maps.md) | Collections |
|
|
| 6 | [Structs and enums](06-structs-and-enums.md) | Your own types, methods |
|
|
| 7 | [Errors and nil](07-errors-and-nil.md) | `error`, `?`, `T?` |
|
|
| 8 | [Memory](08-memory.md) | Copies, `shared`, `weak` |
|
|
| 9 | [Generics and interfaces](09-generics-and-interfaces.md) | Code that works for many types |
|
|
| 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time`, `rand`, tests |
|
|
| 11 | [Cheatsheet](11-cheatsheet.md) | Everything on one page |
|
|
| 12 | [A real program](12-calculator-walkthrough.md) | A calculator, split over five files |
|
|
| 13 | [C and C++ libraries](13-c-libraries.md) | `import "raylib.h"`, your own C code |
|
|
|
|
## How to read the examples
|
|
|
|
Every `rex` block that has a `func main` is a complete program you can save and run.
|
|
The block right after it (marked `text`) shows what it prints.
|
|
|
|
````
|
|
```rex
|
|
func main() {
|
|
print("hello")
|
|
}
|
|
```
|
|
```text
|
|
hello
|
|
```
|
|
````
|
|
|
|
## The four ideas behind Rex
|
|
|
|
1. **Values.** `var b = a` makes a real copy. Change `b` and `a` stays the same.
|
|
There are no hidden aliases, which makes programs easy to reason about.
|
|
2. **The compiler infers.** You don't write `mut`, lifetimes or `&`. A method that changes
|
|
its struct is detected automatically; a parameter that is only read is passed without a copy.
|
|
3. **Errors are values.** Functions return `(value, error)` and you pass problems up with `?`.
|
|
Missing values are `T?` and the compiler makes you check for `nil`.
|
|
4. **Sharing is explicit.** When you really want one value with several owners, you
|
|
write `shared`. Everything else has exactly one owner and is freed at the end of its block.
|