use std::alloc::{alloc, dealloc, Layout}; use std::ptr; fn main() { unsafe { let layout = Layout::new::<i32>(); let ptr = alloc(layout) as *mut i32; if ptr.is_null() { panic!("メモリ確保に失敗しました"); } *ptr = 42; println!("ptrが指す値: {}", *ptr); dealloc(ptr as *mut u8, layout); } }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.83s
Running `target/debug/memory`
ptrが指す値: 42