9951 explained code solutions for 126 technologies


rustPointer cast example in Rust


Pointer casting in Rust is a way to convert a pointer of one type to a pointer of another type. This is done by using the as keyword. For example, to convert a pointer of type *const i32 to a pointer of type *mut i32, you can use the following code:

let ptr_const: *const i32 = &42;
let ptr_mut: *mut i32 = ptr_const as *mut i32;

Output example

No output example

Explanation

The as keyword is used to cast the pointer ptr_const of type *const i32 to a pointer of type *mut i32. This is done by assigning the result of the cast to the variable ptr_mut.

Relevant links

Rust Pointer Casting Rust Pointer Types Rust Pointer Safety

Edit this code on GitHub