|
12 | 12 |
|
13 | 13 | //! Raw, unsafe pointers, `*const T`, and `*mut T`
|
14 | 14 | //!
|
15 |
| -//! Working with raw pointers in Rust is uncommon, |
16 |
| -//! typically limited to a few patterns. |
17 |
| -//! |
18 |
| -//! Use the `null` function to create null pointers, and the `is_null` method |
19 |
| -//! of the `*const T` type to check for null. The `*const T` type also defines |
20 |
| -//! the `offset` method, for pointer math. |
21 |
| -//! |
22 |
| -//! # Common ways to create raw pointers |
23 |
| -//! |
24 |
| -//! ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`). |
25 |
| -//! |
26 |
| -//! ``` |
27 |
| -//! let my_num: i32 = 10; |
28 |
| -//! let my_num_ptr: *const i32 = &my_num; |
29 |
| -//! let mut my_speed: i32 = 88; |
30 |
| -//! let my_speed_ptr: *mut i32 = &mut my_speed; |
31 |
| -//! ``` |
32 |
| -//! |
33 |
| -//! To get a pointer to a boxed value, dereference the box: |
34 |
| -//! |
35 |
| -//! ``` |
36 |
| -//! let my_num: Box<i32> = Box::new(10); |
37 |
| -//! let my_num_ptr: *const i32 = &*my_num; |
38 |
| -//! let mut my_speed: Box<i32> = Box::new(88); |
39 |
| -//! let my_speed_ptr: *mut i32 = &mut *my_speed; |
40 |
| -//! ``` |
41 |
| -//! |
42 |
| -//! This does not take ownership of the original allocation |
43 |
| -//! and requires no resource management later, |
44 |
| -//! but you must not use the pointer after its lifetime. |
45 |
| -//! |
46 |
| -//! ## 2. Consume a box (`Box<T>`). |
47 |
| -//! |
48 |
| -//! The `into_raw` function consumes a box and returns |
49 |
| -//! the raw pointer. It doesn't destroy `T` or deallocate any memory. |
50 |
| -//! |
51 |
| -//! ``` |
52 |
| -//! # #![feature(box_raw)] |
53 |
| -//! let my_speed: Box<i32> = Box::new(88); |
54 |
| -//! let my_speed: *mut i32 = Box::into_raw(my_speed); |
55 |
| -//! |
56 |
| -//! // By taking ownership of the original `Box<T>` though |
57 |
| -//! // we are obligated to put it together later to be destroyed. |
58 |
| -//! unsafe { |
59 |
| -//! drop(Box::from_raw(my_speed)); |
60 |
| -//! } |
61 |
| -//! ``` |
62 |
| -//! |
63 |
| -//! Note that here the call to `drop` is for clarity - it indicates |
64 |
| -//! that we are done with the given value and it should be destroyed. |
65 |
| -//! |
66 |
| -//! ## 3. Get it from C. |
67 |
| -//! |
68 |
| -//! ``` |
69 |
| -//! # #![feature(libc)] |
70 |
| -//! extern crate libc; |
71 |
| -//! |
72 |
| -//! use std::mem; |
73 |
| -//! |
74 |
| -//! fn main() { |
75 |
| -//! unsafe { |
76 |
| -//! let my_num: *mut i32 = libc::malloc(mem::size_of::<i32>() as libc::size_t) as *mut i32; |
77 |
| -//! if my_num.is_null() { |
78 |
| -//! panic!("failed to allocate memory"); |
79 |
| -//! } |
80 |
| -//! libc::free(my_num as *mut libc::c_void); |
81 |
| -//! } |
82 |
| -//! } |
83 |
| -//! ``` |
84 |
| -//! |
85 |
| -//! Usually you wouldn't literally use `malloc` and `free` from Rust, |
86 |
| -//! but C APIs hand out a lot of pointers generally, so are a common source |
87 |
| -//! of raw pointers in Rust. |
| 15 | +//! *[See also the pointer primitive types](../primitive.pointer.html).* |
88 | 16 |
|
89 | 17 | #![stable(feature = "rust1", since = "1.0.0")]
|
90 |
| -#![doc(primitive = "pointer")] |
91 | 18 |
|
92 | 19 | use mem;
|
93 | 20 | use clone::Clone;
|
|
0 commit comments