<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `ptr` mod in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, ptr"><title>std::ptr - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize1.35.0.css"><link rel="stylesheet" type="text/css" href="../../rustdoc1.35.0.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark1.35.0.css"><link rel="stylesheet" type="text/css" href="../../light1.35.0.css" id="themeStyle"><script src="../../storage1.35.0.js"></script><noscript><link rel="stylesheet" href="../../noscript1.35.0.css"></noscript><link rel="shortcut icon" href="../../favicon1.35.0.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow1.35.0.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../std/index.html'><img src='../../rust-logo1.35.0.png' alt='logo' width='100'></a><p class='location'>Module ptr</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'ptr', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush1.35.0.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme1.35.0.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel1.35.0.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/core/lib.rs.html#174' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>std</a>::<wbr><a class="mod" href=''>ptr</a></span></h1><div class='docblock'><p>Manually manage memory through raw pointers.</p> <p><em><a href="../../std/primitive.pointer.html">See also the pointer primitive types</a>.</em></p> <h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1> <p>Many functions in this module take raw pointers as arguments and read from or write to them. For this to be safe, these pointers must be <em>valid</em>. Whether a pointer is valid depends on the operation it is used for (read or write), and the extent of the memory that is accessed (i.e., how many bytes are read/written). Most functions use <code>*mut T</code> and <code>*const T</code> to access only a single value, in which case the documentation omits the size and implicitly assumes it to be <code>size_of::<T>()</code> bytes.</p> <p>The precise rules for validity are not determined yet. The guarantees that are provided at this point are very minimal:</p> <ul> <li>A <a href="./fn.null.html">null</a> pointer is <em>never</em> valid, not even for accesses of <a href="../../nomicon/exotic-sizes.html#zero-sized-types-zsts">size zero</a>.</li> <li>All pointers (except for the null pointer) are valid for all operations of <a href="../../nomicon/exotic-sizes.html#zero-sized-types-zsts">size zero</a>.</li> <li>All accesses performed by functions in this module are <em>non-atomic</em> in the sense of <a href="../../std/sync/atomic/index.html">atomic operations</a> used to synchronize between threads. This means it is undefined behavior to perform two concurrent accesses to the same location from different threads unless both accesses only read from memory. Notice that this explicitly includes <a href="./fn.read_volatile.html"><code>read_volatile</code></a> and <a href="./fn.write_volatile.html"><code>write_volatile</code></a>: Volatile accesses cannot be used for inter-thread synchronization.</li> <li>The result of casting a reference to a pointer is valid for as long as the underlying object is live and no reference (just raw pointers) is used to access the same memory.</li> </ul> <p>These axioms, along with careful use of <a href="../../std/primitive.pointer.html#method.offset"><code>offset</code></a> for pointer arithmetic, are enough to correctly implement many useful things in unsafe code. Stronger guarantees will be provided eventually, as the <a href="../../nomicon/aliasing.html">aliasing</a> rules are being determined. For more information, see the <a href="../../book/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer">book</a> as well as the section in the reference devoted to <a href="../../reference/behavior-considered-undefined.html">undefined behavior</a>.</p> <h2 id="alignment" class="section-header"><a href="#alignment">Alignment</a></h2> <p>Valid raw pointers as defined above are not necessarily properly aligned (where "proper" alignment is defined by the pointee type, i.e., <code>*const T</code> must be aligned to <code>mem::align_of::<T>()</code>). However, most functions require their arguments to be properly aligned, and will explicitly state this requirement in their documentation. Notable exceptions to this are <a href="./fn.read_unaligned.html"><code>read_unaligned</code></a> and <a href="./fn.write_unaligned.html"><code>write_unaligned</code></a>.</p> <p>When a function requires proper alignment, it does so even if the access has size 0, i.e., even if memory is not actually touched. Consider using <a href="./struct.NonNull.html#method.dangling"><code>NonNull::dangling</code></a> in such cases.</p> </div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2> <table><tr class='module-item'><td><a class="struct" href="struct.NonNull.html" title='std::ptr::NonNull struct'>NonNull</a></td><td class='docblock-short'><p><code>*mut T</code> but non-zero and covariant.</p> </td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2> <table><tr class='module-item'><td><a class="fn" href="fn.copy.html" title='std::ptr::copy fn'>copy</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Copies <code>count * size_of::<T>()</code> bytes from <code>src</code> to <code>dst</code>. The source and destination may overlap.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.copy_nonoverlapping.html" title='std::ptr::copy_nonoverlapping fn'>copy_nonoverlapping</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Copies <code>count * size_of::<T>()</code> bytes from <code>src</code> to <code>dst</code>. The source and destination must <em>not</em> overlap.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.drop_in_place.html" title='std::ptr::drop_in_place fn'>drop_in_place</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Executes the destructor (if any) of the pointed-to value.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.eq.html" title='std::ptr::eq fn'>eq</a></td><td class='docblock-short'><p>Compares raw pointers for equality.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.hash.html" title='std::ptr::hash fn'>hash</a></td><td class='docblock-short'><p>Hash a raw pointer.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.null.html" title='std::ptr::null fn'>null</a></td><td class='docblock-short'><p>Creates a null raw pointer.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.null_mut.html" title='std::ptr::null_mut fn'>null_mut</a></td><td class='docblock-short'><p>Creates a null mutable raw pointer.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.read.html" title='std::ptr::read fn'>read</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Reads the value from <code>src</code> without moving it. This leaves the memory in <code>src</code> unchanged.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.read_unaligned.html" title='std::ptr::read_unaligned fn'>read_unaligned</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Reads the value from <code>src</code> without moving it. This leaves the memory in <code>src</code> unchanged.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.read_volatile.html" title='std::ptr::read_volatile fn'>read_volatile</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Performs a volatile read of the value from <code>src</code> without moving it. This leaves the memory in <code>src</code> unchanged.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.replace.html" title='std::ptr::replace fn'>replace</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Moves <code>src</code> into the pointed <code>dst</code>, returning the previous <code>dst</code> value.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.swap.html" title='std::ptr::swap fn'>swap</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Swaps the values at two mutable locations of the same type, without deinitializing either.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.swap_nonoverlapping.html" title='std::ptr::swap_nonoverlapping fn'>swap_nonoverlapping</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Swaps <code>count * size_of::<T>()</code> bytes between the two regions of memory beginning at <code>x</code> and <code>y</code>. The two regions must <em>not</em> overlap.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.write.html" title='std::ptr::write fn'>write</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Overwrites a memory location with the given value without reading or dropping the old value.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.write_bytes.html" title='std::ptr::write_bytes fn'>write_bytes</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Sets <code>count * size_of::<T>()</code> bytes of memory starting at <code>dst</code> to <code>val</code>.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.write_unaligned.html" title='std::ptr::write_unaligned fn'>write_unaligned</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Overwrites a memory location with the given value without reading or dropping the old value.</p> </td></tr><tr class='module-item'><td><a class="fn" href="fn.write_volatile.html" title='std::ptr::write_volatile fn'>write_volatile</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td><td class='docblock-short'><p>Performs a volatile write of a memory location with the given value without reading or dropping the old value.</p> </td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "std";</script><script src="../../aliases.js"></script><script src="../../main1.35.0.js"></script><script defer src="../../search-index.js"></script></body></html>