Skip to content

Commit 4272b33

Browse files
committed
Clean up and add reference to slurping assigment (#37410)
1 parent c5c8719 commit 4272b33

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

doc/src/manual/functions.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,7 @@ julia> b
368368

369369
The value on the right should be an iterator (see [Iteration interface](@ref man-interface-iteration))
370370
at least as long as the number of variables on the left (any excess elements of the
371-
iterator are ignored). Additionally a single underscore `_` (which is an otherwise invalid
372-
variable name, see [Allowed Variable Names](@ref man-allowed-variable-names)) can be used
373-
on the left to avoid assigning specific elements:
374-
375-
```jldoctest
376-
julia> _, _, _, d = 1:10
377-
1:10
378-
379-
julia> d
380-
4
381-
```
371+
iterator are ignored).
382372

383373
This can be used to simulate returning multiple values from functions by returning a tuple or
384374
other iterable value. For example, the following function returns a two values:
@@ -411,6 +401,39 @@ julia> y
411401
6
412402
```
413403

404+
If only a subset of the elements of the iterator are required, a common convention is to assign ignored elements to a variable
405+
consisting of only underscores `_` (which is an otherwise invalid variable name, see
406+
[Allowed Variable Names](@ref man-allowed-variable-names)):
407+
408+
```jldoctest
409+
julia> _, _, _, d = 1:10
410+
1:10
411+
412+
julia> d
413+
4
414+
```
415+
416+
!!! compat "Julia 1.6"
417+
`...` with assignment requires Julia 1.6
418+
419+
If the last symbol in the assignment list is suffixed by `...` (known as _slurping_), then
420+
it will collect the remaining elements of the iterator:
421+
422+
```jldoctest
423+
julia> a, b... = 1:4
424+
1:4
425+
426+
julia> a
427+
1
428+
429+
julia> b
430+
3-element Vector{Int64}:
431+
2
432+
3
433+
4
434+
```
435+
This behaviour can be customized for specific types by extending [`Base.rest`](@ref).
436+
414437
## Argument destructuring
415438

416439
The destructuring feature can also be used within a function argument.

0 commit comments

Comments
 (0)