Skip to content

Instantly share code, notes, and snippets.

@SimonSapin
Created February 17, 2021 18:48
Show Gist options
  • Save SimonSapin/7d4763a08063ca7f2951e307618b6767 to your computer and use it in GitHub Desktop.
Save SimonSapin/7d4763a08063ca7f2951e307618b6767 to your computer and use it in GitHub Desktop.
<!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 `Self` keyword in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, Self"><title>Self - Rust</title><link rel="stylesheet" type="text/css" href="../normalize1.52.0.css"><link rel="stylesheet" type="text/css" href="../rustdoc1.52.0.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../light1.52.0.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark1.52.0.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu1.52.0.css" disabled ><script id="default-settings"></script><script src="../storage1.52.0.js"></script><noscript><link rel="stylesheet" href="../noscript1.52.0.css"></noscript><link rel="icon" type="image/svg+xml" href="../favicon1.52.0.svg">
<link rel="alternate icon" type="image/png" href="../favicon-16x161.52.0.png">
<link rel="alternate icon" type="image/png" href="../favicon-32x321.52.0.png"><style type="text/css">#crate-search{background-image:url("../down-arrow1.52.0.svg");}</style></head><body class="rustdoc keyword"><!--[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='../std/index.html'><div class='logo-container rust-logo'><img src='../rust-logo1.52.0.png' alt='logo'></div></a><div class="sidebar-elems"><p class="location"><a href="index.html">std</a></p><div id="sidebar-vars" data-name="Self" data-ty="keyword" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../brush1.52.0.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../theme1.52.0.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" class="help-button">?</button>
<a id="settings-menu" href="../settings.html"><img src="../wheel1.52.0.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Keyword <a class="keyword" href="">Self</a></span><span class="out-of-band"><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/std/keyword_docs.rs.html#1380" title="goto source code">[src]</a></span></h1><div class="docblock"><p>The implementing type within a <a href="keyword.trait.html"><code>trait</code></a> or <a href="keyword.impl.html"><code>impl</code></a> block, or the current type within a type
definition.</p>
<p>Within a type definition:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">Node</span> {
<span class="ident">elem</span>: <span class="ident">i32</span>,
<span class="comment">// `Self` is a `Node` here.</span>
<span class="ident">next</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Box</span><span class="op">&lt;</span><span class="self">Self</span><span class="op">&gt;</span><span class="op">&gt;</span>,
}</pre><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Ballow(dead_code)%5D%0Afn%20main()%20%7B%0Astruct%20Node%20%7B%0A%20%20%20%20elem%3A%20i32%2C%0A%20%20%20%20%2F%2F%20%60Self%60%20is%20a%20%60Node%60%20here.%0A%20%20%20%20next%3A%20Option%3CBox%3CSelf%3E%3E%2C%0A%7D%0A%7D&amp;edition=2018">Run</a></div>
<p>In an <a href="keyword.impl.html"><code>impl</code></a> block:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">Foo</span>(<span class="ident">i32</span>);
<span class="kw">impl</span> <span class="ident">Foo</span> {
<span class="kw">fn</span> <span class="ident">new</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span> {
<span class="self">Self</span>(<span class="number">0</span>)
}
}
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">Foo</span>::<span class="ident">new</span>().<span class="number">0</span>, <span class="ident">Foo</span>(<span class="number">0</span>).<span class="number">0</span>);</pre><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Astruct%20Foo(i32)%3B%0A%0Aimpl%20Foo%20%7B%0A%20%20%20%20fn%20new()%20-%3E%20Self%20%7B%0A%20%20%20%20%20%20%20%20Self(0)%0A%20%20%20%20%7D%0A%7D%0A%0Aassert_eq!(Foo%3A%3Anew().0%2C%20Foo(0).0)%3B%0A%7D&amp;edition=2018">Run</a></div>
<p>Generic parameters are implicit with <code>Self</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">Wrap</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="ident">elem</span>: <span class="ident">T</span>,
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Wrap</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">elem</span>: <span class="ident">T</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span> {
<span class="self">Self</span> { <span class="ident">elem</span> }
}
}</pre><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Ballow(dead_code)%5D%0Afn%20main()%20%7B%0Astruct%20Wrap%3CT%3E%20%7B%0A%20%20%20%20elem%3A%20T%2C%0A%7D%0A%0Aimpl%3CT%3E%20Wrap%3CT%3E%20%7B%0A%20%20%20%20fn%20new(elem%3A%20T)%20-%3E%20Self%20%7B%0A%20%20%20%20%20%20%20%20Self%20%7B%20elem%20%7D%0A%20%20%20%20%7D%0A%7D%0A%7D&amp;edition=2018">Run</a></div>
<p>In a <a href="keyword.trait.html"><code>trait</code></a> definition and related <a href="keyword.impl.html"><code>impl</code></a> block:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">trait</span> <span class="ident">Example</span> {
<span class="kw">fn</span> <span class="ident">example</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span>;
}
<span class="kw">struct</span> <span class="ident">Foo</span>(<span class="ident">i32</span>);
<span class="kw">impl</span> <span class="ident">Example</span> <span class="kw">for</span> <span class="ident">Foo</span> {
<span class="kw">fn</span> <span class="ident">example</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span> {
<span class="self">Self</span>(<span class="number">42</span>)
}
}
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">Foo</span>::<span class="ident">example</span>().<span class="number">0</span>, <span class="ident">Foo</span>(<span class="number">42</span>).<span class="number">0</span>);</pre><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Atrait%20Example%20%7B%0A%20%20%20%20fn%20example()%20-%3E%20Self%3B%0A%7D%0A%0Astruct%20Foo(i32)%3B%0A%0Aimpl%20Example%20for%20Foo%20%7B%0A%20%20%20%20fn%20example()%20-%3E%20Self%20%7B%0A%20%20%20%20%20%20%20%20Self(42)%0A%20%20%20%20%7D%0A%7D%0A%0Aassert_eq!(Foo%3A%3Aexample().0%2C%20Foo(42).0)%3B%0A%7D&amp;edition=2018">Run</a></div>
</div></section><section id="search" class="content hidden"></section><section class="footer"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="std"></div>
<script src="../main1.52.0.js"></script><script defer src="../search-index1.52.0.js"></script></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment