# No collect!() function?

**URL:** <https://discourse.julialang.org/t/no-collect-function/48815>\
**Category:** New to Julia\
**Created:** [October 22, 2020, 12:50pm UTC](https://discourse.julialang.org/t/no-collect-function/48815 "2020-10-22T12:50:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 22, 2020, 12:50pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/1 "2020-10-22T12:50:31Z")

</div>

seems like an implementation of `collect!()` is missing?

how could I do in-place collection like `collect!(v, range(1, 10, length=3))` then?

Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2020, 12:51pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/2 "2020-10-22T12:51:40Z")

</div>

`append!`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 22, 2020, 12:52pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/3 "2020-10-22T12:52:10Z")

</div>

Also, `copyto!` can be used for this

```julia
julia> copyto!(zeros(10), 1:10)
10-element Array{Float64,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 22, 2020, 1:24pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/4 "2020-10-22T13:24:35Z")

</div>

yes! `copyto!()` works (but not `append!()` that should sometimes allocate).

```julia
julia> v = randn(5);
julia> copyto!(v, range(1, 10, length = 5))
5-element Array{Float64,1}:
  1.0
  3.25
  5.5
  7.75
 10.0

julia> v
5-element Array{Float64,1}:
  1.0
  3.25
  5.5
  7.75
 10.0

julia> @btime copyto!($v, $range(1, 10, length = 5));
  122.753 ns (0 allocations: 0 bytes)

```

so, is it a confusion in function naming? why not rename `copyto!()` as `collect!()`?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2020, 1:39pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/5 "2020-10-22T13:39:52Z")

</div>

> [@tomtom](#):
>
> so, is it a confusion in function naming? why not rename `copyto!()` as `collect!()` ?

The confusion is on your part. `collect` will of course allocate a new vector, that’s its job. `append!` will have to allocate, as it appends to an existing one (but it is pretty smart about it). `copyto!` copies to an existing container, which you have to preallocate, with a known element type — very differently from `collect`, which figures out these things for you.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 22, 2020, 2:07pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/6 "2020-10-22T14:07:38Z")

</div>

I’d use broadcasted assignment here — simply `v .= range(1, 10, length=3)`.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 22, 2020, 2:29pm UTC](https://discourse.julialang.org/t/no-collect-function/48815/7 "2020-10-22T14:29:30Z")

</div>

Understand now. Thanks.
