Sophie

Sophie

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

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 `Extend` trait in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, Extend"><title>core::iter::Extend - 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 trait"><!--[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><p class='location'>Trait Extend</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.extend">extend</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>iter</a></p><script>window.sidebarCurrent = {name: 'Extend', ty: 'trait', 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/iter/traits/collect.rs.html#320-342' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>iter</a>::<wbr><a class="trait" href=''>Extend</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait Extend&lt;A&gt; {
    fn <a href='#tymethod.extend' class='fnname'>extend</a>&lt;T:&nbsp;<a class="trait" href="../../core/iter/trait.IntoIterator.html" title="trait core::iter::IntoIterator">IntoIterator</a>&lt;Item = A&gt;&gt;(&amp;mut self, iter: T);
}</pre></div><div class='docblock'><p>Extend a collection with the contents of an iterator.</p>
<p>Iterators produce a series of values, and collections can also be thought
of as a series of values. The <code>Extend</code> trait bridges this gap, allowing you
to extend a collection by including the contents of that iterator. When
extending a collection with an already existing key, that entry is updated
or, in the case of collections that permit multiple entries with equal
keys, that entry is inserted.</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="comment">// You can extend a String with some chars:</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">message</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;The first three letters are: &quot;</span>);

<span class="ident">message</span>.<span class="ident">extend</span>(<span class="kw-2">&amp;</span>[<span class="string">&#39;a&#39;</span>, <span class="string">&#39;b&#39;</span>, <span class="string">&#39;c&#39;</span>]);

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&quot;abc&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">message</span>[<span class="number">29</span>..<span class="number">32</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%2F%2F%20You%20can%20extend%20a%20String%20with%20some%20chars%3A%0Afn%20main()%20%7B%0Alet%20mut%20message%20%3D%20String%3A%3Afrom(%22The%20first%20three%20letters%20are%3A%20%22)%3B%0A%0Amessage.extend(%26%5B'a'%2C%20'b'%2C%20'c'%5D)%3B%0A%0Aassert_eq!(%22abc%22%2C%20%26message%5B29..32%5D)%3B%0A%7D">Run</a></pre></div>
<p>Implementing <code>Extend</code>:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="comment">// A sample collection, that&#39;s just a wrapper over Vec&lt;T&gt;</span>
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>)]</span>
<span class="kw">struct</span> <span class="ident">MyCollection</span>(<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span>);

<span class="comment">// Let&#39;s give it some methods so we can create one and add things</span>
<span class="comment">// to it.</span>
<span class="kw">impl</span> <span class="ident">MyCollection</span> {
    <span class="kw">fn</span> <span class="ident">new</span>() <span class="op">-&gt;</span> <span class="ident">MyCollection</span> {
        <span class="ident">MyCollection</span>(<span class="ident">Vec</span>::<span class="ident">new</span>())
    }

    <span class="kw">fn</span> <span class="ident">add</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>, <span class="ident">elem</span>: <span class="ident">i32</span>) {
        <span class="self">self</span>.<span class="number">0</span>.<span class="ident">push</span>(<span class="ident">elem</span>);
    }
}

<span class="comment">// since MyCollection has a list of i32s, we implement Extend for i32</span>
<span class="kw">impl</span> <span class="ident">Extend</span><span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span> <span class="kw">for</span> <span class="ident">MyCollection</span> {

    <span class="comment">// This is a bit simpler with the concrete type signature: we can call</span>
    <span class="comment">// extend on anything which can be turned into an Iterator which gives</span>
    <span class="comment">// us i32s. Because we need i32s to put into MyCollection.</span>
    <span class="kw">fn</span> <span class="ident">extend</span><span class="op">&lt;</span><span class="ident">T</span>: <span class="ident">IntoIterator</span><span class="op">&lt;</span><span class="ident">Item</span><span class="op">=</span><span class="ident">i32</span><span class="op">&gt;&gt;</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>, <span class="ident">iter</span>: <span class="ident">T</span>) {

        <span class="comment">// The implementation is very straightforward: loop through the</span>
        <span class="comment">// iterator, and add() each element to ourselves.</span>
        <span class="kw">for</span> <span class="ident">elem</span> <span class="kw">in</span> <span class="ident">iter</span> {
            <span class="self">self</span>.<span class="ident">add</span>(<span class="ident">elem</span>);
        }
    }
}

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">MyCollection</span>::<span class="ident">new</span>();

<span class="ident">c</span>.<span class="ident">add</span>(<span class="number">5</span>);
<span class="ident">c</span>.<span class="ident">add</span>(<span class="number">6</span>);
<span class="ident">c</span>.<span class="ident">add</span>(<span class="number">7</span>);

<span class="comment">// let&#39;s extend our collection with three more numbers</span>
<span class="ident">c</span>.<span class="ident">extend</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);

