rustHow to convert pointer to reference in Rust
In Rust, you can convert a pointer to a reference by using the & operator. For example, if you have a pointer p to a type T, you can convert it to a reference by using &p. This will create a reference to the same value that the pointer points to. The output of this operation will be a reference of type &T.
let p: *const i32 = &10;
let r: &i32 = &p;
Output example
This code will not produce any output.
Explanation
The & operator is used to convert a pointer to a reference. In this example, the pointer p is of type *const i32, which is a pointer to a constant integer. The & operator is used to convert this pointer to a reference of type &i32, which is a reference to an integer.
Relevant links
Related
- How to get pointer to variable in Rust
- How to get size of pointer in Rust
- How to do pointer write in Rust
- How to print pointer in Rust
- How to get address of pointer in Rust
- How to get pointer of object in Rust
- How to increment pointer in Rust
- How to cast pointer to usize in Rust
- How to get pointer to struct in Rust
- Pointer to array element in Rust
More of Rust
- How to use regex to match a double quote in Rust?
- How to map a Rust slice?
- How to use regex to match a group in Rust?
- How to perform matrix operations in Rust?
- How to replace strings using Rust regex?
- How to use regex lookbehind in Rust?
- Bitwise operator example in Rust
- Regex example to match multiline string in Rust?
- How to insert an element into a Rust HashMap if it does not already exist?
- Pointer arithmetic example in Rust
See more codes...