# When does dereferencing a pointer create a copy?

**URL:** <https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955>\
**Category:** help\
**Created:** [November 25, 2020, 6:52pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955 "2020-11-25T18:52:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rectang](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rectang/32/18571_2.png) [@rectang](https://users.rust-lang.org/u/rectang)\
**Post date:** [November 25, 2020, 6:52pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955/1 "2020-11-25T18:52:40Z")

</div>

Why does `&mut (*pointer)` create a reference to the original memory behind `pointer`, while `&mut { *pointer }` creates a reference to a copy?

```rust
#[derive(Copy, Clone)]
struct MyStruct {
    num: u32,
}

fn main() {
    let mut my_struct = MyStruct { num: 42 };
    let pointer = &mut my_struct as *mut MyStruct;
    let ref_to_orig = unsafe { &mut (*pointer) };
    ref_to_orig.num += 100;
    println!("{}", my_struct.num); // prints "142"
    let ref_to_copy = unsafe { &mut { *pointer } };
    ref_to_copy.num += 100;
    println!("{}", my_struct.num); // still prints "142"
}

```

(Context: I need to understand this because I'm trying to work with Apple's CoreAudio C API, which uses callbacks. I need to write a callback in Rust conforming to a C API which takes a `void*` as an argument. The `void*` will be a pointer to a struct, whose fields the callback needs to modify. Updating the fields of a local copy would be pointless.)

([Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=2bf9e0b32a4a91c751840ea794c078cf))

---

<div class="post-metadata">

**Author:** ![jschievink](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jschievink/32/7453_2.png) [@jschievink](https://users.rust-lang.org/u/jschievink)\
**Post date:** [November 25, 2020, 7:53pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955/2 "2020-11-25T19:53:45Z")

</div>

`(*pointer)` is a _place expression_, while `{ *pointer }` is a _value expression_.

You can read more about their behavior in this case in the Rust reference: [Expressions - The Rust Reference](https://doc.rust-lang.org/reference/expressions.html#place-expressions-and-value-expressions)

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://users.rust-lang.org/u/jethrogb)\
**Post date:** [November 25, 2020, 7:56pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955/4 "2020-11-25T19:56:39Z")

</div>

Interestingly though, neither that page nor the page on [Grouped expressions](https://doc.rust-lang.org/stable/reference/expressions/grouped-expr.html) specify what type of expression a grouped expression is.

---

<div class="post-metadata">

**Author:** ![trentj](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/trentj/32/5690_2.png) [@trentj](https://users.rust-lang.org/u/trentj)\
**Post date:** [November 25, 2020, 8:04pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955/5 "2020-11-25T20:04:55Z")

</div>

It is neither (and both): `(expr)` can be either a place expression or a value expression, depending on what `expr` is.

`(*pointer)` is a place expression because `*pointer` is a place expression. `({*pointer})` would be a value expression. The parentheses do nothing but group.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [November 25, 2020, 9:09pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955/6 "2020-11-25T21:09:49Z")

</div>

You might also be interested in this article: [Stuff the Identity Function Does (in Rust)](https://bluss.github.io/rust/fun/2015/10/11/stuff-the-identity-function-does/#rust-has-dedicated-syntax-for-this)

---

<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:** [February 23, 2021, 9:09pm UTC](https://users.rust-lang.org/t/when-does-dereferencing-a-pointer-create-a-copy/51955/7 "2021-02-23T21:09:53Z")

</div>

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.
