# How to implement Vec extend\_from\_within

**URL:** <https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177>\
**Category:** help\
**Created:** [May 19, 2026, 10:58am UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177 "2026-05-19T10:58:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![geebee22](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geebee22/32/16527_2.png) [@geebee22](https://users.rust-lang.org/u/geebee22)\
**Post date:** [May 19, 2026, 10:58am UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177/1 "2026-05-19T10:58:39Z")

</div>

I was wondering today how I might improve my [current implementation](https://docs.rs/pstd/1.0.134/pstd/vec/struct.VecA.html#method.extend_from_within), which just has a loop cloning and pushing elements. Especially, I was thinking about using some kind of low-level byte copy where the element type is Copy and not just Clone. If there a way to test if T is Copy and do it one way, otherwise do it another way?

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [May 19, 2026, 11:17am UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177/2 "2026-05-19T11:17:32Z")

</div>

> [@geebee22](#):
>
> If there a way to test if T is Copy and do it one way, otherwise do it another way?

that is called specialization. that is [what `std` does](https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#3530).  
you cannot do that in stable code.

ignoring specialization, your implementation could potentially be improved by doing something like [what std does in the non-Copy case](https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#3672)

---

<div class="post-metadata">

**Author:** ![geebee22](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geebee22/32/16527_2.png) [@geebee22](https://users.rust-lang.org/u/geebee22)\
**Post date:** [May 19, 2026, 12:14pm UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177/3 "2026-05-19T12:14:03Z")

</div>

Ah, thanks.

Something that seemed odd to me, [it uses](https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#3710) [`ptr::copy_nonoverlapping`](https://doc.rust-lang.org/std/ptr/fn.copy_nonoverlapping.html)`.`

But I think the source and destination can overlap. Maybe it somehow "knows" that doesn't matter? If so the comments don't make that clear.

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [May 19, 2026, 12:30pm UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177/4 "2026-05-19T12:30:20Z")

</div>

> [@geebee22](#):
>
> But I think the source and destination can overlap

they cannot overlap. the source is a subsection of the already initialized part of the vector, while the destination is the start of the uninitialized part of the vector.

---

<div class="post-metadata">

**Author:** ![geebee22](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geebee22/32/16527_2.png) [@geebee22](https://users.rust-lang.org/u/geebee22)\
**Post date:** [May 19, 2026, 12:48pm UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177/5 "2026-05-19T12:48:11Z")

</div>

Ah, yes, I was thinking I might be able to use it in a situation where there is overlap, but it doesn't support that.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 17, 2026, 12:49pm UTC](https://users.rust-lang.org/t/how-to-implement-vec-extend-from-within/140177/6 "2026-08-17T12:49:06Z")

</div>

Closed on August 17, 2026, 12:49pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
