Skip to content

Commit a5bd61a

Browse files
committed
doc: fmt ownership.md examples, add okfmt tags
1 parent 303b6d9 commit a5bd61a

1 file changed

Lines changed: 58 additions & 58 deletions

File tree

‎doc/ownership.md‎

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ with the `-ownership` flag.
66

77
## Quick start
88

9-
```v
9+
```v okfmt
1010
fn main() {
11-
s1 := 'hello'.to_owned()
12-
s2 := s1 // s1 is moved to s2
13-
println(s1) // error: use of moved value: `s1`
11+
s1 := 'hello'.to_owned()
12+
s2 := s1 // s1 is moved to s2
13+
println(s1) // error: use of moved value: `s1`
1414
}
1515
```
1616

@@ -26,158 +26,158 @@ Call `.to_owned()` on a string to create an owned copy. Only strings created wit
2626
`.to_owned()` participate in ownership tracking — regular string literals and primitive
2727
types (int, f64, bool, ...) are unaffected.
2828

29-
```v
30-
s := 'hello'.to_owned() // s is owned
31-
t := 'world' // t is a normal string, no ownership tracking
29+
```v okfmt
30+
s := 'hello'.to_owned() // s is owned
31+
t := 'world' // t is a normal string, no ownership tracking
3232
```
3333

3434
## Move semantics
3535

3636
Assigning an owned value to another variable **moves** it. The original variable
3737
becomes unusable:
3838

39-
```v
39+
```v okfmt
4040
s1 := 'hello'.to_owned()
41-
s2 := s1 // move
42-
println(s1) // error: use of moved value: `s1`
41+
s2 := s1 // move
42+
println(s1) // error: use of moved value: `s1`
4343
```
4444

4545
Passing an owned value to a function also moves it:
4646

47-
```v
47+
```v okfmt
4848
fn takes_ownership(s string) {
49-
println(s)
49+
println(s)
5050
}
5151
5252
fn main() {
53-
s := 'hello'.to_owned()
54-
takes_ownership(s)
55-
println(s) // error: use of moved value: `s`
53+
s := 'hello'.to_owned()
54+
takes_ownership(s)
55+
println(s) // error: use of moved value: `s`
5656
}
5757
```
5858

5959
### Preventing moves with `.clone()`
6060

6161
Use `.clone()` to make an independent copy instead of moving:
6262

63-
```v
63+
```v okfmt
6464
s1 := 'hello'.to_owned()
65-
s2 := s1.clone() // s1 is NOT moved
66-
println(s1) // ok
67-
println(s2) // ok
65+
s2 := s1.clone() // s1 is NOT moved
66+
println(s1) // ok
67+
println(s2) // ok
6868
```
6969

70-
```v
71-
takes_ownership(s.clone()) // s is NOT moved
72-
println(s) // ok
70+
```v okfmt
71+
takes_ownership(s.clone()) // s is NOT moved
72+
println(s) // ok
7373
```
7474

7575
## Borrowing
7676

7777
Pass `&variable` to borrow without moving. The original stays usable:
7878

79-
```v
79+
```v okfmt
8080
fn calculate_length(s &string) int {
81-
return s.len
81+
return s.len
8282
}
8383
8484
fn main() {
85-
s := 'hello'.to_owned()
86-
len := calculate_length(&s)
87-
println(s) // ok — s was borrowed, not moved
85+
s := 'hello'.to_owned()
86+
len := calculate_length(&s)
87+
println(s) // ok — s was borrowed, not moved
8888
}
8989
```
9090

9191
Multiple immutable borrows are allowed:
9292

93-
```v
93+
```v okfmt
9494
s := 'hello'.to_owned()
9595
r1 := &s
96-
r2 := &s // ok
96+
r2 := &s // ok
9797
```
9898

9999
Mutable borrows via `mut` parameters work too — the variable is usable after the
100100
call returns:
101101

102-
```v
102+
```v ignore
103103
fn append_world(mut s string) {
104-
s = s + ' world'
104+
s = s + ' world'
105105
}
106106
107107
fn main() {
108-
mut s := 'hello'.to_owned()
109-
append_world(mut s)
110-
println(s) // ok — prints "hello world"
108+
mut s := 'hello'.to_owned()
109+
append_world(mut s)
110+
println(s) // ok — prints "hello world"
111111
}
112112
```
113113

114114
### Borrow restrictions
115115

116116
A borrowed variable cannot be moved or reassigned while the borrow is active:
117117

118-
```v
118+
```v okfmt
119119
s := 'hello'.to_owned()
120120
r := &s
121-
s2 := s // error: cannot move `s` because it is borrowed
121+
s2 := s // error: cannot move `s` because it is borrowed
122122
```
123123

124-
```v
124+
```v okfmt
125125
mut s := 'hello'.to_owned()
126126
r := &s
127-
s = 'world'.to_owned() // error: cannot assign to `s` because it is borrowed
127+
s = 'world'.to_owned() // error: cannot assign to `s` because it is borrowed
128128
```
129129

130130
## Return value ownership
131131

132132
Functions that create and return owned values transfer ownership to the caller:
133133

134-
```v
134+
```v okfmt
135135
fn gives_ownership() string {
136-
return 'hello'.to_owned()
136+
return 'hello'.to_owned()
137137
}
138138
139139
fn main() {
140-
s1 := gives_ownership() // s1 is owned
141-
s2 := s1 // move
142-
println(s1) // error: use of moved value
140+
s1 := gives_ownership() // s1 is owned
141+
s2 := s1 // move
142+
println(s1) // error: use of moved value
143143
}
144144
```
145145

146146
Functions that return a parameter pass ownership through:
147147

148-
```v
148+
```v okfmt
149149
fn takes_and_gives_back(s string) string {
150-
return s
150+
return s
151151
}
152152
153153
fn main() {
154-
s1 := 'hello'.to_owned()
155-
s2 := takes_and_gives_back(s1) // s1 moved in, ownership comes back as s2
156-
println(s1) // error: s1 was moved
157-
println(s2) // ok
154+
s1 := 'hello'.to_owned()
155+
s2 := takes_and_gives_back(s1) // s1 moved in, ownership comes back as s2
156+
println(s1) // error: s1 was moved
157+
println(s2) // ok
158158
}
159159
```
160160

161161
## Full example
162162

163163
From the Rust book, translated to V:
164164

165-
```v
165+
```v okfmt
166166
fn gives_ownership() string {
167-
s := 'hello'.to_owned()
168-
return s
167+
s := 'hello'.to_owned()
168+
return s
169169
}
170170
171171
fn takes_and_gives_back(a_string string) string {
172-
return a_string
172+
return a_string
173173
}
174174
175175
fn main() {
176-
s1 := gives_ownership()
177-
s2 := 'hello'.to_owned()
178-
s3 := takes_and_gives_back(s2)
179-
println(s1) // ok
180-
println(s3) // ok
176+
s1 := gives_ownership()
177+
s2 := 'hello'.to_owned()
178+
s3 := takes_and_gives_back(s2)
179+
println(s1) // ok
180+
println(s3) // ok
181181
}
182182
```
183183

0 commit comments

Comments
 (0)