Sophie

Sophie

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

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 `Index` trait in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, Index"><title>core::ops::Index - 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 Index</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#associated-types">Associated Types</a><div class="sidebar-links"><a href="#associatedtype.Output">Output</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.index">index</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'>ops</a></p><script>window.sidebarCurrent = {name: 'Index', 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/ops/index.rs.html#61-69' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>ops</a>::<wbr><a class="trait" href=''>Index</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'><div class="docblock attributes">#[lang = "index"]
</div>pub trait Index&lt;Idx:&nbsp;?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; {
    type <a href='#associatedtype.Output' class="type">Output</a>: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>;
    fn <a href='#tymethod.index' class='fnname'>index</a>(&amp;self, index: Idx) -&gt; &amp;Self::<a class="type" href="../../core/ops/trait.Index.html#associatedtype.Output" title="type core::ops::Index::Output">Output</a>;
}</pre></div><div class='docblock'><p>Used for indexing operations (<code>container[index]</code>) in immutable contexts.</p>
<p><code>container[index]</code> is actually syntactic sugar for <code>*container.index(index)</code>,
but only when used as an immutable value. If a mutable value is requested,
<a href="../../std/ops/trait.IndexMut.html"><code>IndexMut</code></a> is used instead. This allows nice things such as
<code>let value = v[index]</code> if the type of <code>value</code> implements <a href="../../std/marker/trait.Copy.html"><code>Copy</code></a>.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>The following example implements <code>Index</code> on a read-only <code>NucleotideCount</code>
container, enabling individual counts to be retrieved with index syntax.</p>

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

<span class="kw">enum</span> <span class="ident">Nucleotide</span> {
    <span class="ident">A</span>,
    <span class="ident">C</span>,
    <span class="ident">G</span>,
    <span class="ident">T</span>,
}

<span class="kw">struct</span> <span class="ident">NucleotideCount</span> {
    <span class="ident">a</span>: <span class="ident">usize</span>,
    <span class="ident">c</span>: <span class="ident">usize</span>,
    <span class="ident">g</span>: <span class="ident">usize</span>,
    <span class="ident">t</span>: <span class="ident">usize</span>,
}

<span class="kw">impl</span> <span class="ident">Index</span><span class="op">&lt;</span><span class="ident">Nucleotide</span><span class="op">&gt;</span> <span class="kw">for</span> <span class="ident">NucleotideCount</span> {
    <span class="kw">type</span> <span class="ident">Output</span> <span class="op">=</span> <span class="ident">usize</span>;

    <span class="kw">fn</span> <span class="ident">index</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">nucleotide</span>: <span class="ident">Nucleotide</span>) <span class="op">-&gt;</span> <span class="kw-2">&amp;</span><span class="self">Self</span>::<span class="ident">Output</span> {
        <span class="kw">match</span> <span class="ident">nucleotide</span> {
            <span class="ident">Nucleotide</span>::<span class="ident">A</span> <span class="op">=&gt;</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">a</span>,
            <span class="ident">Nucleotide</span>::<span class="ident">C</span> <span class="op">=&gt;</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">c</span>,
            <span class="ident">Nucleotide</span>::<span class="ident">G</span> <span class="op">=&gt;</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">g</span>,
            <span class="ident">Nucleotide</span>::<span class="ident">T</span> <span class="op">=&gt;</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">t</span>,
        }
    }
}

