mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
doc: add a struct reference example (#13638)
This commit is contained in:
parent
ac1b31dbba
commit
1e76cccd48
26
doc/docs.md
26
doc/docs.md
@ -1929,6 +1929,32 @@ println(p.x)
|
||||
The type of `p` is `&Point`. It's a [reference](#references) to `Point`.
|
||||
References are similar to Go pointers and C++ references.
|
||||
|
||||
```v
|
||||
struct Foo {
|
||||
mut:
|
||||
x int
|
||||
}
|
||||
|
||||
fa := Foo{1}
|
||||
mut a := fa
|
||||
a.x = 2
|
||||
assert fa.x == 1
|
||||
assert a.x == 2
|
||||
|
||||
// fb := Foo{ 1 }
|
||||
// mut b := &fb // error: `fb` is immutable, cannot have a mutable reference to it
|
||||
// b.x = 2
|
||||
|
||||
mut fc := Foo{1}
|
||||
mut c := &fc
|
||||
c.x = 2
|
||||
assert fc.x == 2
|
||||
assert c.x == 2
|
||||
println(fc) // Foo{ x: 2 }
|
||||
println(c) // &Foo{ x: 2 } // Note `&` prefixed.
|
||||
```
|
||||
see also [Stack and Heap](#stack-and-heap)
|
||||
|
||||
### Default field values
|
||||
|
||||
```v
|
||||
|
Loading…
Reference in New Issue
Block a user