Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 9817

rust-doc-1.28.0-1.mga6.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 `uninitialized` fn in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, uninitialized"><title>core::mem::uninitialized - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></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='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>mem</a></p><script>window.sidebarCurrent = {name: 'uninitialized', 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="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Function <a href='../index.html'>core</a>::<wbr><a href='index.html'>mem</a>::<wbr><a class="fn" href=''>uninitialized</a></span><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/mem.rs.html#613-615' title='goto source code'>[src]</a></span></h1><pre class='rust fn'>pub unsafe fn uninitialized&lt;T&gt;() -&gt; T</pre><div class='docblock'><p>Bypasses Rust's normal memory-initialization checks by pretending to
produce a value of type <code>T</code>, while doing nothing at all.</p>
<p><strong>This is incredibly dangerous and should not be done lightly. Deeply
consider initializing your memory with a default value instead.</strong></p>
<p>This is useful for <a href="../../book/first-edition/ffi.html">FFI</a> functions and initializing arrays sometimes,
but should generally be avoided.</p>
<h1 id="undefined-behavior" class="section-header"><a href="#undefined-behavior">Undefined behavior</a></h1>
<p>It is <a href="../../reference/behavior-considered-undefined.html">undefined behavior</a> to read uninitialized memory, even just an
uninitialized boolean. For instance, if you branch on the value of such
a boolean, your program may take one, both, or neither of the branches.</p>
<p>Writing to the uninitialized value is similarly dangerous. Rust believes the
value is initialized, and will therefore try to <a href="../ops/trait.Drop.html"><code>Drop</code></a> the uninitialized
value and its fields if you try to overwrite it in a normal manner. The only way
to safely initialize an uninitialized value is with <a href="../ptr/fn.write.html"><code>ptr::write</code></a>,
<a href="../intrinsics/fn.copy.html"><code>ptr::copy</code></a>, or <a href="../intrinsics/fn.copy_nonoverlapping.html"><code>ptr::copy_nonoverlapping</code></a>.</p>
<p>If the value does implement <a href="../ops/trait.Drop.html"><code>Drop</code></a>, it must be initialized before
it goes out of scope (and therefore would be dropped). Note that this
includes a <code>panic</code> occurring and unwinding the stack suddenly.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Here's how to safely initialize an array of <a href="../../std/vec/struct.Vec.html"><code>Vec</code></a>s.</p>

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

<span class="comment">// Only declare the array. This safely leaves it</span>
<span class="comment">// uninitialized in a way that Rust will track for us.</span>
<span class="comment">// However we can&#39;t initialize it element-by-element</span>
<span class="comment">// safely, and we can&#39;t use the `[value; 1000]`</span>
<span class="comment">// constructor because it only works with `Copy` data.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">data</span>: [<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;</span>; <span class="number">1000</span>];

<span class="kw">unsafe</span> {
    <span class="comment">// So we need to do this to initialize it.</span>
    <span class="ident">data</span> <span class="op">=</span> <span class="ident">mem</span>::<span class="ident">uninitialized</span>();

    <span class="comment">// DANGER ZONE: if anything panics or otherwise</span>
    <span class="comment">// incorrectly reads the array here, we will have</span>
    <span class="comment">// Undefined Behavior.</span>

    <span class="comment">// It&#39;s ok to mutably iterate the data, since this</span>
    <span class="comment">// doesn&#39;t involve reading it at all.</span>
    <span class="comment">// (ptr and len are statically known for arrays)</span>
    <span class="kw">for</span> <span class="ident">elem</span> <span class="kw">in</span> <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">data</span>[..] {
        <span class="comment">// *elem = Vec::new() would try to drop the</span>
        <span class="comment">// uninitialized memory at `elem` -- bad!</span>
        <span class="comment">//</span>
        <span class="comment">// Vec::new doesn&#39;t allocate or do really</span>
        <span class="comment">// anything. It&#39;s only safe to call here</span>
        <span class="comment">// because we know it won&#39;t panic.</span>
        <span class="ident">ptr</span>::<span class="ident">write</span>(<span class="ident">elem</span>, <span class="ident">Vec</span>::<span class="ident">new</span>());
    }

    <span class="comment">// SAFE ZONE: everything is initialized.</span>
}

