Blog post: View types for Rust

This is a very natural extension of the language IMO, in line with rough ideas I’ve head myself, too. I like the part discussing that it gives explicit syntax to language features that the borrow checker (within a function) already offers.

The example code in the blog post seems to suggest that (when not using method-call syntax) you’d use an explicit expression-syntax to select which fields to view when creating a reference. I’d assume that the necessary view type could actually usually be inferred, extending the meaning of &EXPR and &mut EXPR.

Something like

let x = Foo { bar, baz };
let ref_1 = &mut x;
let ref_2 = &mut x;
ref_1.bar += 1;
ref_2.baz += 1;

would then probably also start to compile (because the compiler could infer ref_1 to be &mut {bar} Foo, etc..), but is that a bad thing?

16 Likes