this post was submitted on 22 Oct 2023
18 points (95.0% liked)

Rust

6044 readers
5 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS
 

I'm slowly starting Rust for Rustaceans, and it's already poking holes in my understanding of Rust. Here's a couple initial questions I have:

A shared reference, &T is , as the name implies, a pointer that may be shared. Any number of references may exist to the same value, and each shared reference is Copy, so you can trivially make more of them

I don't understand why a shared reference has to implement copy. In fact, isn't this not true just by the fact that references work for Strings and Strings size can't be known at compile time?

  1. I'm having trouble with the idea of assigning a new value to a mutable reference.

let mut x = Box::new(42); *x = 84;

Why in this example, is the assignment dereferenced. Why not just do x=84? is it dereferenced specifically because is Boxed on the heap?

you are viewing a single comment's thread
view the rest of the comments
[–] snaggen 11 points 1 year ago* (last edited 1 year ago) (1 children)

A reference &T holds a pointer, ie. the memory adress to the actual content of T

So, in the example x doesn't hold the value 42, it holds the memory adress to the memory there the integer value 42 is stored. So, to access the value, you need to dereference the reference. Which is why you need to use *x when you assign the value.

[–] snaggen 6 points 1 year ago (2 children)

And the Copy question.It is not that s reference has to implement Copy. A reference IS Copy, by the simple fact that it is a primitive value on the stack.

[–] KillTheMule 9 points 1 year ago* (last edited 1 year ago) (2 children)

A reference IS Copy, by the simple fact that it is a primitive value on the stack.

This seems a bit misleading, noting that unique/mutable references aren't Copy. Shared references are Copy because it's sound to have that, and it's a huge QOL improvement over the alternative.

[–] [email protected] 2 points 1 year ago

In this context mutability is part of the type signatures. &T and &mut T are two different types, the former implements copy but not the later. It's not really an "exception" in the type system.

[–] Anders429 1 points 1 year ago (1 children)

I wouldn't say it's misleading. The question was specifically about shared references, it seemed obvious to me that's what they were referring to in their answer.

[–] KillTheMule 2 points 1 year ago

The question was specifically about shared references

Sure, but the way I read the answer was "All primitive values on the stack are Copy", which isn't true (my example being mutable references, which have the same representation as shared ones, "just" a different semantic meaning). That's what I meant by misleading.

[–] nerdblood 1 points 1 year ago

I think where I was getting hung up here as I was still thinking of the value of the reference rather than the reference itself. That's why I brought up Strings, it didn't make sense that String would automatically implement Copy since they go on the heap. But I see now we're talking about the pointer reference TO that value.

Thanks!