Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do raw pointers need the const keyword when references don't?

Tags:

rust

Please reference the code below, for the line which has been commented out. It cannot be compiled by the Rust compiler, meaning we need to specify the constness like *const i32 explicitly for a raw pointer in Rust. But for a reference that is immutable to its pointee, we don't need to specify the constness explicitly like with a raw pointer. So, why does Rust not use a more symmetrical way to express the same meaning for both references and raw pointers, since the reference is actually a pointer under the hood?

fn main() {
    let x1: &i32;
    let x2: &mut i32;
    // let p1: *i32;  // This line doesn't compile!!!
    let p1: *const i32;  // we need to specify the constness explicitly.
    let p2: *mut i32;
}
like image 519
Jason Yu Avatar asked Apr 18 '21 06:04

Jason Yu


People also ask

What is the point of const in C?

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change the value of const variable by using pointer ).

What is a raw pointer?

A raw pointer is a pointer whose lifetime isn't controlled by an encapsulating object, such as a smart pointer. A raw pointer can be assigned the address of another non-pointer variable, or it can be assigned a value of nullptr . A pointer that hasn't been assigned a value contains random data.

What is a raw pointer?

A raw pointer is a pointer whose lifetime is not controlled by an encapsulating object, such as a smart pointer. A raw pointer can be assigned the address of another non-pointer variable, or it can be assigned a value of nullptr. A pointer that hasn't been assigned a value contains random data.

How to declare a pointer with a const keyword in C++?

Const Keyword With Pointer Variables: Pointers can be declared with a const keyword. So, there are three possible ways to use a const keyword with a pointer, which are as follows: When the pointer variable point to a const value: Syntax: const data_type* var_name; Below is the C++ program to implement the above concept:

What does the const keyword DO in a reference variable?

The const keyword can be used in the declaration of a reference variable to restrict what can be done with it. There's only one possible use:

Can we make a pointer itself constant in C?

We can also make a pointer itself constant. A const pointer is a pointer whose value can not be changed after initialization To declare a const pointer, use the const keyword between the asterisk and the pointer name: Just like a normal const variable, a const pointer must be initialized to a value upon declaration.


1 Answers

Early versions of Rust (pre-1.0) used *T for const raw pointers (with the same syntax as you note does not work today). RFC 0068 renamed *T to *const T, with justification that it is more error-prone to use *T when transcribing type signatures from existing C code:

The current difference in Rust unsafe pointers types with C pointers types is proving to be too error prone to realistically enable these optimizations at a future date. By renaming Rust's unsafe pointers to closely match their C brethren, the likelihood for erroneously transcribing a signature is diminished.

The RFC also notes (as you do) that this introduces an inconsistency in naming between references and raw pointers:

Today's unsafe pointers design is consistent with the borrowed pointers types in Rust, using the mut qualifier for a mutable pointer, and no qualifier for an "immutable" pointer. Renaming the pointers would be divergence from this consistency, and would also introduce a keyword that is not used elsewhere in the language, const.

like image 199
tari Avatar answered Oct 01 '22 00:10

tari