<!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>Borrow and AsRef</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.27.0</span><br> <a href="https://github.com/rust-lang/rust/commit/" class="hash white-sticker"></a> </div> <h1 class="title">Borrow and AsRef</h1> <p><small>There is a new edition of the book and this is an old link.</small></p> <blockquote> <p>A cheap reference-to-reference conversion. Used to convert a value to a reference value within generic code.</p> </blockquote> <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">AsRef</span><span class="op"><</span><span class="ident">str</span><span class="op">>></span>(<span class="ident">s</span>: <span class="ident">T</span>) { <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"hello"</span>, <span class="ident">s</span>.<span class="ident">as_ref</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%20AsRef%3Cstr%3E%3E(s%3A%20T)%20%7B%0A%20%20%20assert_eq!(%22hello%22%2C%20s.as_ref())%3B%0A%7D%0A%7D">Run</a></pre> <hr /> <p>This chapter does not exist in <a href="second-edition/index.html">the second edition</a>. The best place to learn more about this is <a href="../std/convert/trait.AsRef.html">the Rust documentation</a>.</p> <ul> <li><strong><a href="../std/convert/trait.AsRef.html">In the Rust documentation: <code>convert::AsRef</code></a></strong></li> <li><small><a href="first-edition/borrow-and-asref.html">In the first edition: Ch 4.10 — Borrow and AsRef</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>