<!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"> <title>Operators and Overloading</title> <link rel="stylesheet" type="text/css" href="../rust.css"> <link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"> </head> <body class="rustdoc"> <!--[if lte IE 8]> <div class="warning"> This old browser is unsupported and will most likely display funky things. </div> <![endif]--> <div id="versioninfo"> <img src="https://www.rust-lang.org/logos/rust-logo-32x32-blk.png" width="32" height="32" alt="Rust logo"><br> <span class="white-sticker"><a href="https://www.rust-lang.org">Rust</a> 1.25.0</span><br> <a href="https://github.com/rust-lang/rust/commit/" class="hash white-sticker"></a> </div> <h1 class="title">Operators and Overloading</h1> <nav id="TOC"><ul></ul></nav><p><small>There is a new edition of the book and this is an old link.</small></p> <blockquote> <p>Rust does not allow you to create your own operators or overload arbitrary operators, but the operations and corresponding traits listed in <code>std::ops</code> can be overloaded by implementing the traits associated with the operator.</p> </blockquote> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ops</span>::<span class="ident">Add</span>; <span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>,<span class="ident">PartialEq</span>)]</span> <span class="kw">struct</span> <span class="ident">Point</span> { <span class="ident">x</span>: <span class="ident">i32</span>, <span class="ident">y</span>: <span class="ident">i32</span>, } <span class="kw">impl</span> <span class="ident">Add</span> <span class="kw">for</span> <span class="ident">Point</span> { <span class="kw">type</span> <span class="ident">Output</span> <span class="op">=</span> <span class="ident">Point</span>; <span class="kw">fn</span> <span class="ident">add</span>(<span class="self">self</span>, <span class="ident">other</span>: <span class="ident">Point</span>) <span class="op">-></span> <span class="ident">Point</span> { <span class="ident">Point</span> { <span class="ident">x</span>: <span class="self">self</span>.<span class="ident">x</span> <span class="op">+</span> <span class="ident">other</span>.<span class="ident">x</span>, <span class="ident">y</span>: <span class="self">self</span>.<span class="ident">y</span> <span class="op">+</span> <span class="ident">other</span>.<span class="ident">y</span>, } } } <span class="kw">fn</span> <span class="ident">main</span>() { <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">Point</span> { <span class="ident">x</span>: <span class="number">1</span>, <span class="ident">y</span>: <span class="number">0</span> } <span class="op">+</span> <span class="ident">Point</span> { <span class="ident">x</span>: <span class="number">2</span>, <span class="ident">y</span>: <span class="number">3</span> }, <span class="ident">Point</span> { <span class="ident">x</span>: <span class="number">3</span>, <span class="ident">y</span>: <span class="number">3</span> }); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aops%3A%3AAdd%3B%0A%0A%23%5Bderive(Debug%2CPartialEq)%5D%0Astruct%20Point%20%7B%0A%20%20%20%20x%3A%20i32%2C%0A%20%20%20%20y%3A%20i32%2C%0A%7D%0A%0Aimpl%20Add%20for%20Point%20%7B%0A%20%20%20%20type%20Output%20%3D%20Point%3B%0A%0A%20%20%20%20fn%20add(self%2C%20other%3A%20Point)%20-%3E%20Point%20%7B%0A%20%20%20%20%20%20%20%20Point%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3A%20self.x%20%2B%20other.x%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3A%20self.y%20%2B%20other.y%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20assert_eq!(Point%20%7B%20x%3A%201%2C%20y%3A%200%20%7D%20%2B%20Point%20%7B%20x%3A%202%2C%20y%3A%203%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Point%20%7B%20x%3A%203%2C%20y%3A%203%20%7D)%3B%0A%7D%0A">Run</a></pre> <hr /> <p>Here are the relevant sections in the new and old books:</p> <ul> <li><strong><a href="second-edition/ch19-03-advanced-traits.html#default-generic-type-parameters-and-operator-overloading">In the second edition: Ch 19.03 — Advanced Traits, section Operator Overloading</a></strong></li> <li><a href="../std/ops/index.html">In the Rust documentation: <code>std::ops</code></a></li> <li><small><a href="first-edition/operators-and-overloading.html">In the first edition: Ch 3.32 — Operators and Overloading</a></small></li> </ul> <footer><p> Copyright © 2011 The Rust Project Developers. Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a> or the <a href="https://opensource.org/licenses/MIT">MIT license</a>, at your option. </p><p> This file may not be copied, modified, or distributed except according to those terms. </p></footer> </body> </html>