4 ms·
The author talks about adding structure, and I've been puzzled about what they are talking about until realizing that they apparently mean that a reference to s
by pflanze 3y ago
The author talks about adding structure, and I've been puzzled about what they are talking about until realizing that they apparently mean that a reference to something is "more" than the something hence it "adds structure". That doesn't make sense to me. More structure is if there are more elements in the data (more fields in a struct, or more alternatives in an enum), whereas a reference is just an indirection of access.
Or am I missing something?
- clord 3y ago&T, &mut T, and T are all different types in a sense. Hence ref is transmuting types during restructuring. Really, they are adding a new view, so perhaps this is like Haskell lenses, where you can make views with a different type that acts on underlying data
- fkcgnad 3y ago[dead]
- bobbylarrybobby 3y agoI think it's “more” in the same sense that Some(x) is more than x. You can destructure &x in exactly the same way.
- cstrahan 3y agoThe set of all &T is larger than the set of T. Let’s consider the case where T is u8. There are 256 inhabitants of the u8 type (0u8-255u8). Now consider &u8. Each u8 referent is still some value between 0 and 255, but the number of references you could have is dependent on the pointer width of your target system — let’s say 64 bits. So that’s 2^8 possible u8 values times 2^64 possible references, or 2^72 inhabitants. Well, minus one to account for null, and I’m probably missing other details, but you get the idea. So going from T to &T introduces structure in the same sense that going from T to (T, U) does; and going from &T to T destructures in the same sense that going from (T, U) to T does. That structure may not be directly observable (as in the case of a reference: you’d have to go out of your way to observe the underlying address), but conceptually the structure is there nonetheless. Edit: Another way to look at it is this: Consider a Rust “newtype”, like struct Days(u8). If you pattern match on that to obtain the inner u8, we call that destructuring. Destructuring is to reduce or break down the structure of a thing, so the opposite must be building up structure, or “restructuring”. Therefore going in the opposite direction — stuffing a u8 value in a Days - must be restructuring. In this case both types have the same number of inhabitants (256); regardless, one direction is destructuring and the other direction is restructuring.