fn main() {
// Structs allow moving fields individually
struct Struct {
a: String,
b: String,
}
let the_struct = Struct {
a: "hello".into(),
b: "hi".into(),
};
// both field access and destructuring only move the referenced fields
drop(the_struct.a);
let Struct { b, .. } = the_struct;
drop(b);
// Enums will be moved as a whole
enum Enum {
A(String),
B(String),
}
let the_enum = Enum::A("hello".into());
// IMO only A should be moved
if let Enum::A(a) = the_enum {
drop(a);
}
// Do something that makes match inconvinient
// B variant cannot be accessed
if let Enum::B(b) = the_enum {}
}
I think this is a known limitation in the current borrow checker, it’s not as much about partial borrows but following control flow and reasoning about two different worlds (the one where the enum was an A, vs the one where it was a B). The original borrock was strictly lexical (based on literal textual scopes), and the current one is more permissive but still forbids a lot of valid code. But the next generation has been in the works for a while.
2 Likes