5 ms·
If you don't mind, I'm curious about what "partially move structs" are? Structs have always feel like a chore to me, complicating things more when compared to m
by Scarbutt 7y ago
If you don't mind, I'm curious about what "partially move structs" are? Structs have always feel like a chore to me, complicating things more when compared to maps in dynamic langs. Maybe Rust has something for dealing with the "explosion of types" issue and dealing with partial information in structs that I don't know about. Skimming through the rust book looks like Option<T> is the solution to missing data in structs.
- coder543 7y agoPartially moved just means that the compiler tracks which fields have been “moved out” of the struct and prevents you from either accessing those fields or attempting to reuse the partially moved struct as if it were still a whole struct, since it is no longer whole... it has given up some of its members, conceptually. In the article linked by this discussion, the author specifically mentions how fields can’t be moved out in D’s implementation. Option is the correct approach for dealing with information that might be missing, but that’s not really related to partially moved structs. Structs document what is available in a given value, to both the compiler and to you. If that’s not convincing, then I’m not going to try more here... it’s off topic. But I will say you can use maps in Rust or other statically typed languages. They’re just not meant to be an alternative to well-defined structs.
- Scarbutt 7y agoSure, my intention was not to get in a dynamic vs static lang argument, when seeing "partial moved structs" and "ergonomics" in the same sentence my mind jump to think that there is something Rust that can make it feel closer to a dynamic lang regarding types ;) Thanks.
- didibus 7y agoCan Rust now handle partial move semantics on structs? I remember reading it couldn't do it before, and there were threads about how you should make small structs because of that, or that you should not use methods, because they take ownership of the full struct and not just the fields that they actually use, etc.
- littlestymaar 7y agoIt can, with the big caveat that it only works well if you don't cross a function boundary.
- didibus 7y agoAh, so passing a struct as an argument to a function will still take ownership or borrow the entire struct?
- WalterBright 7y agoThere are no plans at the moment for D to implement partial struct moving.
- didibus 7y agoRust doesn't have anything for that. Partial move means that if a function only really need to access one field on the struct, it would only take ownership of that field, even though it took the full struct as an argument. So two functions could have ownership of different fields of the struct at the same time. One thing with Rust is that it is really focused on performance and safety. It also cares about ergonomics, but not over performance and safety. Unfortunately, type explosion does make for faster code that can be validated more easily for safety by the compiler. That said, Rust has good support for vectors, those are used a lot, but are homogeneous. It also supports tuples with nice literal syntax and pattern matching on them. While it favours structs over maps, they are pretty lightweight compared to objects, so very small syntax to define them, you can create one out of another, and have a nice literal syntax to create them. Still, this is not the magical world of dynamic data-structures, so don't try and think you can just map/reduce over your structs and tuples, compose and restructure them freely, print out their content, or merge and split them, etc..
- eternalban 7y agoSo the only thing that is "moving" is the ownership of elements of a structure. It seems "partial move" actually means "partial transfer".
- didibus 7y ago> So the only thing that is "moving" is the ownership of elements of a structure. Yup In rust, you can move, copy or borrow a value. Move - means that the responsibility of freeing the allocated memory is moving to someone else. Rust says the ownership has moved. Copy - means that the full value is copied, and a copy is passed, thus now you have two instances of the value, and each can act independently. Borrow - means that the access permissions to the value are temporarily transferred to something else, but will come back when done, and the responsibility of freeing the memory is still up to the owner (lender). That's why it is called "partial move", cause it's using Rust's terminology. Transfer isn't in the Rust lexicon, so it wouldn't be as appropriate.