<span class="kw">let</span> <span class="ident">nucleotide_count</span> <span class="op">=</span> <span class="ident">NucleotideCount</span> {<span class="ident">a</span>: <span class="number">14</span>, <span class="ident">c</span>: <span class="number">9</span>, <span class="ident">g</span>: <span class="number">10</span>, <span class="ident">t</span>: <span class="number">12</span>};
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">nucleotide_count</span>[<span class="ident">Nucleotide</span>::<span class="ident">A</span>], <span class="number">14</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">nucleotide_count</span>[<span class="ident">Nucleotide</span>::<span class="ident">C</span>], <span class="number">9</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">nucleotide_count</span>[<span class="ident">Nucleotide</span>::<span class="ident">G</span>], <span class="number">10</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">nucleotide_count</span>[<span class="ident">Nucleotide</span>::<span class="ident">T</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%0Ause%20std%3A%3Aops%3A%3AIndex%3B%0A%0Aenum%20Nucleotide%20%7B%0A%20%20%20%20A%2C%0A%20%20%20%20C%2C%0A%20%20%20%20G%2C%0A%20%20%20%20T%2C%0A%7D%0A%0Astruct%20NucleotideCount%20%7B%0A%20%20%20%20a%3A%20usize%2C%0A%20%20%20%20c%3A%20usize%2C%0A%20%20%20%20g%3A%20usize%2C%0A%20%20%20%20t%3A%20usize%2C%0A%7D%0A%0Aimpl%20Index%3CNucleotide%3E%20for%20NucleotideCount%20%7B%0A%20%20%20%20type%20Output%20%3D%20usize%3B%0A%0A%20%20%20%20fn%20index(%26self%2C%20nucleotide%3A%20Nucleotide)%20-%3E%20%26Self%3A%3AOutput%20%7B%0A%20%20%20%20%20%20%20%20match%20nucleotide%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Nucleotide%3A%3AA%20%3D%3E%20%26self.a%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Nucleotide%3A%3AC%20%3D%3E%20%26self.c%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Nucleotide%3A%3AG%20%3D%3E%20%26self.g%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Nucleotide%3A%3AT%20%3D%3E%20%26self.t%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20nucleotide_count%20%3D%20NucleotideCount%20%7Ba%3A%2014%2C%20c%3A%209%2C%20g%3A%2010%2C%20t%3A%2012%7D%3B%0Aassert_eq!(nucleotide_count%5BNucleotide%3A%3AA%5D%2C%2014)%3B%0Aassert_eq!(nucleotide_count%5BNucleotide%3A%3AC%5D%2C%209)%3B%0Aassert_eq!(nucleotide_count%5BNucleotide%3A%3AG%5D%2C%2010)%3B%0Aassert_eq!(nucleotide_count%5BNucleotide%3A%3AT%5D%2C%2012)%3B%0A%7D">Run</a></pre></div>
</div>
            <h2 id='associated-types' class='small-section-header'>Associated Types<a href='#associated-types' class='anchor'></a></h2><div class='methods'><h3 id='associatedtype.Output' class='method'><code id='Output.t'>type <a href='#associatedtype.Output' class="type">Output</a>: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></code></h3><div class='docblock'><p>The returned type after indexing.</p>
</div></div><span class='loading-content'>Loading content...</span>
            <h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.index' class='method'><code id='index.v'>fn <a href='#tymethod.index' class='fnname'>index</a>(&amp;self, index: Idx) -&gt; &amp;Self::<a class="type" href="../../core/ops/trait.Index.html#associatedtype.Output" title="type core::ops::Index::Output">Output</a></code></h3><div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p>
</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-Index%3CI%3E' class='impl'><code class='in-band'>impl&lt;I&gt; Index&lt;I&gt; for str <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="../../core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a>&lt;str&gt;,&nbsp;</span></code><a href='#impl-Index%3CI%3E' class='anchor'></a><a class='srclink' href='../../src/core/str/mod.rs.html#1608-1618' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Output-1' class="type"><code id='Output.t-1'>type <a href='#associatedtype.Output-1' class="type">Output</a> = I::<a class="type" href="../../core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a></code></h4><h4 id='method.index' class="method hidden"><code id='index.v-1'>fn <a href='#method.index' class='fnname'>index</a>(&amp;self, index: I) -&gt; &amp;I::<a class="type" href="../../core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a></code><a class='srclink' href='../../src/core/str/mod.rs.html#1615-1617' title='goto source code'>[src]</a></h4></div><h3 id='impl-Index%3CI%3E-1' class='impl'><code class='in-band'>impl&lt;T, I&gt; Index&lt;I&gt; for [T] <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="../../core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a>&lt;[T]&gt;,&nbsp;</span></code><a href='#impl-Index%3CI%3E-1' class='anchor'></a><a class='srclink' href='../../src/core/slice/mod.rs.html#2536-2545' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Output-2' class="type"><code id='Output.t-2'>type <a href='#associatedtype.Output-2' class="type">Output</a> = I::<a class="type" href="../../core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a></code></h4><h4 id='method.index-1' class="method hidden"><code id='index.v-2'>fn <a href='#method.index-1' class='fnname'>index</a>(&amp;self, index: I) -&gt; &amp;I::<a class="type" href="../../core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a></code><a class='srclink' href='../../src/core/slice/mod.rs.html#2542-2544' 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/ops/trait.Index.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>