Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 0c2243f8a1696816431e7210e991fa52 > files > 12209

rust-doc-1.35.0-1.mga7.armv7hl.rpm

<!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 `read` fn in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, read"><title>core::ptr::read - 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 fn"><!--[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">&#9776;</div><a href='../../core/index.html'><img src='../../rust-logo1.35.0.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>ptr</a></p><script>window.sidebarCurrent = {name: 'read', ty: 'fn', 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'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/core/ptr.rs.html#573-577' title='goto source code'>[src]</a></span><span class='in-band'>Function <a href='../index.html'>core</a>::<wbr><a href='index.html'>ptr</a>::<wbr><a class="fn" href=''>read</a></span></h1><pre class='rust fn'>pub unsafe fn read&lt;T&gt;(src: *const T) -&gt; T</pre><div class='docblock'><p>Reads the value from <code>src</code> without moving it. This leaves the
memory in <code>src</code> unchanged.</p>
<h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1>
<p>Behavior is undefined if any of the following conditions are violated:</p>
<ul>
<li>
<p><code>src</code> must be <a href="../ptr/index.html#safety">valid</a> for reads.</p>
</li>
<li>
<p><code>src</code> must be properly aligned. Use <a href="./fn.read_unaligned.html"><code>read_unaligned</code></a> if this is not the
case.</p>
</li>
</ul>
<p>Note that even if <code>T</code> has size <code>0</code>, the pointer must be non-NULL and properly aligned.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Basic usage:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">12</span>;
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">x</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw">const</span> <span class="ident">i32</span>;

<span class="kw">unsafe</span> {
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">std</span>::<span class="ident">ptr</span>::<span class="ident">read</span>(<span class="ident">y</span>), <span class="number">12</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20x%20%3D%2012%3B%0Alet%20y%20%3D%20%26x%20as%20*const%20i32%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20assert_eq!(std%3A%3Aptr%3A%3Aread(y)%2C%2012)%3B%0A%7D%0A%7D">Run</a></pre></div>
<p>Manually implement <a href="../mem/fn.swap.html"><code>mem::swap</code></a>:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>;

<span class="kw">fn</span> <span class="ident">swap</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(<span class="ident">a</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span>, <span class="ident">b</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span>) {
    <span class="kw">unsafe</span> {
        <span class="comment">// Create a bitwise copy of the value at `a` in `tmp`.</span>
        <span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">ptr</span>::<span class="ident">read</span>(<span class="ident">a</span>);

        <span class="comment">// Exiting at this point (either by explicitly returning or by</span>
        <span class="comment">// calling a function which panics) would cause the value in `tmp` to</span>
        <span class="comment">// be dropped while the same value is still referenced by `a`. This</span>
        <span class="comment">// could trigger undefined behavior if `T` is not `Copy`.</span>

        <span class="comment">// Create a bitwise copy of the value at `b` in `a`.</span>
        <span class="comment">// This is safe because mutable references cannot alias.</span>
        <span class="ident">ptr</span>::<span class="ident">copy_nonoverlapping</span>(<span class="ident">b</span>, <span class="ident">a</span>, <span class="number">1</span>);

        <span class="comment">// As above, exiting here could trigger undefined behavior because</span>
        <span class="comment">// the same value is referenced by `a` and `b`.</span>

        <span class="comment">// Move `tmp` into `b`.</span>
        <span class="ident">ptr</span>::<span class="ident">write</span>(<span class="ident">b</span>, <span class="ident">tmp</span>);

        <span class="comment">// `tmp` has been moved (`write` takes ownership of its second argument),</span>
        <span class="comment">// so nothing is dropped implicitly here.</span>
    }
}

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">foo</span> <span class="op">=</span> <span class="string">&quot;foo&quot;</span>.<span class="ident">to_owned</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">bar</span> <span class="op">=</span> <span class="string">&quot;bar&quot;</span>.<span class="ident">to_owned</span>();

<span class="ident">swap</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">foo</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">bar</span>);

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">foo</span>, <span class="string">&quot;bar&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">bar</span>, <span class="string">&quot;foo&quot;</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aptr%3B%0A%0Afn%20swap%3CT%3E(a%3A%20%26mut%20T%2C%20b%3A%20%26mut%20T)%20%7B%0A%20%20%20%20unsafe%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20bitwise%20copy%20of%20the%20value%20at%20%60a%60%20in%20%60tmp%60.%0A%20%20%20%20%20%20%20%20let%20tmp%20%3D%20ptr%3A%3Aread(a)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Exiting%20at%20this%20point%20(either%20by%20explicitly%20returning%20or%20by%0A%20%20%20%20%20%20%20%20%2F%2F%20calling%20a%20function%20which%20panics)%20would%20cause%20the%20value%20in%20%60tmp%60%20to%0A%20%20%20%20%20%20%20%20%2F%2F%20be%20dropped%20while%20the%20same%20value%20is%20still%20referenced%20by%20%60a%60.%20This%0A%20%20%20%20%20%20%20%20%2F%2F%20could%20trigger%20undefined%20behavior%20if%20%60T%60%20is%20not%20%60Copy%60.%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20bitwise%20copy%20of%20the%20value%20at%20%60b%60%20in%20%60a%60.%0A%20%20%20%20%20%20%20%20%2F%2F%20This%20is%20safe%20because%20mutable%20references%20cannot%20alias.%0A%20%20%20%20%20%20%20%20ptr%3A%3Acopy_nonoverlapping(b%2C%20a%2C%201)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20As%20above%2C%20exiting%20here%20could%20trigger%20undefined%20behavior%20because%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20same%20value%20is%20referenced%20by%20%60a%60%20and%20%60b%60.%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Move%20%60tmp%60%20into%20%60b%60.%0A%20%20%20%20%20%20%20%20ptr%3A%3Awrite(b%2C%20tmp)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20%60tmp%60%20has%20been%20moved%20(%60write%60%20takes%20ownership%20of%20its%20second%20argument)%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20so%20nothing%20is%20dropped%20implicitly%20here.%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20mut%20foo%20%3D%20%22foo%22.to_owned()%3B%0Alet%20mut%20bar%20%3D%20%22bar%22.to_owned()%3B%0A%0Aswap(%26mut%20foo%2C%20%26mut%20bar)%3B%0A%0Aassert_eq!(foo%2C%20%22bar%22)%3B%0Aassert_eq!(bar%2C%20%22foo%22)%3B%0A%7D">Run</a></pre></div>
<h2 id="ownership-of-the-returned-value" class="section-header"><a href="#ownership-of-the-returned-value">Ownership of the Returned Value</a></h2>
<p><code>read</code> creates a bitwise copy of <code>T</code>, regardless of whether <code>T</code> is <a href="../marker/trait.Copy.html"><code>Copy</code></a>.
If <code>T</code> is not <a href="../marker/trait.Copy.html"><code>Copy</code></a>, using both the returned value and the value at
<code>*src</code> can violate memory safety. Note that assigning to <code>*src</code> counts as a
use because it will attempt to drop the value at <code>*src</code>.</p>
<p><a href="./fn.write.html"><code>write</code></a> can be used to overwrite data without causing it to be dropped.</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>;

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;foo&quot;</span>);
<span class="kw">unsafe</span> {
    <span class="comment">// `s2` now points to the same underlying memory as `s`.</span>
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s2</span>: <span class="ident">String</span> <span class="op">=</span> <span class="ident">ptr</span>::<span class="ident">read</span>(<span class="kw-2">&amp;</span><span class="ident">s</span>);

    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s2</span>, <span class="string">&quot;foo&quot;</span>);

    <span class="comment">// Assigning to `s2` causes its original value to be dropped. Beyond</span>
    <span class="comment">// this point, `s` must no longer be used, as the underlying memory has</span>
    <span class="comment">// been freed.</span>
    <span class="ident">s2</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">default</span>();
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s2</span>, <span class="string">&quot;&quot;</span>);

    <span class="comment">// Assigning to `s` would cause the old value to be dropped again,</span>
    <span class="comment">// resulting in undefined behavior.</span>
    <span class="comment">// s = String::from(&quot;bar&quot;); // ERROR</span>

    <span class="comment">// `ptr::write` can be used to overwrite a value without dropping it.</span>
    <span class="ident">ptr</span>::<span class="ident">write</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">s</span>, <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;bar&quot;</span>));
}

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="string">&quot;bar&quot;</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aptr%3B%0A%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0Aunsafe%20%7B%0A%20%20%20%20%2F%2F%20%60s2%60%20now%20points%20to%20the%20same%20underlying%20memory%20as%20%60s%60.%0A%20%20%20%20let%20mut%20s2%3A%20String%20%3D%20ptr%3A%3Aread(%26s)%3B%0A%0A%20%20%20%20assert_eq!(s2%2C%20%22foo%22)%3B%0A%0A%20%20%20%20%2F%2F%20Assigning%20to%20%60s2%60%20causes%20its%20original%20value%20to%20be%20dropped.%20Beyond%0A%20%20%20%20%2F%2F%20this%20point%2C%20%60s%60%20must%20no%20longer%20be%20used%2C%20as%20the%20underlying%20memory%20has%0A%20%20%20%20%2F%2F%20been%20freed.%0A%20%20%20%20s2%20%3D%20String%3A%3Adefault()%3B%0A%20%20%20%20assert_eq!(s2%2C%20%22%22)%3B%0A%0A%20%20%20%20%2F%2F%20Assigning%20to%20%60s%60%20would%20cause%20the%20old%20value%20to%20be%20dropped%20again%2C%0A%20%20%20%20%2F%2F%20resulting%20in%20undefined%20behavior.%0A%20%20%20%20%2F%2F%20s%20%3D%20String%3A%3Afrom(%22bar%22)%3B%20%2F%2F%20ERROR%0A%0A%20%20%20%20%2F%2F%20%60ptr%3A%3Awrite%60%20can%20be%20used%20to%20overwrite%20a%20value%20without%20dropping%20it.%0A%20%20%20%20ptr%3A%3Awrite(%26mut%20s%2C%20String%3A%3Afrom(%22bar%22))%3B%0A%7D%0A%0Aassert_eq!(s%2C%20%22bar%22)%3B%0A%7D">Run</a></pre></div>
</div></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>&#9166;</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 = "core";</script><script src="../../aliases.js"></script><script src="../../main1.35.0.js"></script><script defer src="../../search-index.js"></script></body></html>