<span class="comment">// we&#39;ve added these elements onto the end</span>
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&quot;MyCollection([5, 6, 7, 1, 2, 3])&quot;</span>, <span class="macro">format</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">c</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%2F%2F%20A%20sample%20collection%2C%20that's%20just%20a%20wrapper%20over%20Vec%3CT%3E%0Afn%20main()%20%7B%0A%23%5Bderive(Debug)%5D%0Astruct%20MyCollection(Vec%3Ci32%3E)%3B%0A%0A%2F%2F%20Let's%20give%20it%20some%20methods%20so%20we%20can%20create%20one%20and%20add%20things%0A%2F%2F%20to%20it.%0Aimpl%20MyCollection%20%7B%0A%20%20%20%20fn%20new()%20-%3E%20MyCollection%20%7B%0A%20%20%20%20%20%20%20%20MyCollection(Vec%3A%3Anew())%0A%20%20%20%20%7D%0A%0A%20%20%20%20fn%20add(%26mut%20self%2C%20elem%3A%20i32)%20%7B%0A%20%20%20%20%20%20%20%20self.0.push(elem)%3B%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20since%20MyCollection%20has%20a%20list%20of%20i32s%2C%20we%20implement%20Extend%20for%20i32%0Aimpl%20Extend%3Ci32%3E%20for%20MyCollection%20%7B%0A%0A%20%20%20%20%2F%2F%20This%20is%20a%20bit%20simpler%20with%20the%20concrete%20type%20signature%3A%20we%20can%20call%0A%20%20%20%20%2F%2F%20extend%20on%20anything%20which%20can%20be%20turned%20into%20an%20Iterator%20which%20gives%0A%20%20%20%20%2F%2F%20us%20i32s.%20Because%20we%20need%20i32s%20to%20put%20into%20MyCollection.%0A%20%20%20%20fn%20extend%3CT%3A%20IntoIterator%3CItem%3Di32%3E%3E(%26mut%20self%2C%20iter%3A%20T)%20%7B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20implementation%20is%20very%20straightforward%3A%20loop%20through%20the%0A%20%20%20%20%20%20%20%20%2F%2F%20iterator%2C%20and%20add()%20each%20element%20to%20ourselves.%0A%20%20%20%20%20%20%20%20for%20elem%20in%20iter%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20self.add(elem)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20mut%20c%20%3D%20MyCollection%3A%3Anew()%3B%0A%0Ac.add(5)%3B%0Ac.add(6)%3B%0Ac.add(7)%3B%0A%0A%2F%2F%20let's%20extend%20our%20collection%20with%20three%20more%20numbers%0Ac.extend(vec!%5B1%2C%202%2C%203%5D)%3B%0A%0A%2F%2F%20we've%20added%20these%20elements%20onto%20the%20end%0Aassert_eq!(%22MyCollection(%5B5%2C%206%2C%207%2C%201%2C%202%2C%203%5D)%22%2C%20format!(%22%7B%3A%3F%7D%22%2C%20c))%3B%0A%7D">Run</a></pre></div>
</div>
            <h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.extend' class='method'><code id='extend.v'>fn <a href='#tymethod.extend' class='fnname'>extend</a>&lt;T:&nbsp;<a class="trait" href="../../core/iter/trait.IntoIterator.html" title="trait core::iter::IntoIterator">IntoIterator</a>&lt;Item = A&gt;&gt;(&amp;mut self, iter: T)</code></h3><div class='docblock'><p>Extends a collection with the contents of an iterator.</p>
<p>As this is the only method for this trait, the <a href="trait.Extend.html">trait-level</a> docs
contain more details.</p>
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
<p>Basic usage:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="comment">// You can extend a String with some chars:</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">message</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;abc&quot;</span>);

<span class="ident">message</span>.<span class="ident">extend</span>([<span class="string">&#39;d&#39;</span>, <span class="string">&#39;e&#39;</span>, <span class="string">&#39;f&#39;</span>].<span class="ident">iter</span>());

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&quot;abcdef&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">message</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%2F%2F%20You%20can%20extend%20a%20String%20with%20some%20chars%3A%0Afn%20main()%20%7B%0Alet%20mut%20message%20%3D%20String%3A%3Afrom(%22abc%22)%3B%0A%0Amessage.extend(%5B'd'%2C%20'e'%2C%20'f'%5D.iter())%3B%0A%0Aassert_eq!(%22abcdef%22%2C%20%26message)%3B%0A%7D">Run</a></pre></div>
</div></div><span class='loading-content'>Loading content...</span>
            <h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'><h3 id='impl-Extend%3C()%3E' class='impl'><code class='in-band'>impl Extend&lt;()&gt; for ()</code><a href='#impl-Extend%3C()%3E' class='anchor'></a><a class='srclink' href='../../src/core/iter/traits/collect.rs.html#345-349' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.extend' class="method hidden"><code id='extend.v-1'>fn <a href='#method.extend' class='fnname'>extend</a>&lt;T:&nbsp;<a class="trait" href="../../core/iter/trait.IntoIterator.html" title="trait core::iter::IntoIterator">IntoIterator</a>&lt;Item = ()&gt;&gt;(&amp;mut self, iter: T)</code><a class='srclink' href='../../src/core/iter/traits/collect.rs.html#346-348' title='goto source code'>[src]</a></h4></div></div><span class='loading-content'>Loading content...</span><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
                         src="../../implementors/core/iter/trait.Extend.js">
                 </script></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>