0

What is the difference between (a) and (b) in the following

let a: Box<i32> = Box::new(1); // (a)
let mut a: Box<i32> = Box::new(1); // (b)
a = Box::new(2); // (c)
*a = 3; // (d)

(a) is equivalent to the following in C++

int const * const a = new int(1);

and (b) is equivalent to

int * a = new int(1);

In Rust, is there anything equivalent to

int const * a = new int(1);  // this allows (c) but not (d)

or

int * const a = new int(1);  // this allows (d) but not (c)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jihyun
  • 883
  • 5
  • 17
  • 2
    Your question might be answered by the answers of [What's the difference between placing “mut” before a variable name and after the “:”?](https://stackoverflow.com/q/28587698/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 22 '20 at 17:45
  • 1
    It's not super clear what you are asking here — the one with `mut` is mutable, the other isn't. You may also be asking multiple questions at once; it's hard to answer multiple questions made in one post. Please separate them into multiple questions so that we can help you better and so that your questions will help others in the future that have one of the same questions as you! – Shepmaster Dec 22 '20 at 17:48
  • https://users.rust-lang.org/t/why-are-we-able-to-change-the-content-of-box/2125 might answer your question – smed Dec 22 '20 at 18:14
  • 4
    The things that you say are equivalent aren't really. Raw pointers work almost 100% the same way in Rust and C++. A closer analogy to `Box` would be `std::unique_ptr` (although there is no exact equivalent, because C++ smart pointers and references can always be aliased) – trent Dec 22 '20 at 18:18
  • See also [Why does Rust allow mutation through a reference field using an immutable binding?](https://stackoverflow.com/q/50124680/155423); [How can I force a struct's field to always be immutable in Rust?](https://stackoverflow.com/q/23743566/155423); [Do a container's members inherit its mutability?](https://stackoverflow.com/q/47915905/155423); [Difference in mutability between reference and box](https://stackoverflow.com/q/31567708/155423); – Shepmaster Dec 22 '20 at 18:28

1 Answers1

2

In Rust, is there anything equivalent to

Not using Box (or most other smart pointers) because it's completely useless. Box is a unique pointer, there is no functional / observable difference between (c) and (d).

You can do this using references though: you can have a mut x: &T, a mut x: &mut T, a x: &T, or a x: &mut T.

#[allow(unused_assignments)]
fn foo(mut v1; u8, mut v2: u8) {
    let a = &mut v1;
    a = &mut v2; // doesn't compile, `a` is an immutable binding
    *a = v2; // compiles, `a` is bound to a mutable reference
    
    let mut b = &v1;
    b = &v2; // compiles, `b` is a mutable binding
    *b = v2; // doesn't compile, `b` binds is bound to an immutable reference
}
Masklinn
  • 34,759
  • 3
  • 38
  • 57