<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">data</span>[<span class="number">0</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%3Amem%3B%0Ause%20std%3A%3Aptr%3B%0A%0A%2F%2F%20Only%20declare%20the%20array.%20This%20safely%20leaves%20it%0A%2F%2F%20uninitialized%20in%20a%20way%20that%20Rust%20will%20track%20for%20us.%0A%2F%2F%20However%20we%20can't%20initialize%20it%20element-by-element%0A%2F%2F%20safely%2C%20and%20we%20can't%20use%20the%20%60%5Bvalue%3B%201000%5D%60%0A%2F%2F%20constructor%20because%20it%20only%20works%20with%20%60Copy%60%20data.%0Alet%20mut%20data%3A%20%5BVec%3Cu32%3E%3B%201000%5D%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20%2F%2F%20So%20we%20need%20to%20do%20this%20to%20initialize%20it.%0A%20%20%20%20data%20%3D%20mem%3A%3Auninitialized()%3B%0A%0A%20%20%20%20%2F%2F%20DANGER%20ZONE%3A%20if%20anything%20panics%20or%20otherwise%0A%20%20%20%20%2F%2F%20incorrectly%20reads%20the%20array%20here%2C%20we%20will%20have%0A%20%20%20%20%2F%2F%20Undefined%20Behavior.%0A%0A%20%20%20%20%2F%2F%20It's%20ok%20to%20mutably%20iterate%20the%20data%2C%20since%20this%0A%20%20%20%20%2F%2F%20doesn't%20involve%20reading%20it%20at%20all.%0A%20%20%20%20%2F%2F%20(ptr%20and%20len%20are%20statically%20known%20for%20arrays)%0A%20%20%20%20for%20elem%20in%20%26mut%20data%5B..%5D%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20*elem%20%3D%20Vec%3A%3Anew()%20would%20try%20to%20drop%20the%0A%20%20%20%20%20%20%20%20%2F%2F%20uninitialized%20memory%20at%20%60elem%60%20--%20bad!%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%2F%2F%20Vec%3A%3Anew%20doesn't%20allocate%20or%20do%20really%0A%20%20%20%20%20%20%20%20%2F%2F%20anything.%20It's%20only%20safe%20to%20call%20here%0A%20%20%20%20%20%20%20%20%2F%2F%20because%20we%20know%20it%20won't%20panic.%0A%20%20%20%20%20%20%20%20ptr%3A%3Awrite(elem%2C%20Vec%3A%3Anew())%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20SAFE%20ZONE%3A%20everything%20is%20initialized.%0A%7D%0A%0Aprintln!(%22%7B%3A%3F%7D%22%2C%20%26data%5B0%5D)%3B%0A%7D">Run</a></pre>
<p>This example emphasizes exactly how delicate and dangerous using <code>mem::uninitialized</code>
can be. Note that the <a href="../../std/macro.vec.html"><code>vec!</code></a> macro <em>does</em> let you initialize every element with a
value that is only <a href="../../std/clone/trait.Clone.html"><code>Clone</code></a>, so the following is semantically equivalent and
vastly less dangerous, as long as you can live with an extra heap
allocation:</p>

<pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">data</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;&gt;</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Vec</span>::<span class="ident">new</span>(); <span class="number">1000</span>];
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">data</span>[<span class="number">0</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20data%3A%20Vec%3CVec%3Cu32%3E%3E%20%3D%20vec!%5BVec%3A%3Anew()%3B%201000%5D%3B%0Aprintln!(%22%7B%3A%3F%7D%22%2C%20%26data%5B0%5D)%3B%0A%7D">Run</a></pre>
</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="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>