Rust reference assignment

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: Rust Playground

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.

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

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

2 Likes

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: Rust Playground

1 Like

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