Skip to content

Commit 95001ec

Browse files
committed
docs: describe new ref syntax
1 parent 31f1ef4 commit 95001ec

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Now, with that out of the way, let's see the reference and then some more detail
7979
### References and Lifetimes
8080

8181
- `@lifetime lt begin ... end`: Create a scope for references whose lifetimes `lt` are the duration of the block
82-
- `@ref [:mut] var = value in lt`: Create a reference to owned value `value` and assign it to `var` within the given lifetime scope `lt` (mutable if `:mut` is specified)
82+
- `@ref lt [:mut] var = value`: Create a reference, for the duration of `lt`, to owned value `value` and assign it to `var` (mutable if `:mut` is specified)
8383

8484
### Assignment
8585

@@ -227,8 +227,8 @@ immutable references first:
227227
```julia
228228
julia> @bind :mut data = [1, 2, 3];
229229

230-
julia> @lifetime lt begin
231-
@ref ref = data in lt
230+
julia> @lifetime a begin
231+
@ref a ref = data
232232
ref
233233
end
234234
Borrowed{Vector{Int64},BoundMut{Vector{Int64}}}([1, 2, 3])
@@ -246,9 +246,9 @@ BoundMut{Vector{Int64}}([4, 2, 3])
246246
Note that we can have multiple _immutable_ references at once:
247247

248248
```julia
249-
julia> @lifetime lt begin
250-
@ref ref1 = data in lt
251-
@ref ref2 = data in lt
249+
julia> @lifetime a begin
250+
@ref a ref1 = data
251+
@ref a ref2 = data
252252
ref1 == ref2
253253
end
254254
true
@@ -257,19 +257,19 @@ true
257257
For mutable references, we can only have one at a time:
258258

259259
```julia
260-
julia> @lifetime lt begin
261-
@ref :mut mut_ref = data in lt
262-
@ref :mut mut_ref2 = data in lt
260+
julia> @lifetime a begin
261+
@ref a :mut mut_ref = data
262+
@ref a :mut mut_ref2 = data
263263
end
264264
ERROR: Cannot create mutable reference: value is already mutably borrowed
265265
```
266266

267267
And we can't mix mutable and immutable references:
268268

269269
```julia
270-
julia> @lifetime lt begin
271-
@ref ref = data in lt
272-
@ref :mut mut_ref = data in lt
270+
julia> @lifetime a begin
271+
@ref a ref = data
272+
@ref a :mut mut_ref = data
273273
end
274274
ERROR: Cannot create mutable reference: value is immutably borrowed
275275
```
@@ -284,8 +284,8 @@ julia> function borrow_vector(v::Borrowed) # Signature confirms we only need im
284284
julia> @bind vec = [1, 2, 3]
285285
Bound{Vector{Int64}}([1, 2, 3])
286286

287-
julia> @lifetime lt begin
288-
borrow_vector(@ref d = vec in lt) # Immutable borrow
287+
julia> @lifetime a begin
288+
borrow_vector(@ref a d = vec) # Immutable borrow
289289
end
290290

291291
julia> vec
@@ -298,8 +298,8 @@ We are also able to clone from reference:
298298
julia> @bind :mut data = [1, 2, 3]
299299
BoundMut{Vector{Int64}}([1, 2, 3])
300300

301-
julia> @lifetime lt begin
302-
@ref ref = data in lt
301+
julia> @lifetime a begin
302+
@ref a ref = data
303303
@clone :mut clone = ref
304304
clone[2] = 4
305305
@show clone ref

0 commit comments

Comments
 (0)