3 ms·
> In Rust it's also possible to copy values as a way to silence borrow checking errors, but this does add visual noise as copies have to be explicitly specified
by kd5bjo 7y ago
> In Rust it's also possible to copy values as a way to silence borrow checking errors, but this does add visual noise as copies have to be explicitly specified.
This statement isn't quite accurate. Types that can be safely duplicated by memcopy generally implement the Copy trait, which allows the compiler to do the same kind of silent copying that the author describes for Swift.
Cloning, on the other hand, must be explicitly specified; the difference here is that it may involve some amount of computation to make the logical copy in addition to simply copying the bytes.
- csomar 7y ago> Value types, by contrast, are stack-allocated in general, and their memory is managed statically. However, the behavior of value types in Swift is much different to how Rust handles memory. Yep. The only difference in that article is that it says that they are stack-allocated in general. I would be more interested to know the difference between what can be copy in Rust vs. the a Value type is swift.
- pornel 7y agoIn Rust user-defined types are copyable only if they explicitly opt-in. Even trivial tiny structs can't be copied unless they have `derive(Copy, Clone)` in their declaration. This is very helpful, because you know you can't copy something expensive by accident. It comes handy in libraries. Zero-sized non-copyable objects with destructors can be used as lock guards/handles that control exclusive access to a shared resource.
- twic 7y ago> In Rust user-defined types are copyable only if they explicitly opt-in. Even trivial tiny structs can't be copied unless they have `derive(Copy, Clone)` in their declaration. This is true, but it's only half the story, and i think it's the less interesting half. As you can say, you can make a type copy by asking the compiler to derive(Copy). But the compiler will only do this if all the fields in the struct are themselves Copy [1]. Primitives (integers, floats, booleans, chars) are Copy; shared (aka immutable) references are copy; tuples and arrays containing only Copy types are Copy. Structs and enums declared to be Copy are Copy (as long as they only contain Copy fields). But exclusive (aka mutable) references are not copy: Rust's safety guarantees depend on there only being one instance of an exclusive pointer to something (hence the name). Unique, which wraps a raw pointer, is also not Copy, because the whole point of it is to mark a pointer as unique. Unique underlies Box, Vec, and String, so those are also not Copy. So, user-defined types are copyable only if they explicitly opt-in and it is actually safe to copy them. As an aside, smart pointer types like Rc are also not Copy, but not for structural reasons: the one field in a Rc is Copy. But Rc itself being Copy would defeat the object of actually counting references, so it is not declared Copy. [1] https://doc.rust-lang.org/std/marker/trait.Copy.html#when-can-my-type-be-copy https://doc.rust-lang.org/std/marker/trait.Copy.html#when-ca...