<!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 `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, Into"> <title>std::convert::Into - 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="../../main.css" id="themeStyle"> <script src="../../storage.js"></script> <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"> <div class="sidebar-menu">☰</div> <a href='../../std/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="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.into">into</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>std</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></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"> </div> </form> </nav> <section id='main' class="content"> <h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>std</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'>−</span>] </a> </span><a class='srclink' href='../../src/core/convert.rs.html#244-248' title='goto source code'>[src]</a></span></h1> <pre class='rust trait'>pub trait Into<T> { fn <a href='#tymethod.into' class='fnname'>into</a>(self) -> 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<T></code></a> or a <a href="../../std/result/enum.Result.html"><code>Result<T, E></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<T></code></a><code>for U</code> implies <code>Into<U> for T</code></li> <li><a href="trait.Into.html#tymethod.into"><code>into</code></a> is reflexive, which means that <code>Into<T> 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's kind of esoteric. If the destination type is not part of the current crate, and it uses a generic variable, then you can't implement <code>From</code> directly. For example, take this crate:</p> <div class='information'><div class='tooltip compile_fail'>ⓘ<span class='tooltiptext'>This example deliberately fails to compile</span></div></div><pre class="rust rust-example-rendered compile_fail"> <span class="kw">struct</span> <span class="ident">Wrapper</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>(<span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>); <span class="kw">impl</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="ident">From</span><span class="op"><</span><span class="ident">Wrapper</span><span class="op"><</span><span class="ident">T</span><span class="op">>></span> <span class="kw">for</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> { <span class="kw">fn</span> <span class="ident">from</span>(<span class="ident">w</span>: <span class="ident">Wrapper</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>) <span class="op">-></span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> { <span class="ident">w</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%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"><</span><span class="ident">T</span><span class="op">></span>(<span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>); <span class="kw">impl</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="ident">Into</span><span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">>></span> <span class="kw">for</span> <span class="ident">Wrapper</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> { <span class="kw">fn</span> <span class="ident">into</span>(<span class="self">self</span>) <span class="op">-></span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> { <span class="self">self</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%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'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'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<Vec<u8>></code>:</p> <pre class="rust rust-example-rendered"> <span class="kw">fn</span> <span class="ident">is_hello</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">Into</span><span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="ident">u8</span><span class="op">>></span><span class="op">></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"hello"</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">"hello"</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=%23!%5Ballow(unused)%5D%0Afn%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' class='small-section-header'> Required Methods<a href='#required-methods' class='anchor'></a> </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) -> T</code></span></h3><div class='docblock'><p>Performs the conversion.</p> </div></div> <h2 id='implementors' class='small-section-header'> Implementors<a href='#implementors' class='anchor'></a> </h2> <ul class='item-list' id='implementors-list'> <li><div class='out-of-band'><a class='srclink' href='../../src/core/convert.rs.html#412-417' title='goto source code'>[src]</a></div><code>impl<T, U> Into<U> for T <span class="where fmt-newline">where<br> U: <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><T>, </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><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>⏎</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> </div> </div> </aside> <script> window.rootPath = "../../"; window.currentCrate = "std"; </script> <script src="../../main.js"></script> <script defer src="../../search-index.js"></script> </body> </html>