<!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 `Weak` struct in crate `alloc`."> <meta name="keywords" content="rust, rustlang, rust-lang, Weak"> <title>alloc::arc::Weak - Rust</title> <link rel="stylesheet" type="text/css" href="../../normalize.css"> <link rel="stylesheet" type="text/css" href="../../rustdoc.css"> <link rel="stylesheet" type="text/css" href="../../main.css"> <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"> </head> <body class="rustdoc struct"> <!--[if lte IE 8]> <div class="warning"> This old browser is unsupported and will most likely display funky things. </div> <![endif]--> <nav class="sidebar"> <a href='../../alloc/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a> <p class='location'>Struct Weak</p><div class="block items"><ul><li><a href="#methods">Methods</a></li><li><a href="#implementations">Trait Implementations</a></li></ul></div><p class='location'><a href='../index.html'>alloc</a>::<wbr><a href='index.html'>arc</a></p><script>window.sidebarCurrent = {name: 'Weak', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script> </nav> <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"> </div> </form> </nav> <section id='main' class="content"> <h1 class='fqn'><span class='in-band'>Struct <a href='../index.html'>alloc</a>::<wbr><a href='index.html'>arc</a>::<wbr><a class="struct" href=''>Weak</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.4.0'>1.4.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/alloc/arc.rs.html#229-231' title='goto source code'>[src]</a></span></h1> <pre class='rust struct'>pub struct Weak<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> { /* fields omitted */ }</pre><div class='docblock'><p><code>Weak</code> is a version of <a href="struct.Arc.html"><code>Arc</code></a> that holds a non-owning reference to the managed value. The value is accessed by calling <a href="struct.Weak.html#method.upgrade"><code>upgrade</code></a> on the <code>Weak</code> pointer, which returns an <a href="../../std/option/enum.Option.html"><code>Option</code></a><code><</code><a href="struct.Arc.html"><code>Arc</code></a><code><T>></code>.</p> <p>Since a <code>Weak</code> reference does not count towards ownership, it will not prevent the inner value from being dropped, and <code>Weak</code> itself makes no guarantees about the value still being present and may return <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a> when <a href="struct.Weak.html#method.upgrade"><code>upgrade</code></a>d.</p> <p>A <code>Weak</code> pointer is useful for keeping a temporary reference to the value within <a href="struct.Arc.html"><code>Arc</code></a> without extending its lifetime. It is also used to prevent circular references between <a href="struct.Arc.html"><code>Arc</code></a> pointers, since mutual owning references would never allow either <a href="struct.Arc.html"><code>Arc</code></a> to be dropped. For example, a tree could have strong <a href="struct.Arc.html"><code>Arc</code></a> pointers from parent nodes to children, and <code>Weak</code> pointers from children back to their parents.</p> <p>The typical way to obtain a <code>Weak</code> pointer is to call <a href="struct.Arc.html#method.downgrade"><code>Arc::downgrade</code></a>.</p> </div><h2 id='methods'>Methods</h2><h3 class='impl'><span class='in-band'><code>impl<T> <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#822-849' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.new' class="method"><span id='new.v' class='invisible'><code>fn <a href='#method.new' class='fnname'>new</a>() -> <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code><div class='since' title='Stable since Rust version 1.10.0'>1.10.0</div></span></h4> <div class='docblock'><p>Constructs a new <code>Weak<T></code>, allocating memory for <code>T</code> without initializing it. Calling <a href="struct.Weak.html#method.upgrade"><code>upgrade</code></a> on the return value always gives <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a>.</p> <h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">Weak</span>; <span class="kw">let</span> <span class="ident">empty</span>: <span class="ident">Weak</span><span class="op"><</span><span class="ident">i64</span><span class="op">></span> <span class="op">=</span> <span class="ident">Weak</span>::<span class="ident">new</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">empty</span>.<span class="ident">upgrade</span>().<span class="ident">is_none</span>());</pre> </div></div><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#851-915' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.upgrade' class="method"><span id='upgrade.v' class='invisible'><code>fn <a href='#method.upgrade' class='fnname'>upgrade</a>(&self) -> <a class="enum" href="../../core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="../../alloc/arc/struct.Arc.html" title="struct alloc::arc::Arc">Arc</a><T>></code></span></h4> <div class='docblock'><p>Attempts to upgrade the <code>Weak</code> pointer to an <a href="struct.Arc.html"><code>Arc</code></a>, extending the lifetime of the value if successful.</p> <p>Returns <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a> if the value has since been dropped.</p> <h1 id='examples-1' class='section-header'><a href='#examples-1'>Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">Arc</span>; <span class="kw">let</span> <span class="ident">five</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="number">5</span>); <span class="kw">let</span> <span class="ident">weak_five</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">downgrade</span>(<span class="kw-2">&</span><span class="ident">five</span>); <span class="kw">let</span> <span class="ident">strong_five</span>: <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">Arc</span><span class="op"><</span>_<span class="op">>></span> <span class="op">=</span> <span class="ident">weak_five</span>.<span class="ident">upgrade</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">strong_five</span>.<span class="ident">is_some</span>()); <span class="comment">// Destroy all strong pointers.</span> <span class="ident">drop</span>(<span class="ident">strong_five</span>); <span class="ident">drop</span>(<span class="ident">five</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">weak_five</span>.<span class="ident">upgrade</span>().<span class="ident">is_none</span>());</pre> </div></div><h2 id='implementations'>Trait Implementations</h2><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + <a class="trait" href="../../core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>> <a class="trait" href="../../core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#234' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + <a class="trait" href="../../core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>> <a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#236' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a><U>, U: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> <a class="trait" href="../../core/ops/trait.CoerceUnsized.html" title="trait core::ops::CoerceUnsized">CoerceUnsized</a><<a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><U>> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#239' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="../../core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>> <a class="trait" href="../../core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#242-246' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.fmt' class="method"><span id='fmt.v' class='invisible'><code>fn <a href='../../core/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class="struct" href="../../core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a>) -> <a class="type" href="../../core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></code></span></h4> <div class='docblock'><p>Formats the value using the given formatter.</p> </div></div><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> <a class="trait" href="../../core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#918-947' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.clone' class="method"><span id='clone.v' class='invisible'><code>fn <a href='../../core/clone/trait.Clone.html#tymethod.clone' class='fnname'>clone</a>(&self) -> <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span></h4> <div class='docblock'><p>Makes a clone of the <code>Weak</code> pointer that points to the same value.</p> <h1 id='examples-2' class='section-header'><a href='#examples-2'>Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::{<span class="ident">Arc</span>, <span class="ident">Weak</span>}; <span class="kw">let</span> <span class="ident">weak_five</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">downgrade</span>(<span class="kw-2">&</span><span class="ident">Arc</span>::<span class="ident">new</span>(<span class="number">5</span>)); <span class="ident">Weak</span>::<span class="ident">clone</span>(<span class="kw-2">&</span><span class="ident">weak_five</span>);</pre> </div><h4 id='method.clone_from' class="method"><span id='clone_from.v' class='invisible'><code>fn <a href='../../core/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&mut self, source: &Self)</code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4> <div class='docblock'><p>Performs copy-assignment from <code>source</code>. <a href="../../core/clone/trait.Clone.html#method.clone_from">Read more</a></p> </div></div><h3 class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../core/default/trait.Default.html" title="trait core::default::Default">Default</a> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.10.0'>1.10.0</div><a class='srclink' href='../../src/alloc/arc.rs.html#950-968' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.default' class="method"><span id='default.v' class='invisible'><code>fn <a href='../../core/default/trait.Default.html#tymethod.default' class='fnname'>default</a>() -> <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span></h4> <div class='docblock'><p>Constructs a new <code>Weak<T></code>, allocating memory for <code>T</code> without initializing it. Calling <a href="struct.Weak.html#method.upgrade"><code>upgrade</code></a> on the return value always gives <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a>.</p> <h1 id='examples-3' class='section-header'><a href='#examples-3'>Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">Weak</span>; <span class="kw">let</span> <span class="ident">empty</span>: <span class="ident">Weak</span><span class="op"><</span><span class="ident">i64</span><span class="op">></span> <span class="op">=</span> <span class="ident">Default</span>::<span class="ident">default</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">empty</span>.<span class="ident">upgrade</span>().<span class="ident">is_none</span>());</pre> </div></div><h3 class='impl'><span class='in-band'><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> <a class="trait" href="../../core/ops/trait.Drop.html" title="trait core::ops::Drop">Drop</a> for <a class="struct" href="../../alloc/arc/struct.Weak.html" title="struct alloc::arc::Weak">Weak</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#971-1012' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.drop' class="method"><span id='drop.v' class='invisible'><code>fn <a href='../../core/ops/trait.Drop.html#tymethod.drop' class='fnname'>drop</a>(&mut self)</code></span></h4> <div class='docblock'><p>Drops the <code>Weak</code> pointer.</p> <h1 id='examples-4' class='section-header'><a href='#examples-4'>Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::{<span class="ident">Arc</span>, <span class="ident">Weak</span>}; <span class="kw">struct</span> <span class="ident">Foo</span>; <span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">Foo</span> { <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="self">self</span>) { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"dropped!"</span>); } } <span class="kw">let</span> <span class="ident">foo</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">Foo</span>); <span class="kw">let</span> <span class="ident">weak_foo</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">downgrade</span>(<span class="kw-2">&</span><span class="ident">foo</span>); <span class="kw">let</span> <span class="ident">other_weak_foo</span> <span class="op">=</span> <span class="ident">Weak</span>::<span class="ident">clone</span>(<span class="kw-2">&</span><span class="ident">weak_foo</span>); <span class="ident">drop</span>(<span class="ident">weak_foo</span>); <span class="comment">// Doesn't print anything</span> <span class="ident">drop</span>(<span class="ident">foo</span>); <span class="comment">// Prints "dropped!"</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">other_weak_foo</span>.<span class="ident">upgrade</span>().<span class="ident">is_none</span>());</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>?</dt> <dd>Show this help dialog</dd> <dt>S</dt> <dd>Focus the search field</dd> <dt>⇤</dt> <dd>Move up in search results</dd> <dt>⇥</dt> <dd>Move down in search results</dd> <dt>⏎</dt> <dd>Go to active search result</dd> <dt>+</dt> <dd>Collapse/expand 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> </div> </div> </aside> <script> window.rootPath = "../../"; window.currentCrate = "alloc"; </script> <script src="../../main.js"></script> <script defer src="../../search-index.js"></script> </body> </html>