# Is My Highly Unsafe Code Correct? In Place Mapping a Vector

**URL:** <https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078>\
**Category:** code review\
**Created:** [June 26, 2023, 6:35pm UTC](https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078 "2023-06-26T18:35:28Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![geo-ant](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geo-ant/32/20737_2.png) [@geo-ant](https://users.rust-lang.org/u/geo-ant)\
**Post date:** [July 20, 2023, 8:37pm UTC](https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078/21 "2023-07-20T20:37:04Z")

</div>

Thanks to both of you! The explanation about the difference between [T] and &[T] was very helpful. I forgot at some point that those unsized types existed and in my mind &[T] was the slice, hence my confusion.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@H2CO3](https://users.rust-lang.org/u/H2CO3)\
**Post date:** [July 21, 2023, 5:49am UTC](https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078/22 "2023-07-21T05:49:33Z")

</div>

The confusion really was that you thought a `&mut [T]` would coerce to a `*mut T`. The slice type `[T]` is just a regular type; you can have a reference or a pointer to it, there's no "array to pointer decay" going on as in C. A reference-to-slice doesn't automatically become a pointer-to-element, so it doesn't lose information.

This "information" here is of course purely type-level; the numeric value of the pointer to the slice is exactly the same as a pointer to its first element, but whether it has type `T` or `[T]` matters a lot, because the function will drop whatever type it gets. If it gets a pointer to a slice, it will drop the whole slice. If it gets a pointer to an element, it will drop that element. There's no magic going on, this is exactly what you should expect.

---

<div class="post-metadata">

**Author:** ![geo-ant](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geo-ant/32/20737_2.png) [@geo-ant](https://users.rust-lang.org/u/geo-ant)\
**Post date:** [July 21, 2023, 3:11pm UTC](https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078/23 "2023-07-21T15:11:27Z")

</div>

Yes, precisely. However, I disagree with your last sentence. I've been working in C++ again lately, so a type system that actually cares about types _does_ feel like magic 🤣

---

<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:** [October 19, 2023, 3:12pm UTC](https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078/24 "2023-10-19T15:12:17Z")

</div>

Closed on October 19, 2023, 3:12pm 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.

[Previous page](https://users.rust-lang.org/t/is-my-highly-unsafe-code-correct-in-place-mapping-a-vector/96078.md?page=1)
