Rust reference assignment


#1

Hello,
I’m very new to Rust and I have some doubts about assigning a value to a mutable references,
Is there a way to assign a value to something like a &mut var without having to dereference var (*var = ...).
I’ve made a small code to illustrate my problem: https://play.rust-lang.org/?gist=605835a5480bc363b0675a0bd677f39f&version=stable&mode=debug

Basically, I want to point to something inside a vector and expose, to the API user, a struct that looks like a normal struct and not a reference. In other languages you would override the assignment operator, but in Rust there is no such thing (as far as I can tell), I wonder if there is some other language feature that achieve something similar.


#2

You are right. There is no such thing in Rust.

For normal methods, autoderef takes care of it, but you can’t override assignment.


#3

There is no way to change the behavior of x.y = in rust.

Just to let you know, though, there is a safe way to do what you’re doing in to_tuple and to_struct: [_]::split_at_mut

Your code, without manual unsafe: https://play.rust-lang.org/?gist=6665244220a5e6470089682bfcaf236b&version=stable&mode=debug&edition=2015


#4

That is much better, thanks. It also gives the exact same instructions as the unsafe approach.