dereferencing raw ptr
Tutorial Video
Text description (accessibility)
This video demonstrates the "dereferencing raw ptr" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. This example covers a specific aspect of Rust's unsafe programming model: raw memory manipulation, FFI interop, allocator customization, or soundness principles. Key difference from OCaml: 1. **Safety model**: Rust requires explicit `unsafe` for these operations; OCaml achieves safety through the GC and type system without explicit unsafe regions.
Tutorial
The Problem
This example covers a specific aspect of Rust's unsafe programming model: raw memory manipulation, FFI interop, allocator customization, or soundness principles. These topics are essential for systems programming — writing OS components, device drivers, game engines, and any code that must interact with C libraries or control memory layout precisely. Rust's unsafe system is designed to confine unsafety to small, auditable regions while maintaining safety in the surrounding code.
🎯 Learning Outcomes
Code Example
#![allow(clippy::all)]
//! # Dereferencing Raw Pointers
pub fn raw_ptr_example() -> i32 {
let x = 42;
let ptr = &x as *const i32;
unsafe { *ptr }
}
pub fn mutable_raw_ptr() -> i32 {
let mut x = 10;
let ptr = &mut x as *mut i32;
unsafe {
*ptr += 5;
*ptr
}
}
pub fn ptr_from_address(addr: usize) -> *const i32 {
addr as *const i32
}
pub fn null_check(ptr: *const i32) -> Option<i32> {
if ptr.is_null() {
None
} else {
unsafe { Some(*ptr) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_raw() {
assert_eq!(raw_ptr_example(), 42);
}
#[test]
fn test_mut() {
assert_eq!(mutable_raw_ptr(), 15);
}
#[test]
fn test_null() {
let x = 5;
assert_eq!(null_check(&x), Some(5));
assert_eq!(null_check(std::ptr::null()), None);
}
}Key Differences
unsafe for these operations; OCaml achieves safety through the GC and type system without explicit unsafe regions.extern "C"; OCaml uses ctypes which wraps C types in OCaml values.#[repr(C)], custom allocators); OCaml's GC manages memory layout automatically.OCaml Approach
OCaml's GC and type system eliminate most of the need for these unsafe operations. The equivalent functionality typically uses:
ctypes library for external function callsBigarray for controlled raw memory access Bytes.t for mutable byte sequencesOCaml programs rarely need operations equivalent to these Rust unsafe patterns.
Full Source
#![allow(clippy::all)]
//! # Dereferencing Raw Pointers
pub fn raw_ptr_example() -> i32 {
let x = 42;
let ptr = &x as *const i32;
unsafe { *ptr }
}
pub fn mutable_raw_ptr() -> i32 {
let mut x = 10;
let ptr = &mut x as *mut i32;
unsafe {
*ptr += 5;
*ptr
}
}
pub fn ptr_from_address(addr: usize) -> *const i32 {
addr as *const i32
}
pub fn null_check(ptr: *const i32) -> Option<i32> {
if ptr.is_null() {
None
} else {
unsafe { Some(*ptr) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_raw() {
assert_eq!(raw_ptr_example(), 42);
}
#[test]
fn test_mut() {
assert_eq!(mutable_raw_ptr(), 15);
}
#[test]
fn test_null() {
let x = 5;
assert_eq!(null_check(&x), Some(5));
assert_eq!(null_check(std::ptr::null()), None);
}
}#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_raw() {
assert_eq!(raw_ptr_example(), 42);
}
#[test]
fn test_mut() {
assert_eq!(mutable_raw_ptr(), 15);
}
#[test]
fn test_null() {
let x = 5;
assert_eq!(null_check(&x), Some(5));
assert_eq!(null_check(std::ptr::null()), None);
}
}
Deep Comparison
Dereferencing Raw Pointers
See example files for OCaml vs Rust comparison.
Exercises
bytemuck for transmute, CString for FFI strings) and implement it.