Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify how-to-convert-pointer-to-reference-in-rust.md so the code compiles #2004

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions rust/how-to-convert-pointer-to-reference-in-rust.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# How to convert pointer to reference in Rust
// plain

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`.
```rust
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
- [Rust Pointers](https://doc.rust-lang.org/book/ch19-01-pointers.html)
- [Rust References](https://doc.rust-lang.org/book/ch15-01-box.html)
- [Rust Operators](https://doc.rust-lang.org/book/ch03-02-operators-and-overloading.html)

group: rust-pointers
// plain

In Rust, you can convert a pointer to a reference by dereferencing the pointer with `*`, then creating a reference with `&`. 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`.
```rust
let p: *const i32 = &10;

unsafe {
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 `*` in `&*p` is used to dereference `p`, then the `&` creates a reference to that location. This pointer to a reference of type `&i32`, which is a reference to an integer. The unsafe block is needed to dereference the raw pointer.

### Relevant links
- [Rust Pointers](https://doc.rust-lang.org/book/ch19-01-pointers.html)
- [Rust References](https://doc.rust-lang.org/book/ch15-01-box.html)
- [Rust Operators](https://doc.rust-lang.org/book/ch03-02-operators-and-overloading.html)

group: rust-pointers