Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 564935689ab5527f955e5449ded02799 > files > 822

rust-doc-1.19.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 `Into` trait in crate `core`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, Into">

    <title>core::convert::Into - 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 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">
        <a href='../../core/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
        <p class='location'>Trait Into</p><div class="block items"><ul><li><a href="#required-methods">Required Methods</a></li><li><a href="#implementors">Implementors</a></li></ul></div><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>convert</a></p><script>window.sidebarCurrent = {name: 'Into', ty: 'trait', 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'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>convert</a>::<wbr><a class="trait" href=''>Into</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/convert.rs.html#227-231' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'>pub trait Into&lt;T&gt;: <a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
    fn <a href='#tymethod.into' class='fnname'>into</a>(self) -&gt; T;
}</pre><div class='docblock'><p>A conversion that consumes <code>self</code>, which may or may not be expensive. The
reciprocal of <a href="trait.From.html"><code>From</code></a>.</p>

<p><strong>Note: this trait must not fail</strong>. If the conversion can fail, use
<a href="trait.TryInto.html"><code>TryInto</code></a> or a dedicated method which returns an <a href="../../std/option/enum.Option.html"><code>Option&lt;T&gt;</code></a> or a
<a href="../../std/result/enum.Result.html"><code>Result&lt;T, E&gt;</code></a>.</p>

<p>Library authors should not directly implement this trait, but should prefer
implementing the <a href="trait.From.html"><code>From</code></a> trait, which offers greater flexibility and
provides an equivalent <code>Into</code> implementation for free, thanks to a blanket
implementation in the standard library.</p>

<h1 id='generic-implementations' class='section-header'><a href='#generic-implementations'>Generic Implementations</a></h1>
<ul>
<li><a href="trait.From.html"><code>From&lt;T&gt;</code></a><code>for U</code> implies <code>Into&lt;U&gt; for T</code></li>
<li><a href="trait.Into.html#tymethod.into"><code>into</code></a> is reflexive, which means that <code>Into&lt;T&gt; for T</code> is implemented</li>
</ul>

<h1 id='implementing-into' class='section-header'><a href='#implementing-into'>Implementing <code>Into</code></a></h1>
<p>There is one exception to implementing <code>Into</code>, and it&#39;s kind of esoteric.
If the destination type is not part of the current crate, and it uses a
generic variable, then you can&#39;t implement <code>From</code> directly.  For example,
take this crate:</p>

<pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">Wrapper</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>);
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">From</span><span class="op">&lt;</span><span class="ident">Wrapper</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span> <span class="kw">for</span> <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
    <span class="kw">fn</span> <span class="ident">from</span>(<span class="ident">w</span>: <span class="ident">Wrapper</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>) <span class="op">-&gt;</span> <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
        <span class="ident">w</span>.<span class="number">0</span>
    }
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Astruct%20Wrapper%3CT%3E(Vec%3CT%3E)%3B%0Aimpl%3CT%3E%20From%3CWrapper%3CT%3E%3E%20for%20Vec%3CT%3E%20%7B%0A%20%20%20%20fn%20from(w%3A%20Wrapper%3CT%3E)%20-%3E%20Vec%3CT%3E%20%7B%0A%20%20%20%20%20%20%20%20w.0%0A%20%20%20%20%7D%0A%7D%0A%7D">Run</a></pre>

<p>To fix this, you can implement <code>Into</code> directly:</p>

<pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">Wrapper</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>);
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span> <span class="kw">for</span> <span class="ident">Wrapper</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
    <span class="kw">fn</span> <span class="ident">into</span>(<span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
        <span class="self">self</span>.<span class="number">0</span>
    }
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Astruct%20Wrapper%3CT%3E(Vec%3CT%3E)%3B%0Aimpl%3CT%3E%20Into%3CVec%3CT%3E%3E%20for%20Wrapper%3CT%3E%20%7B%0A%20%20%20%20fn%20into(self)%20-%3E%20Vec%3CT%3E%20%7B%0A%20%20%20%20%20%20%20%20self.0%0A%20%20%20%20%7D%0A%7D%0A%7D">Run</a></pre>

<p>This won&#39;t always allow the conversion: for example, <code>try!</code> and <code>?</code>
always use <code>From</code>. However, in most cases, people use <code>Into</code> to do the
conversions, and this will allow that.</p>

<p>In almost all cases, you should try to implement <code>From</code>, then fall back
to <code>Into</code> if <code>From</code> can&#39;t be implemented.</p>

<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
<p><a href="../../std/string/struct.String.html"><code>String</code></a> implements <code>Into&lt;Vec&lt;u8&gt;&gt;</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">fn</span> <span class="ident">is_hello</span><span class="op">&lt;</span><span class="ident">T</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;&gt;</span><span class="op">&gt;</span>(<span class="ident">s</span>: <span class="ident">T</span>) {
   <span class="kw">let</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="string">b&quot;hello&quot;</span>.<span class="ident">to_vec</span>();
   <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">bytes</span>, <span class="ident">s</span>.<span class="ident">into</span>());
}

<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">&quot;hello&quot;</span>.<span class="ident">to_string</span>();
<span class="ident">is_hello</span>(<span class="ident">s</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Afn%20is_hello%3CT%3A%20Into%3CVec%3Cu8%3E%3E%3E(s%3A%20T)%20%7B%0A%20%20%20let%20bytes%20%3D%20b%22hello%22.to_vec()%3B%0A%20%20%20assert_eq!(bytes%2C%20s.into())%3B%0A%7D%0A%0Alet%20s%20%3D%20%22hello%22.to_string()%3B%0Ais_hello(s)%3B%0A%7D">Run</a></pre>
</div>
            <h2 id='required-methods'>Required Methods</h2>
            <div class='methods'>
        <h3 id='tymethod.into' class='method'><span id='into.v' class='invisible'><code>fn <a href='#tymethod.into' class='fnname'>into</a>(self) -&gt; T</code></span></h3><div class='docblock'><p>Performs the conversion.</p>
</div></div>
        <h2 id='implementors'>Implementors</h2>
        <ul class='item-list' id='implementors-list'>
    <li><code>impl&lt;T, U&gt; Into&lt;U&gt; for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: <a class="trait" href="../../core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt;,&nbsp;</span></code></li>
</ul><script type="text/javascript" async
                         src="../../implementors/core/convert/trait.Into.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>?</dt>
                    <dd>Show this help dialog</dd>
                    <dt>S</dt>
                    <dd>Focus the search field</dd>
                    <dt>&larrb;</dt>
                    <dd>Move up in search results</dd>
                    <dt>&rarrb;</dt>
                    <dd>Move down in search results</dd>
                    <dt>&#9166;</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 = "core";
    </script>
    <script src="../../main.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>