You are not logged in.

#1 2025-07-07 17:57:05

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 352

So when multi reference pointers is useful ?

I'm currently learning Rust, and as far as I know,  it seems that the language just try to not make you able to do multiple reference with many pointers, like for example:

 pointer3 --> pointer2 --> pointer1 --> variable -> 10 

This kind of relationship which is possible in C, and assuming also in c++ [not sure if it's a good practice in C++ btw, since smart pointers and all that stuff] and assuming my current knowledge of Rust, is not possible, Rust tries to just let you create many immutable  reference or just only one mutable one, which in the case of the last one, let's you modify the variable that the pointer reference.

So this kind of imposed behavior that Rust tries to make in your programming, made me think: In which cases this multiple reference that C and c++ have is useful ?

So I'm wanting for your big brain comments here smile

btw please I know that some people believe that Rust is the superior low level language and all that nonsense, so keep focus on the big brain part only please tongue

Offline

#2 2025-07-07 18:25:35

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,305
Website

Re: So when multi reference pointers is useful ?

As a Rustacean, I'd argue that multiple mutable references to the same owned value are never useful, since they enable race conditions and other UB.
I dare say that your comparison to C++ may be misguided. Rust does not aim to be another copy of C++ or any other language. It tries to provide different tools and semantics to make systems programming safe, even fool proof, by default. Therefore potentially unsafe operations require an explicit opt out of Rust's safety features.
If you have a concrete issue you're trying to solve in Rust using C-esque semantics, you're probably falling for an xy problem. In Rust you sometimes need to approach solutions differently.


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3 2025-07-07 18:35:23

unixman
Member
Registered: 2015-03-12
Posts: 115

Re: So when multi reference pointers is useful ?

for C case:

its just a syntactic sugar to cut down typing
and to see all ancestors at single shot (its good think)
and a little flexibility for compiler to optimize a little bit.

for rust case:
i am not rust people:)

Offline

#4 2025-07-07 19:33:58

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 352

Re: So when multi reference pointers is useful ?

schard wrote:

As a Rustacean, I'd argue that multiple mutable references to the same owned value are never useful, since they enable race conditions and other UB.
I dare say that your comparison to C++ may be misguided. Rust does not aim to be another copy of C++ or any other language. It tries to provide different tools and semantics to make systems programming safe, even fool proof, by default. Therefore potentially unsafe operations require an explicit opt out of Rust's safety features.
If you have a concrete issue you're trying to solve in Rust using C-esque semantics, you're probably falling for an xy problem. In Rust you sometimes need to approach solutions differently.

This is something that I'm thinking right now, I mean something very similar as you said with the C++ comparison in my mind, but I think I was not so clear in my writing tongue.

I see Rust as you said, just another different tool which in some specific use cases could shine more, but not as a replacement of c and c++ . But since I get that Rust avoids to have multiple pointers reference, it's where c and c++ would shine more.  As for me, the code that had been written in Rust is more like keeping in mind [all the time] where are the pointers are, so I don't make the mistake of using the borrow of the memory address without noticing it.

In C [and it seems c++ also] it's more comfy, you just get the segmentation fault at the end if you don't notice it  lol , I mean the memory addressing is more relax because it's fully up to you, when in the case of Rust you have the compiler checking you that for you, because it's force you to use the memory addressing in the Rust way, which is just one thing can change the value in a reference, not two or more.

I imagine that you can create that using the unsafe word in your code block, and using the C api also, but I believe that using that destroys all the main propose of Rust. So maybe I'm wrong with this, but if i start to use the unsafe mode of Rust, then I just go back to c++.

I imagine that this creates the "need to approach solutions diffrently" smile

Last edited by Succulent of your garden (2025-07-07 19:38:33)

Offline

#5 2025-07-07 20:39:56

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,305
Website

Re: So when multi reference pointers is useful ?

Reading your last post, maybe you're confusing exclusive references and smart pointers. You can only ever have one exclusive reference to an owned value in Rust.
But Rust also has smart pointers.


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#6 2025-07-08 13:01:20

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 352

Re: So when multi reference pointers is useful ?

Thanks Schard, I get it now smile

But do you think that the multiple reference is not useful in a general sense ? I'm trying to understand also in which cases could be useful, so I can polish more my low level programming skills tongue

Offline

#7 2025-07-08 14:01:17

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,305
Website

Re: So when multi reference pointers is useful ?

The fact that I cannot think of any use case does not mean that there isn't one.
Any use case I can think of, where I don't need concurrency will work with only one mutable reference and reborrowing.
Any use case I can think of, where I do need concurrency will potentially cause UB with multiple exclusive references to the same owned value, so I'd need some form of Mutex anyways.

Edit: reading your OP again, in Rust, you can have something like

let r = &mut &mut &mut [0u8; 32];

But I'd still wonder of what use this would be.

Last edited by schard (2025-07-08 14:03:19)


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#8 2025-07-08 16:49:21

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 352

Re: So when multi reference pointers is useful ?

Thanks schard for your feedback, I really appreciate it smile

As for me, I also can't see a potential use case, but it was nice to have the recent feedback of yours with more details  big_smile

Offline

Board footer

Powered by FluxBB