Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
50 lines
925 B
Plaintext
50 lines
925 B
Plaintext
struct Counter {
|
|
n: int
|
|
|
|
func bump(): int {
|
|
n += 1
|
|
return n
|
|
}
|
|
}
|
|
|
|
struct Pair {
|
|
left: Counter
|
|
right: Counter
|
|
}
|
|
|
|
func bump_copy(c: Counter): int {
|
|
return c.bump()
|
|
}
|
|
|
|
func main() {
|
|
// assignment copies
|
|
var a = Counter{1}
|
|
var b = a
|
|
b.n = 100
|
|
print(a.n, b.n)
|
|
|
|
// passing to a function copies
|
|
print(bump_copy(a), a.n)
|
|
|
|
// methods change the variable they are called on
|
|
a.bump()
|
|
a.bump()
|
|
print(a.n)
|
|
|
|
// nested structs are copied as a whole
|
|
var p = Pair{Counter{10}, Counter{20}}
|
|
var q = p
|
|
q.left.bump()
|
|
p.right.bump()
|
|
p.right.bump()
|
|
print(p.left.n, p.right.n, q.left.n, q.right.n)
|
|
|
|
// reading before a later call in the same expression: the earlier read wins
|
|
var c = Counter{0}
|
|
print(c.n, c.bump(), c.n)
|
|
|
|
// constants can be read and used with non-mutating code
|
|
const k = Counter{7}
|
|
print(k.n)
|
|
}
|