*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit fa6b5f98 authored by Rebola Pardo, Adrian's avatar Rebola Pardo, Adrian
Browse files

added pointer dereference example

parent 1bfd7354
No related branches found
No related tags found
No related merge requests found
[package]
name = "ptr-deref"
version = "0.1.0"
edition = "2024"
[dependencies]
fn main() {
// First create a `Vec`
let mut vec = Vec::from([100, 101, 102]);
// Let's take a raw pointer to the first item
let ptr0 = &raw mut vec[0];
// Also a raw pointer to the second item
let ptr1 = unsafe { ptr0.add(1) };
// Here I am implicitly taking a mutable reference to `vec`
vec[0] = 300;
// Unsafe but valid: no other reference at this point refers to
// memory overlapping *ptr1.
unsafe { *ptr1 = 301 };
// Valid: there is no mutable reference to memory owned by `vec`.
println!("OUTPUT: {:?}", &vec)
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment