<!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 `String` struct in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, String"> <title>std::string::String - 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 struct"> <!--[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'>Struct String</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#methods">Methods</a><div class="sidebar-links"><a href="#method.new">new</a><a href="#method.with_capacity">with_capacity</a><a href="#method.from_utf8">from_utf8</a><a href="#method.from_utf8_lossy">from_utf8_lossy</a><a href="#method.from_utf16">from_utf16</a><a href="#method.from_utf16_lossy">from_utf16_lossy</a><a href="#method.from_raw_parts">from_raw_parts</a><a href="#method.from_utf8_unchecked">from_utf8_unchecked</a><a href="#method.into_bytes">into_bytes</a><a href="#method.as_str">as_str</a><a href="#method.as_mut_str">as_mut_str</a><a href="#method.push_str">push_str</a><a href="#method.capacity">capacity</a><a href="#method.reserve">reserve</a><a href="#method.reserve_exact">reserve_exact</a><a href="#method.shrink_to_fit">shrink_to_fit</a><a href="#method.push">push</a><a href="#method.as_bytes">as_bytes</a><a href="#method.truncate">truncate</a><a href="#method.pop">pop</a><a href="#method.remove">remove</a><a href="#method.retain">retain</a><a href="#method.insert">insert</a><a href="#method.insert_str">insert_str</a><a href="#method.as_mut_vec">as_mut_vec</a><a href="#method.len">len</a><a href="#method.is_empty">is_empty</a><a href="#method.split_off">split_off</a><a href="#method.clear">clear</a><a href="#method.drain">drain</a><a href="#method.splice">splice</a><a href="#method.into_boxed_str">into_boxed_str</a></div><a class="sidebar-title" href="#deref-methods">Methods from Deref<Target=str></a><div class="sidebar-links"><a href="#method.len">len</a><a href="#method.is_empty">is_empty</a><a href="#method.is_char_boundary">is_char_boundary</a><a href="#method.as_bytes">as_bytes</a><a href="#method.as_ptr">as_ptr</a><a href="#method.get">get</a><a href="#method.get_unchecked">get_unchecked</a><a href="#method.slice_unchecked">slice_unchecked</a><a href="#method.split_at">split_at</a><a href="#method.chars">chars</a><a href="#method.char_indices">char_indices</a><a href="#method.bytes">bytes</a><a href="#method.split_whitespace">split_whitespace</a><a href="#method.lines">lines</a><a href="#method.lines_any">lines_any</a><a href="#method.encode_utf16">encode_utf16</a><a href="#method.contains">contains</a><a href="#method.starts_with">starts_with</a><a href="#method.ends_with">ends_with</a><a href="#method.find">find</a><a href="#method.rfind">rfind</a><a href="#method.split">split</a><a href="#method.rsplit">rsplit</a><a href="#method.split_terminator">split_terminator</a><a href="#method.rsplit_terminator">rsplit_terminator</a><a href="#method.splitn">splitn</a><a href="#method.rsplitn">rsplitn</a><a href="#method.matches">matches</a><a href="#method.rmatches">rmatches</a><a href="#method.match_indices">match_indices</a><a href="#method.rmatch_indices">rmatch_indices</a><a href="#method.trim">trim</a><a href="#method.trim_left">trim_left</a><a href="#method.trim_right">trim_right</a><a href="#method.trim_matches">trim_matches</a><a href="#method.trim_left_matches">trim_left_matches</a><a href="#method.trim_right_matches">trim_right_matches</a><a href="#method.parse">parse</a><a href="#method.replace">replace</a><a href="#method.replacen">replacen</a><a href="#method.to_lowercase">to_lowercase</a><a href="#method.to_uppercase">to_uppercase</a><a href="#method.escape_debug">escape_debug</a><a href="#method.escape_default">escape_default</a><a href="#method.escape_unicode">escape_unicode</a><a href="#method.repeat">repeat</a><a href="#method.is_ascii">is_ascii</a><a href="#method.to_ascii_uppercase">to_ascii_uppercase</a><a href="#method.to_ascii_lowercase">to_ascii_lowercase</a><a href="#method.eq_ignore_ascii_case">eq_ignore_ascii_case</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-Pattern%3C%27a%3E">Pattern<'a></a><a href="#impl-From%3C%26%27a%20str%3E">From<&'a str></a><a href="#impl-From%3CCow%3C%27a%2C%20str%3E%3E">From<Cow<'a, str>></a><a href="#impl-From%3CString%3E">From<String></a><a href="#impl-From%3CBox%3Cstr%3E%3E">From<Box<str>></a><a href="#impl-IndexMut%3CRangeFrom%3Cusize%3E%3E">IndexMut<RangeFrom<usize>></a><a href="#impl-IndexMut%3CRangeToInclusive%3Cusize%3E%3E">IndexMut<RangeToInclusive<usize>></a><a href="#impl-IndexMut%3CRangeTo%3Cusize%3E%3E">IndexMut<RangeTo<usize>></a><a href="#impl-IndexMut%3CRangeInclusive%3Cusize%3E%3E">IndexMut<RangeInclusive<usize>></a><a href="#impl-IndexMut%3CRange%3Cusize%3E%3E">IndexMut<Range<usize>></a><a href="#impl-IndexMut%3CRangeFull%3E">IndexMut<RangeFull></a><a href="#impl-FromStr">FromStr</a><a href="#impl-ToString">ToString</a><a href="#impl-Borrow%3Cstr%3E">Borrow<str></a><a href="#impl-Ord">Ord</a><a href="#impl-Clone">Clone</a><a href="#impl-DerefMut">DerefMut</a><a href="#impl-Extend%3CCow%3C%27a%2C%20str%3E%3E">Extend<Cow<'a, str>></a><a href="#impl-Extend%3C%26%27a%20str%3E">Extend<&'a str></a><a href="#impl-Extend%3C%26%27a%20char%3E">Extend<&'a char></a><a href="#impl-Extend%3CString%3E">Extend<String></a><a href="#impl-Extend%3Cchar%3E">Extend<char></a><a href="#impl-Hash">Hash</a><a href="#impl-Index%3CRangeTo%3Cusize%3E%3E">Index<RangeTo<usize>></a><a href="#impl-Index%3CRangeToInclusive%3Cusize%3E%3E">Index<RangeToInclusive<usize>></a><a href="#impl-Index%3CRangeFull%3E">Index<RangeFull></a><a href="#impl-Index%3CRangeInclusive%3Cusize%3E%3E">Index<RangeInclusive<usize>></a><a href="#impl-Index%3CRange%3Cusize%3E%3E">Index<Range<usize>></a><a href="#impl-Index%3CRangeFrom%3Cusize%3E%3E">Index<RangeFrom<usize>></a><a href="#impl-PartialEq%3Cstr%3E">PartialEq<str></a><a href="#impl-PartialEq%3C%26%27a%20str%3E">PartialEq<&'a str></a><a href="#impl-PartialEq%3CString%3E">PartialEq<String></a><a href="#impl-PartialEq%3CCow%3C%27a%2C%20str%3E%3E">PartialEq<Cow<'a, str>></a><a href="#impl-Add%3C%26%27a%20str%3E">Add<&'a str></a><a href="#impl-Eq">Eq</a><a href="#impl-Display">Display</a><a href="#impl-AddAssign%3C%26%27a%20str%3E">AddAssign<&'a str></a><a href="#impl-AsRef%3C%5Bu8%5D%3E">AsRef<[u8]></a><a href="#impl-AsRef%3Cstr%3E">AsRef<str></a><a href="#impl-Default">Default</a><a href="#impl-FromIterator%3CCow%3C%27a%2C%20str%3E%3E">FromIterator<Cow<'a, str>></a><a href="#impl-FromIterator%3C%26%27a%20str%3E">FromIterator<&'a str></a><a href="#impl-FromIterator%3C%26%27a%20char%3E">FromIterator<&'a char></a><a href="#impl-FromIterator%3Cchar%3E">FromIterator<char></a><a href="#impl-FromIterator%3CString%3E">FromIterator<String></a><a href="#impl-Debug">Debug</a><a href="#impl-Deref">Deref</a><a href="#impl-PartialOrd%3CString%3E">PartialOrd<String></a><a href="#impl-Write">Write</a><a href="#impl-AsRef%3COsStr%3E">AsRef<OsStr></a><a href="#impl-ToSocketAddrs">ToSocketAddrs</a><a href="#impl-AsRef%3CPath%3E">AsRef<Path></a></div></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>string</a></p><script>window.sidebarCurrent = {name: 'String', ty: 'struct', 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'>Struct <a href='../index.html'>std</a>::<wbr><a href='index.html'>string</a>::<wbr><a class="struct" href=''>String</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/alloc/string.rs.html#294-296' title='goto source code'>[src]</a></span></h1> <pre class='rust struct'>pub struct String { /* fields omitted */ }</pre><div class='docblock'><p>A UTF-8 encoded, growable string.</p> <p>The <code>String</code> type is the most common string type that has ownership over the contents of the string. It has a close relationship with its borrowed counterpart, the primitive <a href="../../std/primitive.str.html"><code>str</code></a>.</p> <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1> <p>You can create a <code>String</code> from a literal string with <a href="#method.from"><code>String::from</code></a>:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">hello</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Hello, world!"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20hello%20%3D%20String%3A%3Afrom(%22Hello%2C%20world!%22)%3B%0A%7D">Run</a></pre> <p>You can append a <a href="../../std/primitive.char.html"><code>char</code></a> to a <code>String</code> with the <a href="#method.push"><code>push</code></a> method, and append a <a href="../../std/primitive.str.html"><code>&str</code></a> with the <a href="#method.push_str"><code>push_str</code></a> method:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">hello</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Hello, "</span>); <span class="ident">hello</span>.<span class="ident">push</span>(<span class="string">'w'</span>); <span class="ident">hello</span>.<span class="ident">push_str</span>(<span class="string">"orld!"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20hello%20%3D%20String%3A%3Afrom(%22Hello%2C%20%22)%3B%0A%0Ahello.push('w')%3B%0Ahello.push_str(%22orld!%22)%3B%0A%7D">Run</a></pre> <p>If you have a vector of UTF-8 bytes, you can create a <code>String</code> from it with the <a href="#method.from_utf8"><code>from_utf8</code></a> method:</p> <pre class="rust rust-example-rendered"> <span class="comment">// some bytes, in a vector</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">240</span>, <span class="number">159</span>, <span class="number">146</span>, <span class="number">150</span>]; <span class="comment">// We know these bytes are valid, so we'll use `unwrap()`.</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from_utf8</span>(<span class="ident">sparkle_heart</span>).<span class="ident">unwrap</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"💖"</span>, <span class="ident">sparkle_heart</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20some%20bytes%2C%20in%20a%20vector%0Alet%20sparkle_heart%20%3D%20vec!%5B240%2C%20159%2C%20146%2C%20150%5D%3B%0A%0A%2F%2F%20We%20know%20these%20bytes%20are%20valid%2C%20so%20we'll%20use%20%60unwrap()%60.%0Alet%20sparkle_heart%20%3D%20String%3A%3Afrom_utf8(sparkle_heart).unwrap()%3B%0A%0Aassert_eq!(%22%F0%9F%92%96%22%2C%20sparkle_heart)%3B%0A%7D">Run</a></pre> <h1 id="utf-8" class="section-header"><a href="#utf-8">UTF-8</a></h1> <p><code>String</code>s are always valid UTF-8. This has a few implications, the first of which is that if you need a non-UTF-8 string, consider <a href="../../std/ffi/struct.OsString.html"><code>OsString</code></a>. It is similar, but without the UTF-8 constraint. The second implication is that you cannot index into a <code>String</code>:</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">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"hello"</span>; <span class="macro">println</span><span class="macro">!</span>(<span class="string">"The first letter of s is {}"</span>, <span class="ident">s</span>[<span class="number">0</span>]); <span class="comment">// ERROR!!!</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22hello%22%3B%0A%0Aprintln!(%22The%20first%20letter%20of%20s%20is%20%7B%7D%22%2C%20s%5B0%5D)%3B%20%2F%2F%20ERROR!!!%0A%7D">Run</a></pre> <p>Indexing is intended to be a constant-time operation, but UTF-8 encoding does not allow us to do this. Furthermore, it's not clear what sort of thing the index should return: a byte, a codepoint, or a grapheme cluster. The <a href="#method.bytes"><code>bytes</code></a> and <a href="#method.chars"><code>chars</code></a> methods return iterators over the first two, respectively.</p> <h1 id="deref" class="section-header"><a href="#deref">Deref</a></h1> <p><code>String</code>s implement <a href="../../std/ops/trait.Deref.html"><code>Deref</code></a><code><Target=str></code>, and so inherit all of <a href="../../std/primitive.str.html"><code>str</code></a>'s methods. In addition, this means that you can pass a <code>String</code> to a function which takes a <a href="../../std/primitive.str.html"><code>&str</code></a> by using an ampersand (<code>&</code>):</p> <pre class="rust rust-example-rendered"> <span class="kw">fn</span> <span class="ident">takes_str</span>(<span class="ident">s</span>: <span class="kw-2">&</span><span class="ident">str</span>) { } <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Hello"</span>); <span class="ident">takes_str</span>(<span class="kw-2">&</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%20takes_str(s%3A%20%26str)%20%7B%20%7D%0A%0Alet%20s%20%3D%20String%3A%3Afrom(%22Hello%22)%3B%0A%0Atakes_str(%26s)%3B%0A%7D">Run</a></pre> <p>This will create a <a href="../../std/primitive.str.html"><code>&str</code></a> from the <code>String</code> and pass it in. This conversion is very inexpensive, and so generally, functions will accept <a href="../../std/primitive.str.html"><code>&str</code></a>s as arguments unless they need a <code>String</code> for some specific reason.</p> <p>In certain cases Rust doesn't have enough information to make this conversion, known as <a href="../../std/ops/trait.Deref.html"><code>Deref</code></a> coercion. In the following example a string slice <a href="../../std/primitive.str.html"><code>&'a str</code></a> implements the trait <code>TraitExample</code>, and the function <code>example_func</code> takes anything that implements the trait. In this case Rust would need to make two implicit conversions, which Rust doesn't have the means to do. For that reason, the following example will not compile.</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">trait</span> <span class="ident">TraitExample</span> {} <span class="kw">impl</span><span class="op"><</span><span class="lifetime">'a</span><span class="op">></span> <span class="ident">TraitExample</span> <span class="kw">for</span> <span class="kw-2">&</span><span class="lifetime">'a</span> <span class="ident">str</span> {} <span class="kw">fn</span> <span class="ident">example_func</span><span class="op"><</span><span class="ident">A</span>: <span class="ident">TraitExample</span><span class="op">></span>(<span class="ident">example_arg</span>: <span class="ident">A</span>) {} <span class="kw">fn</span> <span class="ident">main</span>() { <span class="kw">let</span> <span class="ident">example_string</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"example_string"</span>); <span class="ident">example_func</span>(<span class="kw-2">&</span><span class="ident">example_string</span>); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Atrait%20TraitExample%20%7B%7D%0A%0Aimpl%3C'a%3E%20TraitExample%20for%20%26'a%20str%20%7B%7D%0A%0Afn%20example_func%3CA%3A%20TraitExample%3E(example_arg%3A%20A)%20%7B%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20example_string%20%3D%20String%3A%3Afrom(%22example_string%22)%3B%0A%20%20%20%20example_func(%26example_string)%3B%0A%7D%0A">Run</a></pre> <p>There are two options that would work instead. The first would be to change the line <code>example_func(&example_string);</code> to <code>example_func(example_string.as_str());</code>, using the method <a href="struct.String.html#method.as_str"><code>as_str()</code></a> to explicitly extract the string slice containing the string. The second way changes <code>example_func(&example_string);</code> to <code>example_func(&*example_string);</code>. In this case we are dereferencing a <code>String</code> to a <a href="../../std/primitive.str.html"><code>str</code></a>, then referencing the <a href="../../std/primitive.str.html"><code>str</code></a> back to <a href="../../std/primitive.str.html"><code>&str</code></a>. The second way is more idiomatic, however both work to do the conversion explicitly rather than relying on the implicit conversion.</p> <h1 id="representation" class="section-header"><a href="#representation">Representation</a></h1> <p>A <code>String</code> is made up of three components: a pointer to some bytes, a length, and a capacity. The pointer points to an internal buffer <code>String</code> uses to store its data. The length is the number of bytes currently stored in the buffer, and the capacity is the size of the buffer in bytes. As such, the length will always be less than or equal to the capacity.</p> <p>This buffer is always stored on the heap.</p> <p>You can look at these with the <a href="#method.as_ptr"><code>as_ptr</code></a>, <a href="#method.len"><code>len</code></a>, and <a href="#method.capacity"><code>capacity</code></a> methods:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">mem</span>; <span class="kw">let</span> <span class="ident">story</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Once upon a time..."</span>); <span class="kw">let</span> <span class="ident">ptr</span> <span class="op">=</span> <span class="ident">story</span>.<span class="ident">as_ptr</span>(); <span class="kw">let</span> <span class="ident">len</span> <span class="op">=</span> <span class="ident">story</span>.<span class="ident">len</span>(); <span class="kw">let</span> <span class="ident">capacity</span> <span class="op">=</span> <span class="ident">story</span>.<span class="ident">capacity</span>(); <span class="comment">// story has nineteen bytes</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">19</span>, <span class="ident">len</span>); <span class="comment">// Now that we have our parts, we throw the story away.</span> <span class="ident">mem</span>::<span class="ident">forget</span>(<span class="ident">story</span>); <span class="comment">// We can re-build a String out of ptr, len, and capacity. This is all</span> <span class="comment">// unsafe because we are responsible for making sure the components are</span> <span class="comment">// valid:</span> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">String</span>::<span class="ident">from_raw_parts</span>(<span class="ident">ptr</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw-2">mut</span> _, <span class="ident">len</span>, <span class="ident">capacity</span>) } ; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Once upon a time..."</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%0Ause%20std%3A%3Amem%3B%0A%0Alet%20story%20%3D%20String%3A%3Afrom(%22Once%20upon%20a%20time...%22)%3B%0A%0Alet%20ptr%20%3D%20story.as_ptr()%3B%0Alet%20len%20%3D%20story.len()%3B%0Alet%20capacity%20%3D%20story.capacity()%3B%0A%0A%2F%2F%20story%20has%20nineteen%20bytes%0Aassert_eq!(19%2C%20len)%3B%0A%0A%2F%2F%20Now%20that%20we%20have%20our%20parts%2C%20we%20throw%20the%20story%20away.%0Amem%3A%3Aforget(story)%3B%0A%0A%2F%2F%20We%20can%20re-build%20a%20String%20out%20of%20ptr%2C%20len%2C%20and%20capacity.%20This%20is%20all%0A%2F%2F%20unsafe%20because%20we%20are%20responsible%20for%20making%20sure%20the%20components%20are%0A%2F%2F%20valid%3A%0Alet%20s%20%3D%20unsafe%20%7B%20String%3A%3Afrom_raw_parts(ptr%20as%20*mut%20_%2C%20len%2C%20capacity)%20%7D%20%3B%0A%0Aassert_eq!(String%3A%3Afrom(%22Once%20upon%20a%20time...%22)%2C%20s)%3B%0A%7D">Run</a></pre> <p>If a <code>String</code> has enough capacity, adding elements to it will not re-allocate. For example, consider this program:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>(); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">s</span>.<span class="ident">capacity</span>()); <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="number">5</span> { <span class="ident">s</span>.<span class="ident">push_str</span>(<span class="string">"hello"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">s</span>.<span class="ident">capacity</span>()); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Anew()%3B%0A%0Aprintln!(%22%7B%7D%22%2C%20s.capacity())%3B%0A%0Afor%20_%20in%200..5%20%7B%0A%20%20%20%20s.push_str(%22hello%22)%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s.capacity())%3B%0A%7D%0A%7D">Run</a></pre> <p>This will output the following:</p> <pre><code class="language-text">0 5 10 20 20 40 </code></pre> <p>At first, we have no memory allocated at all, but as we append to the string, it increases its capacity appropriately. If we instead use the <a href="#method.with_capacity"><code>with_capacity</code></a> method to allocate the correct capacity initially:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">25</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">s</span>.<span class="ident">capacity</span>()); <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="number">5</span> { <span class="ident">s</span>.<span class="ident">push_str</span>(<span class="string">"hello"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">s</span>.<span class="ident">capacity</span>()); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Awith_capacity(25)%3B%0A%0Aprintln!(%22%7B%7D%22%2C%20s.capacity())%3B%0A%0Afor%20_%20in%200..5%20%7B%0A%20%20%20%20s.push_str(%22hello%22)%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s.capacity())%3B%0A%7D%0A%7D">Run</a></pre> <p>We end up with a different output:</p> <pre><code class="language-text">25 25 25 25 25 25 </code></pre> <p>Here, there's no need to allocate more memory inside the loop.</p> </div> <h2 id='methods' class='small-section-header'> Methods<a href='#methods' class='anchor'></a> </h2> <h3 id='impl' class='impl'><span class='in-band'><code>impl <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#362-1495' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.new' class="method"><span id='new.v' class='invisible'><code>pub fn <a href='#method.new' class='fnname'>new</a>() -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#383-385' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a new empty <code>String</code>.</p> <p>Given that the <code>String</code> is empty, this will not allocate any initial buffer. While that means that this initial operation is very inexpensive, but may cause excessive allocation later, when you add data. If you have an idea of how much data the <code>String</code> will hold, consider the <a href="#method.with_capacity"><code>with_capacity</code></a> method to prevent excessive re-allocation.</p> <h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20String%3A%3Anew()%3B%0A%7D">Run</a></pre> </div><h4 id='method.with_capacity' class="method"><span id='with_capacity.v' class='invisible'><code>pub fn <a href='#method.with_capacity' class='fnname'>with_capacity</a>(capacity: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#426-428' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a new empty <code>String</code> with a particular capacity.</p> <p><code>String</code>s have an internal buffer to hold their data. The capacity is the length of that buffer, and can be queried with the <a href="#method.capacity"><code>capacity</code></a> method. This method creates an empty <code>String</code>, but one with an initial buffer that can hold <code>capacity</code> bytes. This is useful when you may be appending a bunch of data to the <code>String</code>, reducing the number of reallocations it needs to do.</p> <p>If the given capacity is <code>0</code>, no allocation will occur, and this method is identical to the <a href="#method.new"><code>new</code></a> method.</p> <h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="comment">// The String contains no chars, even though it has capacity for more</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">len</span>(), <span class="number">0</span>); <span class="comment">// These are all done without reallocating...</span> <span class="kw">let</span> <span class="ident">cap</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">capacity</span>(); <span class="kw">for</span> <span class="ident">i</span> <span class="kw">in</span> <span class="number">0</span>..<span class="number">10</span> { <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'a'</span>); } <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">capacity</span>(), <span class="ident">cap</span>); <span class="comment">// ...but this may make the vector reallocate</span> <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'a'</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Awith_capacity(10)%3B%0A%0A%2F%2F%20The%20String%20contains%20no%20chars%2C%20even%20though%20it%20has%20capacity%20for%20more%0Aassert_eq!(s.len()%2C%200)%3B%0A%0A%2F%2F%20These%20are%20all%20done%20without%20reallocating...%0Alet%20cap%20%3D%20s.capacity()%3B%0Afor%20i%20in%200..10%20%7B%0A%20%20%20%20s.push('a')%3B%0A%7D%0A%0Aassert_eq!(s.capacity()%2C%20cap)%3B%0A%0A%2F%2F%20...but%20this%20may%20make%20the%20vector%20reallocate%0As.push('a')%3B%0A%7D">Run</a></pre> </div><h4 id='method.from_utf8' class="method"><span id='from_utf8.v' class='invisible'><code>pub fn <a href='#method.from_utf8' class='fnname'>from_utf8</a>(vec: <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>, <a class="struct" href="../../std/string/struct.FromUtf8Error.html" title="struct std::string::FromUtf8Error">FromUtf8Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#502-512' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a vector of bytes to a <code>String</code>.</p> <p>A string slice (<a href="../../std/primitive.str.html"><code>&str</code></a>) is made of bytes (<a href="../../std/primitive.u8.html"><code>u8</code></a>), and a vector of bytes (<a href="../../std/vec/struct.Vec.html"><code>Vec<u8></code></a>) is made of bytes, so this function converts between the two. Not all byte slices are valid <code>String</code>s, however: <code>String</code> requires that it is valid UTF-8. <code>from_utf8()</code> checks to ensure that the bytes are valid UTF-8, and then does the conversion.</p> <p>If you are sure that the byte slice is valid UTF-8, and you don't want to incur the overhead of the validity check, there is an unsafe version of this function, <a href="struct.String.html#method.from_utf8_unchecked"><code>from_utf8_unchecked</code></a>, which has the same behavior but skips the check.</p> <p>This method will take care to not copy the vector, for efficiency's sake.</p> <p>If you need a <a href="../../std/primitive.str.html"><code>&str</code></a> instead of a <code>String</code>, consider <a href="../../std/str/fn.from_utf8.html"><code>str::from_utf8</code></a>.</p> <p>The inverse of this method is <a href="struct.String.html#method.as_bytes"><code>as_bytes</code></a>.</p> <h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1> <p>Returns <a href="../../stdresult/enum.Result.html#variant.Err"><code>Err</code></a> if the slice is not UTF-8 with a description as to why the provided bytes are not UTF-8. The vector you moved in is also included.</p> <h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="comment">// some bytes, in a vector</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">240</span>, <span class="number">159</span>, <span class="number">146</span>, <span class="number">150</span>]; <span class="comment">// We know these bytes are valid, so we'll use `unwrap()`.</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from_utf8</span>(<span class="ident">sparkle_heart</span>).<span class="ident">unwrap</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"💖"</span>, <span class="ident">sparkle_heart</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20some%20bytes%2C%20in%20a%20vector%0Alet%20sparkle_heart%20%3D%20vec!%5B240%2C%20159%2C%20146%2C%20150%5D%3B%0A%0A%2F%2F%20We%20know%20these%20bytes%20are%20valid%2C%20so%20we'll%20use%20%60unwrap()%60.%0Alet%20sparkle_heart%20%3D%20String%3A%3Afrom_utf8(sparkle_heart).unwrap()%3B%0A%0Aassert_eq!(%22%F0%9F%92%96%22%2C%20sparkle_heart)%3B%0A%7D">Run</a></pre> <p>Incorrect bytes:</p> <pre class="rust rust-example-rendered"> <span class="comment">// some invalid bytes, in a vector</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">0</span>, <span class="number">159</span>, <span class="number">146</span>, <span class="number">150</span>]; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">String</span>::<span class="ident">from_utf8</span>(<span class="ident">sparkle_heart</span>).<span class="ident">is_err</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20some%20invalid%20bytes%2C%20in%20a%20vector%0Alet%20sparkle_heart%20%3D%20vec!%5B0%2C%20159%2C%20146%2C%20150%5D%3B%0A%0Aassert!(String%3A%3Afrom_utf8(sparkle_heart).is_err())%3B%0A%7D">Run</a></pre> <p>See the docs for <a href="struct.FromUtf8Error.html"><code>FromUtf8Error</code></a> for more details on what you can do with this error.</p> </div><h4 id='method.from_utf8_lossy' class="method"><span id='from_utf8_lossy.v' class='invisible'><code>pub fn <a href='#method.from_utf8_lossy' class='fnname'>from_utf8_lossy</a>(v: <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>) -> <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#564-594' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a slice of bytes to a string, including invalid characters.</p> <p>Strings are made of bytes (<a href="../../std/primitive.u8.html"><code>u8</code></a>), and a slice of bytes (<a href="../../std/primitive.slice.html"><code>&[u8]</code></a>) is made of bytes, so this function converts between the two. Not all byte slices are valid strings, however: strings are required to be valid UTF-8. During this conversion, <code>from_utf8_lossy()</code> will replace any invalid UTF-8 sequences with <code>U+FFFD REPLACEMENT CHARACTER</code>, which looks like this: �</p> <p>If you are sure that the byte slice is valid UTF-8, and you don't want to incur the overhead of the conversion, there is an unsafe version of this function, <a href="struct.String.html#method.from_utf8_unchecked"><code>from_utf8_unchecked</code></a>, which has the same behavior but skips the checks.</p> <p>This function returns a <a href="../../std/borrow/enum.Cow.html"><code>Cow<'a, str></code></a>. If our byte slice is invalid UTF-8, then we need to insert the replacement characters, which will change the size of the string, and hence, require a <code>String</code>. But if it's already valid UTF-8, we don't need a new allocation. This return type allows us to handle both cases.</p> <h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="comment">// some bytes, in a vector</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">240</span>, <span class="number">159</span>, <span class="number">146</span>, <span class="number">150</span>]; <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from_utf8_lossy</span>(<span class="kw-2">&</span><span class="ident">sparkle_heart</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"💖"</span>, <span class="ident">sparkle_heart</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20some%20bytes%2C%20in%20a%20vector%0Alet%20sparkle_heart%20%3D%20vec!%5B240%2C%20159%2C%20146%2C%20150%5D%3B%0A%0Alet%20sparkle_heart%20%3D%20String%3A%3Afrom_utf8_lossy(%26sparkle_heart)%3B%0A%0Aassert_eq!(%22%F0%9F%92%96%22%2C%20sparkle_heart)%3B%0A%7D">Run</a></pre> <p>Incorrect bytes:</p> <pre class="rust rust-example-rendered"> <span class="comment">// some invalid bytes</span> <span class="kw">let</span> <span class="ident">input</span> <span class="op">=</span> <span class="string">b"Hello \xF0\x90\x80World"</span>; <span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from_utf8_lossy</span>(<span class="ident">input</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"Hello �World"</span>, <span class="ident">output</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20some%20invalid%20bytes%0Alet%20input%20%3D%20b%22Hello%20%5CxF0%5Cx90%5Cx80World%22%3B%0Alet%20output%20%3D%20String%3A%3Afrom_utf8_lossy(input)%3B%0A%0Aassert_eq!(%22Hello%20%EF%BF%BDWorld%22%2C%20output)%3B%0A%7D">Run</a></pre> </div><h4 id='method.from_utf16' class="method"><span id='from_utf16.v' class='invisible'><code>pub fn <a href='#method.from_utf16' class='fnname'>from_utf16</a>(v: <a class="primitive" href="../primitive.slice.html">&[</a><a class="primitive" href="../primitive.u16.html">u16</a><a class="primitive" href="../primitive.slice.html">]</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>, <a class="struct" href="../../std/string/struct.FromUtf16Error.html" title="struct std::string::FromUtf16Error">FromUtf16Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#618-620' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Decode a UTF-16 encoded vector <code>v</code> into a <code>String</code>, returning <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> if <code>v</code> contains any invalid data.</p> <h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="comment">// 𝄞music</span> <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">0xD834</span>, <span class="number">0xDD1E</span>, <span class="number">0x006d</span>, <span class="number">0x0075</span>, <span class="number">0x0073</span>, <span class="number">0x0069</span>, <span class="number">0x0063</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"𝄞music"</span>), <span class="ident">String</span>::<span class="ident">from_utf16</span>(<span class="ident">v</span>).<span class="ident">unwrap</span>()); <span class="comment">// 𝄞mu<invalid>ic</span> <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">0xD834</span>, <span class="number">0xDD1E</span>, <span class="number">0x006d</span>, <span class="number">0x0075</span>, <span class="number">0xD800</span>, <span class="number">0x0069</span>, <span class="number">0x0063</span>]; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">String</span>::<span class="ident">from_utf16</span>(<span class="ident">v</span>).<span class="ident">is_err</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20%F0%9D%84%9Emusic%0Alet%20v%20%3D%20%26%5B0xD834%2C%200xDD1E%2C%200x006d%2C%200x0075%2C%0A%20%20%20%20%20%20%20%20%20%200x0073%2C%200x0069%2C%200x0063%5D%3B%0Aassert_eq!(String%3A%3Afrom(%22%F0%9D%84%9Emusic%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20String%3A%3Afrom_utf16(v).unwrap())%3B%0A%0A%2F%2F%20%F0%9D%84%9Emu%3Cinvalid%3Eic%0Alet%20v%20%3D%20%26%5B0xD834%2C%200xDD1E%2C%200x006d%2C%200x0075%2C%0A%20%20%20%20%20%20%20%20%20%200xD800%2C%200x0069%2C%200x0063%5D%3B%0Aassert!(String%3A%3Afrom_utf16(v).is_err())%3B%0A%7D">Run</a></pre> </div><h4 id='method.from_utf16_lossy' class="method"><span id='from_utf16_lossy.v' class='invisible'><code>pub fn <a href='#method.from_utf16_lossy' class='fnname'>from_utf16_lossy</a>(v: <a class="primitive" href="../primitive.slice.html">&[</a><a class="primitive" href="../primitive.u16.html">u16</a><a class="primitive" href="../primitive.slice.html">]</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#647-649' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Decode a UTF-16 encoded slice <code>v</code> into a <code>String</code>, replacing invalid data with the replacement character (U+FFFD).</p> <p>Unlike <a href="#method.from_utf8_lossy"><code>from_utf8_lossy</code></a> which returns a <a href="../borrow/enum.Cow.html"><code>Cow<'a, str></code></a>, <code>from_utf16_lossy</code> returns a <code>String</code> since the UTF-16 to UTF-8 conversion requires a memory allocation.</p> <h1 id="examples-6" class="section-header"><a href="#examples-6">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="comment">// 𝄞mus<invalid>ic<invalid></span> <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">0xD834</span>, <span class="number">0xDD1E</span>, <span class="number">0x006d</span>, <span class="number">0x0075</span>, <span class="number">0x0073</span>, <span class="number">0xDD1E</span>, <span class="number">0x0069</span>, <span class="number">0x0063</span>, <span class="number">0xD834</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"𝄞mus\u{FFFD}ic\u{FFFD}"</span>), <span class="ident">String</span>::<span class="ident">from_utf16_lossy</span>(<span class="ident">v</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20%F0%9D%84%9Emus%3Cinvalid%3Eic%3Cinvalid%3E%0Alet%20v%20%3D%20%26%5B0xD834%2C%200xDD1E%2C%200x006d%2C%200x0075%2C%0A%20%20%20%20%20%20%20%20%20%200x0073%2C%200xDD1E%2C%200x0069%2C%200x0063%2C%0A%20%20%20%20%20%20%20%20%20%200xD834%5D%3B%0A%0Aassert_eq!(String%3A%3Afrom(%22%F0%9D%84%9Emus%5Cu%7BFFFD%7Dic%5Cu%7BFFFD%7D%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20String%3A%3Afrom_utf16_lossy(v))%3B%0A%7D">Run</a></pre> </div><h4 id='method.from_raw_parts' class="method"><span id='from_raw_parts.v' class='invisible'><code>pub unsafe fn <a href='#method.from_raw_parts' class='fnname'>from_raw_parts</a>(<br> buf: <a class="primitive" href="../primitive.pointer.html">*mut </a><a class="primitive" href="../primitive.u8.html">u8</a>, <br> length: <a class="primitive" href="../primitive.usize.html">usize</a>, <br> capacity: <a class="primitive" href="../primitive.usize.html">usize</a><br>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#694-696' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a new <code>String</code> from a length, capacity, and pointer.</p> <h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1> <p>This is highly unsafe, due to the number of invariants that aren't checked:</p> <ul> <li>The memory at <code>ptr</code> needs to have been previously allocated by the same allocator the standard library uses.</li> <li><code>length</code> needs to be less than or equal to <code>capacity</code>.</li> <li><code>capacity</code> needs to be the correct value.</li> </ul> <p>Violating these may cause problems like corrupting the allocator's internal data structures.</p> <p>The ownership of <code>ptr</code> is effectively transferred to the <code>String</code> which may then deallocate, reallocate or change the contents of memory pointed to by the pointer at will. Ensure that nothing else uses the pointer after calling this function.</p> <h1 id="examples-7" class="section-header"><a href="#examples-7">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">mem</span>; <span class="kw">unsafe</span> { <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="kw">let</span> <span class="ident">ptr</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">as_ptr</span>(); <span class="kw">let</span> <span class="ident">len</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">len</span>(); <span class="kw">let</span> <span class="ident">capacity</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">capacity</span>(); <span class="ident">mem</span>::<span class="ident">forget</span>(<span class="ident">s</span>); <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from_raw_parts</span>(<span class="ident">ptr</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw-2">mut</span> _, <span class="ident">len</span>, <span class="ident">capacity</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"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%0Ause%20std%3A%3Amem%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20let%20s%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0A%20%20%20%20let%20ptr%20%3D%20s.as_ptr()%3B%0A%20%20%20%20let%20len%20%3D%20s.len()%3B%0A%20%20%20%20let%20capacity%20%3D%20s.capacity()%3B%0A%0A%20%20%20%20mem%3A%3Aforget(s)%3B%0A%0A%20%20%20%20let%20s%20%3D%20String%3A%3Afrom_raw_parts(ptr%20as%20*mut%20_%2C%20len%2C%20capacity)%3B%0A%0A%20%20%20%20assert_eq!(String%3A%3Afrom(%22hello%22)%2C%20s)%3B%0A%7D%0A%7D">Run</a></pre> </div><h4 id='method.from_utf8_unchecked' class="method"><span id='from_utf8_unchecked.v' class='invisible'><code>pub unsafe fn <a href='#method.from_utf8_unchecked' class='fnname'>from_utf8_unchecked</a>(bytes: <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#728-730' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a vector of bytes to a <code>String</code> without checking that the string contains valid UTF-8.</p> <p>See the safe version, <a href="struct.String.html#method.from_utf8"><code>from_utf8</code></a>, for more details.</p> <h1 id="safety-1" class="section-header"><a href="#safety-1">Safety</a></h1> <p>This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, it may cause memory unsafety issues with future users of the <code>String</code>, as the rest of the standard library assumes that <code>String</code>s are valid UTF-8.</p> <h1 id="examples-8" class="section-header"><a href="#examples-8">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="comment">// some bytes, in a vector</span> <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">240</span>, <span class="number">159</span>, <span class="number">146</span>, <span class="number">150</span>]; <span class="kw">let</span> <span class="ident">sparkle_heart</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">String</span>::<span class="ident">from_utf8_unchecked</span>(<span class="ident">sparkle_heart</span>) }; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"💖"</span>, <span class="ident">sparkle_heart</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%2F%2F%20some%20bytes%2C%20in%20a%20vector%0Alet%20sparkle_heart%20%3D%20vec!%5B240%2C%20159%2C%20146%2C%20150%5D%3B%0A%0Alet%20sparkle_heart%20%3D%20unsafe%20%7B%0A%20%20%20%20String%3A%3Afrom_utf8_unchecked(sparkle_heart)%0A%7D%3B%0A%0Aassert_eq!(%22%F0%9F%92%96%22%2C%20sparkle_heart)%3B%0A%7D">Run</a></pre> </div><h4 id='method.into_bytes' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></h3><code class="content"><span class="where fmt-newline">impl <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></span></code></div></div><span id='into_bytes.v' class='invisible'><code>pub fn <a href='#method.into_bytes' class='fnname'>into_bytes</a>(self) -> <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#748-750' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a <code>String</code> into a byte vector.</p> <p>This consumes the <code>String</code>, so we do not need to copy its contents.</p> <h1 id="examples-9" class="section-header"><a href="#examples-9">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="kw">let</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">into_bytes</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span>[<span class="number">104</span>, <span class="number">101</span>, <span class="number">108</span>, <span class="number">108</span>, <span class="number">111</span>][..], <span class="kw-2">&</span><span class="ident">bytes</span>[..]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0Alet%20bytes%20%3D%20s.into_bytes()%3B%0A%0Aassert_eq!(%26%5B104%2C%20101%2C%20108%2C%20108%2C%20111%5D%5B..%5D%2C%20%26bytes%5B..%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_str' class="method"><span id='as_str.v' class='invisible'><code>pub fn <a href='#method.as_str' class='fnname'>as_str</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.7.0'>1.7.0</div><a class='srclink' href='../../src/alloc/string.rs.html#765-767' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extracts a string slice containing the entire string.</p> <h1 id="examples-10" class="section-header"><a href="#examples-10">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"foo"</span>, <span class="ident">s</span>.<span class="ident">as_str</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0Aassert_eq!(%22foo%22%2C%20s.as_str())%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_mut_str' class="method"><span id='as_mut_str.v' class='invisible'><code>pub fn <a href='#method.as_mut_str' class='fnname'>as_mut_str</a>(&mut self) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.7.0'>1.7.0</div><a class='srclink' href='../../src/alloc/string.rs.html#785-787' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a <code>String</code> into a mutable string slice.</p> <h1 id="examples-11" class="section-header"><a href="#examples-11">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foobar"</span>); <span class="kw">let</span> <span class="ident">s_mut_str</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">as_mut_str</span>(); <span class="ident">s_mut_str</span>.<span class="ident">make_ascii_uppercase</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"FOOBAR"</span>, <span class="ident">s_mut_str</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foobar%22)%3B%0Alet%20s_mut_str%20%3D%20s.as_mut_str()%3B%0A%0As_mut_str.make_ascii_uppercase()%3B%0A%0Aassert_eq!(%22FOOBAR%22%2C%20s_mut_str)%3B%0A%7D">Run</a></pre> </div><h4 id='method.push_str' class="method"><span id='push_str.v' class='invisible'><code>pub fn <a href='#method.push_str' class='fnname'>push_str</a>(&mut self, string: &<a class="primitive" href="../primitive.str.html">str</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#804-806' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Appends a given string slice onto the end of this <code>String</code>.</p> <h1 id="examples-12" class="section-header"><a href="#examples-12">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="ident">s</span>.<span class="ident">push_str</span>(<span class="string">"bar"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"foobar"</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%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0As.push_str(%22bar%22)%3B%0A%0Aassert_eq!(%22foobar%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.capacity' class="method"><span id='capacity.v' class='invisible'><code>pub fn <a href='#method.capacity' class='fnname'>capacity</a>(&self) -> <a class="primitive" href="../primitive.usize.html">usize</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#821-823' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns this <code>String</code>'s capacity, in bytes.</p> <h1 id="examples-13" class="section-header"><a href="#examples-13">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">10</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20String%3A%3Awith_capacity(10)%3B%0A%0Aassert!(s.capacity()%20%3E%3D%2010)%3B%0A%7D">Run</a></pre> </div><h4 id='method.reserve' class="method"><span id='reserve.v' class='invisible'><code>pub fn <a href='#method.reserve' class='fnname'>reserve</a>(&mut self, additional: <a class="primitive" href="../primitive.usize.html">usize</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#872-874' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Ensures that this <code>String</code>'s capacity is at least <code>additional</code> bytes larger than its length.</p> <p>The capacity may be increased by more than <code>additional</code> bytes if it chooses, to prevent frequent reallocations.</p> <p>If you do not want this "at least" behavior, see the <a href="struct.String.html#method.reserve_exact"><code>reserve_exact</code></a> method.</p> <h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1> <p>Panics if the new capacity overflows <a href="../../std/primitive.usize.html"><code>usize</code></a>.</p> <h1 id="examples-14" class="section-header"><a href="#examples-14">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>(); <span class="ident">s</span>.<span class="ident">reserve</span>(<span class="number">10</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">10</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Anew()%3B%0A%0As.reserve(10)%3B%0A%0Aassert!(s.capacity()%20%3E%3D%2010)%3B%0A%7D">Run</a></pre> <p>This may not actually increase the capacity:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'a'</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'b'</span>); <span class="comment">// s now has a length of 2 and a capacity of 10</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">2</span>, <span class="ident">s</span>.<span class="ident">len</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">10</span>, <span class="ident">s</span>.<span class="ident">capacity</span>()); <span class="comment">// Since we already have an extra 8 capacity, calling this...</span> <span class="ident">s</span>.<span class="ident">reserve</span>(<span class="number">8</span>); <span class="comment">// ... doesn't actually increase.</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">10</span>, <span class="ident">s</span>.<span class="ident">capacity</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Awith_capacity(10)%3B%0As.push('a')%3B%0As.push('b')%3B%0A%0A%2F%2F%20s%20now%20has%20a%20length%20of%202%20and%20a%20capacity%20of%2010%0Aassert_eq!(2%2C%20s.len())%3B%0Aassert_eq!(10%2C%20s.capacity())%3B%0A%0A%2F%2F%20Since%20we%20already%20have%20an%20extra%208%20capacity%2C%20calling%20this...%0As.reserve(8)%3B%0A%0A%2F%2F%20...%20doesn't%20actually%20increase.%0Aassert_eq!(10%2C%20s.capacity())%3B%0A%7D">Run</a></pre> </div><h4 id='method.reserve_exact' class="method"><span id='reserve_exact.v' class='invisible'><code>pub fn <a href='#method.reserve_exact' class='fnname'>reserve_exact</a>(&mut self, additional: <a class="primitive" href="../primitive.usize.html">usize</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#919-921' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Ensures that this <code>String</code>'s capacity is <code>additional</code> bytes larger than its length.</p> <p>Consider using the <a href="#method.reserve"><code>reserve</code></a> method unless you absolutely know better than the allocator.</p> <h1 id="panics-1" class="section-header"><a href="#panics-1">Panics</a></h1> <p>Panics if the new capacity overflows <code>usize</code>.</p> <h1 id="examples-15" class="section-header"><a href="#examples-15">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>(); <span class="ident">s</span>.<span class="ident">reserve_exact</span>(<span class="number">10</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">10</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Anew()%3B%0A%0As.reserve_exact(10)%3B%0A%0Aassert!(s.capacity()%20%3E%3D%2010)%3B%0A%7D">Run</a></pre> <p>This may not actually increase the capacity:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'a'</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'b'</span>); <span class="comment">// s now has a length of 2 and a capacity of 10</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">2</span>, <span class="ident">s</span>.<span class="ident">len</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">10</span>, <span class="ident">s</span>.<span class="ident">capacity</span>()); <span class="comment">// Since we already have an extra 8 capacity, calling this...</span> <span class="ident">s</span>.<span class="ident">reserve_exact</span>(<span class="number">8</span>); <span class="comment">// ... doesn't actually increase.</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">10</span>, <span class="ident">s</span>.<span class="ident">capacity</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Awith_capacity(10)%3B%0As.push('a')%3B%0As.push('b')%3B%0A%0A%2F%2F%20s%20now%20has%20a%20length%20of%202%20and%20a%20capacity%20of%2010%0Aassert_eq!(2%2C%20s.len())%3B%0Aassert_eq!(10%2C%20s.capacity())%3B%0A%0A%2F%2F%20Since%20we%20already%20have%20an%20extra%208%20capacity%2C%20calling%20this...%0As.reserve_exact(8)%3B%0A%0A%2F%2F%20...%20doesn't%20actually%20increase.%0Aassert_eq!(10%2C%20s.capacity())%3B%0A%7D">Run</a></pre> </div><h4 id='method.shrink_to_fit' class="method"><span id='shrink_to_fit.v' class='invisible'><code>pub fn <a href='#method.shrink_to_fit' class='fnname'>shrink_to_fit</a>(&mut self)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#940-942' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Shrinks the capacity of this <code>String</code> to match its length.</p> <h1 id="examples-16" class="section-header"><a href="#examples-16">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="ident">s</span>.<span class="ident">reserve</span>(<span class="number">100</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">capacity</span>() <span class="op">>=</span> <span class="number">100</span>); <span class="ident">s</span>.<span class="ident">shrink_to_fit</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">3</span>, <span class="ident">s</span>.<span class="ident">capacity</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0As.reserve(100)%3B%0Aassert!(s.capacity()%20%3E%3D%20100)%3B%0A%0As.shrink_to_fit()%3B%0Aassert_eq!(3%2C%20s.capacity())%3B%0A%7D">Run</a></pre> </div><h4 id='method.push' class="method"><span id='push.v' class='invisible'><code>pub fn <a href='#method.push' class='fnname'>push</a>(&mut self, ch: <a class="primitive" href="../primitive.char.html">char</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#963-968' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Appends the given <a href="../../std/primitive.char.html"><code>char</code></a> to the end of this <code>String</code>.</p> <h1 id="examples-17" class="section-header"><a href="#examples-17">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"abc"</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'1'</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'2'</span>); <span class="ident">s</span>.<span class="ident">push</span>(<span class="string">'3'</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"abc123"</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%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22abc%22)%3B%0A%0As.push('1')%3B%0As.push('2')%3B%0As.push('3')%3B%0A%0Aassert_eq!(%22abc123%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_bytes' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="primitive" href="../primitive.slice.html">&'a mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></code></div></div><span id='as_bytes.v' class='invisible'><code>pub fn <a href='#method.as_bytes' class='fnname'>as_bytes</a>(&self) -> <a class="primitive" href="../primitive.slice.html">&[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#987-989' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a byte slice of this <code>String</code>'s contents.</p> <p>The inverse of this method is <a href="#method.from_utf8"><code>from_utf8</code></a>.</p> <h1 id="examples-18" class="section-header"><a href="#examples-18">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span>[<span class="number">104</span>, <span class="number">101</span>, <span class="number">108</span>, <span class="number">108</span>, <span class="number">111</span>], <span class="ident">s</span>.<span class="ident">as_bytes</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0A%0Aassert_eq!(%26%5B104%2C%20101%2C%20108%2C%20108%2C%20111%5D%2C%20s.as_bytes())%3B%0A%7D">Run</a></pre> </div><h4 id='method.truncate' class="method"><span id='truncate.v' class='invisible'><code>pub fn <a href='#method.truncate' class='fnname'>truncate</a>(&mut self, new_len: <a class="primitive" href="../primitive.usize.html">usize</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1018-1023' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Shortens this <code>String</code> to the specified length.</p> <p>If <code>new_len</code> is greater than the string's current length, this has no effect.</p> <p>Note that this method has no effect on the allocated capacity of the string</p> <h1 id="panics-2" class="section-header"><a href="#panics-2">Panics</a></h1> <p>Panics if <code>new_len</code> does not lie on a <a href="../../std/primitive.char.html"><code>char</code></a> boundary.</p> <h1 id="examples-19" class="section-header"><a href="#examples-19">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="ident">s</span>.<span class="ident">truncate</span>(<span class="number">2</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"he"</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%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0A%0As.truncate(2)%3B%0A%0Aassert_eq!(%22he%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.pop' class="method"><span id='pop.v' class='invisible'><code>pub fn <a href='#method.pop' class='fnname'>pop</a>(&mut self) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="primitive" href="../primitive.char.html">char</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1046-1053' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Removes the last character from the string buffer and returns it.</p> <p>Returns <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a> if this <code>String</code> is empty.</p> <h1 id="examples-20" class="section-header"><a href="#examples-20">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="string">'o'</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="string">'o'</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">pop</span>(), <span class="prelude-val">Some</span>(<span class="string">'f'</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">pop</span>(), <span class="prelude-val">None</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0Aassert_eq!(s.pop()%2C%20Some('o'))%3B%0Aassert_eq!(s.pop()%2C%20Some('o'))%3B%0Aassert_eq!(s.pop()%2C%20Some('f'))%3B%0A%0Aassert_eq!(s.pop()%2C%20None)%3B%0A%7D">Run</a></pre> </div><h4 id='method.remove' class="method"><span id='remove.v' class='invisible'><code>pub fn <a href='#method.remove' class='fnname'>remove</a>(&mut self, idx: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="primitive" href="../primitive.char.html">char</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1080-1095' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Removes a <a href="../../std/primitive.char.html"><code>char</code></a> from this <code>String</code> at a byte position and returns it.</p> <p>This is an <code>O(n)</code> operation, as it requires copying every element in the buffer.</p> <h1 id="panics-3" class="section-header"><a href="#panics-3">Panics</a></h1> <p>Panics if <code>idx</code> is larger than or equal to the <code>String</code>'s length, or if it does not lie on a <a href="../../std/primitive.char.html"><code>char</code></a> boundary.</p> <h1 id="examples-21" class="section-header"><a href="#examples-21">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">remove</span>(<span class="number">0</span>), <span class="string">'f'</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">remove</span>(<span class="number">1</span>), <span class="string">'o'</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">remove</span>(<span class="number">0</span>), <span class="string">'o'</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0Aassert_eq!(s.remove(0)%2C%20'f')%3B%0Aassert_eq!(s.remove(1)%2C%20'o')%3B%0Aassert_eq!(s.remove(0)%2C%20'o')%3B%0A%7D">Run</a></pre> </div><h4 id='method.retain' class="method"><span id='retain.v' class='invisible'><code>pub fn <a href='#method.retain' class='fnname'>retain</a><F>(&mut self, f: F) <span class="where fmt-newline">where<br> F: <a class="trait" href="../../std/ops/trait.FnMut.html" title="trait std::ops::FnMut">FnMut</a>(<a class="primitive" href="../primitive.char.html">char</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1116-1146' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>string_retain </code><a href="https://github.com/rust-lang/rust/issues/43874">#43874</a>)</div></div><div class='docblock'><p>Retains only the characters specified by the predicate.</p> <p>In other words, remove all characters <code>c</code> such that <code>f(c)</code> returns <code>false</code>. This method operates in place and preserves the order of the retained characters.</p> <h1 id="examples-22" class="section-header"><a href="#examples-22">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="attribute">#![<span class="ident">feature</span>(<span class="ident">string_retain</span>)]</span> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"f_o_ob_ar"</span>); <span class="ident">s</span>.<span class="ident">retain</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">!=</span> <span class="string">'_'</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="string">"foobar"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(string_retain)%5D%0A%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22f_o_ob_ar%22)%3B%0A%0As.retain(%7Cc%7C%20c%20!%3D%20'_')%3B%0A%0Aassert_eq!(s%2C%20%22foobar%22)%3B%0A%7D&version=nightly">Run</a></pre> </div><h4 id='method.insert' class="method"><span id='insert.v' class='invisible'><code>pub fn <a href='#method.insert' class='fnname'>insert</a>(&mut self, idx: <a class="primitive" href="../primitive.usize.html">usize</a>, ch: <a class="primitive" href="../primitive.char.html">char</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1175-1183' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Inserts a character into this <code>String</code> at a byte position.</p> <p>This is an <code>O(n)</code> operation as it requires copying every element in the buffer.</p> <h1 id="panics-4" class="section-header"><a href="#panics-4">Panics</a></h1> <p>Panics if <code>idx</code> is larger than the <code>String</code>'s length, or if it does not lie on a <a href="../../std/primitive.char.html"><code>char</code></a> boundary.</p> <h1 id="examples-23" class="section-header"><a href="#examples-23">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">3</span>); <span class="ident">s</span>.<span class="ident">insert</span>(<span class="number">0</span>, <span class="string">'f'</span>); <span class="ident">s</span>.<span class="ident">insert</span>(<span class="number">1</span>, <span class="string">'o'</span>); <span class="ident">s</span>.<span class="ident">insert</span>(<span class="number">2</span>, <span class="string">'o'</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"foo"</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%0Alet%20mut%20s%20%3D%20String%3A%3Awith_capacity(3)%3B%0A%0As.insert(0%2C%20'f')%3B%0As.insert(1%2C%20'o')%3B%0As.insert(2%2C%20'o')%3B%0A%0Aassert_eq!(%22foo%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.insert_str' class="method"><span id='insert_str.v' class='invisible'><code>pub fn <a href='#method.insert_str' class='fnname'>insert_str</a>(&mut self, idx: <a class="primitive" href="../primitive.usize.html">usize</a>, string: &<a class="primitive" href="../primitive.str.html">str</a>)</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.16.0'>1.16.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1224-1230' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Inserts a string slice into this <code>String</code> at a byte position.</p> <p>This is an <code>O(n)</code> operation as it requires copying every element in the buffer.</p> <h1 id="panics-5" class="section-header"><a href="#panics-5">Panics</a></h1> <p>Panics if <code>idx</code> is larger than the <code>String</code>'s length, or if it does not lie on a <a href="../../std/primitive.char.html"><code>char</code></a> boundary.</p> <h1 id="examples-24" class="section-header"><a href="#examples-24">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"bar"</span>); <span class="ident">s</span>.<span class="ident">insert_str</span>(<span class="number">0</span>, <span class="string">"foo"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"foobar"</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%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22bar%22)%3B%0A%0As.insert_str(0%2C%20%22foo%22)%3B%0A%0Aassert_eq!(%22foobar%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_mut_vec' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></h3><code class="content"><span class="where fmt-newline">impl <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></span></code></div></div><span id='as_mut_vec.v' class='invisible'><code>pub unsafe fn <a href='#method.as_mut_vec' class='fnname'>as_mut_vec</a>(&mut self) -> &mut <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1258-1260' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a mutable reference to the contents of this <code>String</code>.</p> <h1 id="safety-2" class="section-header"><a href="#safety-2">Safety</a></h1> <p>This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, it may cause memory unsafety issues with future users of the <code>String</code>, as the rest of the standard library assumes that <code>String</code>s are valid UTF-8.</p> <h1 id="examples-25" class="section-header"><a href="#examples-25">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="kw">unsafe</span> { <span class="kw">let</span> <span class="ident">vec</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">as_mut_vec</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span>[<span class="number">104</span>, <span class="number">101</span>, <span class="number">108</span>, <span class="number">108</span>, <span class="number">111</span>][..], <span class="kw-2">&</span><span class="ident">vec</span>[..]); <span class="ident">vec</span>.<span class="ident">reverse</span>(); } <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="string">"olleh"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20let%20vec%20%3D%20s.as_mut_vec()%3B%0A%20%20%20%20assert_eq!(%26%5B104%2C%20101%2C%20108%2C%20108%2C%20111%5D%5B..%5D%2C%20%26vec%5B..%5D)%3B%0A%0A%20%20%20%20vec.reverse()%3B%0A%7D%0Aassert_eq!(s%2C%20%22olleh%22)%3B%0A%7D">Run</a></pre> </div><h4 id='method.len' class="method"><span id='len.v' class='invisible'><code>pub fn <a href='#method.len' class='fnname'>len</a>(&self) -> <a class="primitive" href="../primitive.usize.html">usize</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1275-1277' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns the length of this <code>String</code>, in bytes.</p> <h1 id="examples-26" class="section-header"><a href="#examples-26">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">a</span>.<span class="ident">len</span>(), <span class="number">3</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20a%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0Aassert_eq!(a.len()%2C%203)%3B%0A%7D">Run</a></pre> </div><h4 id='method.is_empty' class="method"><span id='is_empty.v' class='invisible'><code>pub fn <a href='#method.is_empty' class='fnname'>is_empty</a>(&self) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1296-1298' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns <code>true</code> if this <code>String</code> has a length of zero.</p> <p>Returns <code>false</code> otherwise.</p> <h1 id="examples-27" class="section-header"><a href="#examples-27">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">is_empty</span>()); <span class="ident">v</span>.<span class="ident">push</span>(<span class="string">'a'</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">v</span>.<span class="ident">is_empty</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20v%20%3D%20String%3A%3Anew()%3B%0Aassert!(v.is_empty())%3B%0A%0Av.push('a')%3B%0Aassert!(!v.is_empty())%3B%0A%7D">Run</a></pre> </div><h4 id='method.split_off' class="method"><span id='split_off.v' class='invisible'><code>pub fn <a href='#method.split_off' class='fnname'>split_off</a>(&mut self, at: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.16.0'>1.16.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1325-1329' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Splits the string into two at the given index.</p> <p>Returns a newly allocated <code>String</code>. <code>self</code> contains bytes <code>[0, at)</code>, and the returned <code>String</code> contains bytes <code>[at, len)</code>. <code>at</code> must be on the boundary of a UTF-8 code point.</p> <p>Note that the capacity of <code>self</code> does not change.</p> <h1 id="panics-6" class="section-header"><a href="#panics-6">Panics</a></h1> <p>Panics if <code>at</code> is not on a <code>UTF-8</code> code point boundary, or if it is beyond the last code point of the string.</p> <h1 id="examples-28" class="section-header"><a href="#examples-28">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">hello</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Hello, World!"</span>); <span class="kw">let</span> <span class="ident">world</span> <span class="op">=</span> <span class="ident">hello</span>.<span class="ident">split_off</span>(<span class="number">7</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">hello</span>, <span class="string">"Hello, "</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">world</span>, <span class="string">"World!"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20hello%20%3D%20String%3A%3Afrom(%22Hello%2C%20World!%22)%3B%0Alet%20world%20%3D%20hello.split_off(7)%3B%0Aassert_eq!(hello%2C%20%22Hello%2C%20%22)%3B%0Aassert_eq!(world%2C%20%22World!%22)%3B%0A%7D%0A">Run</a></pre> </div><h4 id='method.clear' class="method"><span id='clear.v' class='invisible'><code>pub fn <a href='#method.clear' class='fnname'>clear</a>(&mut self)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1351-1353' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Truncates this <code>String</code>, removing all contents.</p> <p>While this means the <code>String</code> will have a length of zero, it does not touch its capacity.</p> <h1 id="examples-29" class="section-header"><a href="#examples-29">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"foo"</span>); <span class="ident">s</span>.<span class="ident">clear</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">is_empty</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">0</span>, <span class="ident">s</span>.<span class="ident">len</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">3</span>, <span class="ident">s</span>.<span class="ident">capacity</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22foo%22)%3B%0A%0As.clear()%3B%0A%0Aassert!(s.is_empty())%3B%0Aassert_eq!(0%2C%20s.len())%3B%0Aassert_eq!(3%2C%20s.capacity())%3B%0A%7D">Run</a></pre> </div><h4 id='method.drain' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/string/struct.Drain.html" title="struct std::string::Drain">Drain</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/string/struct.Drain.html" title="struct std::string::Drain">Drain</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/string/struct.Drain.html" title="struct std::string::Drain">Drain</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.char.html">char</a>;</span></code></div></div><span id='drain.v' class='invisible'><code>pub fn <a href='#method.drain' class='fnname'>drain</a><R>(&mut self, range: R) -> <a class="struct" href="../../std/string/struct.Drain.html" title="struct std::string::Drain">Drain</a> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../std/collections/range/trait.RangeArgument.html" title="trait std::collections::range::RangeArgument">RangeArgument</a><<a class="primitive" href="../primitive.usize.html">usize</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1386-1419' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a draining iterator that removes the specified range in the string and yields the removed chars.</p> <p>Note: The element range is removed even if the iterator is not consumed until the end.</p> <h1 id="panics-7" class="section-header"><a href="#panics-7">Panics</a></h1> <p>Panics if the starting point or end point do not lie on a <a href="../../std/primitive.char.html"><code>char</code></a> boundary, or if they're out of bounds.</p> <h1 id="examples-30" class="section-header"><a href="#examples-30">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"α is alpha, β is beta"</span>); <span class="kw">let</span> <span class="ident">beta_offset</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">find</span>(<span class="string">'β'</span>).<span class="ident">unwrap_or</span>(<span class="ident">s</span>.<span class="ident">len</span>()); <span class="comment">// Remove the range up until the β from the string</span> <span class="kw">let</span> <span class="ident">t</span>: <span class="ident">String</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">drain</span>(..<span class="ident">beta_offset</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">t</span>, <span class="string">"α is alpha, "</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="string">"β is beta"</span>); <span class="comment">// A full range clears the string</span> <span class="ident">s</span>.<span class="ident">drain</span>(..); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="string">""</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22%CE%B1%20is%20alpha%2C%20%CE%B2%20is%20beta%22)%3B%0Alet%20beta_offset%20%3D%20s.find('%CE%B2').unwrap_or(s.len())%3B%0A%0A%2F%2F%20Remove%20the%20range%20up%20until%20the%20%CE%B2%20from%20the%20string%0Alet%20t%3A%20String%20%3D%20s.drain(..beta_offset).collect()%3B%0Aassert_eq!(t%2C%20%22%CE%B1%20is%20alpha%2C%20%22)%3B%0Aassert_eq!(s%2C%20%22%CE%B2%20is%20beta%22)%3B%0A%0A%2F%2F%20A%20full%20range%20clears%20the%20string%0As.drain(..)%3B%0Aassert_eq!(s%2C%20%22%22)%3B%0A%7D">Run</a></pre> </div><h4 id='method.splice' class="method"><span id='splice.v' class='invisible'><code>pub fn <a href='#method.splice' class='fnname'>splice</a><R>(&mut self, range: R, replace_with: &<a class="primitive" href="../primitive.str.html">str</a>) <span class="where fmt-newline">where<br> R: <a class="trait" href="../../std/collections/range/trait.RangeArgument.html" title="trait std::collections::range::RangeArgument">RangeArgument</a><<a class="primitive" href="../primitive.usize.html">usize</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1450-1472' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>splice </code><a href="https://github.com/rust-lang/rust/issues/44643">#44643</a>)</summary><p>recently added</p> </details></div></div><div class='docblock'><p>Creates a splicing iterator that removes the specified range in the string, and replaces it with the given string. The given string doesn't need to be the same length as the range.</p> <p>Note: Unlike <a href="../../std/vec/struct.Vec.html#method.splice"><code>Vec::splice</code></a>, the replacement happens eagerly, and this method does not return the removed chars.</p> <h1 id="panics-8" class="section-header"><a href="#panics-8">Panics</a></h1> <p>Panics if the starting point or end point do not lie on a <a href="../../std/primitive.char.html"><code>char</code></a> boundary, or if they're out of bounds.</p> <h1 id="examples-31" class="section-header"><a href="#examples-31">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="attribute">#![<span class="ident">feature</span>(<span class="ident">splice</span>)]</span> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"α is alpha, β is beta"</span>); <span class="kw">let</span> <span class="ident">beta_offset</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">find</span>(<span class="string">'β'</span>).<span class="ident">unwrap_or</span>(<span class="ident">s</span>.<span class="ident">len</span>()); <span class="comment">// Replace the range up until the β from the string</span> <span class="ident">s</span>.<span class="ident">splice</span>(..<span class="ident">beta_offset</span>, <span class="string">"Α is capital alpha; "</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="string">"Α is capital alpha; β is beta"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(splice)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22%CE%B1%20is%20alpha%2C%20%CE%B2%20is%20beta%22)%3B%0Alet%20beta_offset%20%3D%20s.find('%CE%B2').unwrap_or(s.len())%3B%0A%0A%2F%2F%20Replace%20the%20range%20up%20until%20the%20%CE%B2%20from%20the%20string%0As.splice(..beta_offset%2C%20%22%CE%91%20is%20capital%20alpha%3B%20%22)%3B%0Aassert_eq!(s%2C%20%22%CE%91%20is%20capital%20alpha%3B%20%CE%B2%20is%20beta%22)%3B%0A%7D&version=nightly">Run</a></pre> </div><h4 id='method.into_boxed_str' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></h3><code class="content"><span class="where fmt-newline">impl<I> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <I as <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a>>::<a class="type" href="../../std/iter/trait.Iterator.html#associatedtype.Item" title="type std::iter::Iterator::Item">Item</a>;</span><span class="where fmt-newline">impl<R: <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><R></span><span class="where fmt-newline">impl<W: <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><W></span></code></div></div><span id='into_boxed_str.v' class='invisible'><code>pub fn <a href='#method.into_boxed_str' class='fnname'>into_boxed_str</a>(self) -> <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="primitive" href="../primitive.str.html">str</a>></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.4.0'>1.4.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1491-1494' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts this <code>String</code> into a <a href="../../std/boxed/struct.Box.html"><code>Box</code></a><code><</code><a href="../../std/primitive.str.html"><code>str</code></a><code>></code>.</p> <p>This will drop any excess capacity.</p> <h1 id="examples-32" class="section-header"><a href="#examples-32">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">into_boxed_str</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0A%0Alet%20b%20%3D%20s.into_boxed_str()%3B%0A%7D">Run</a></pre> </div></div> <h2 id='deref-methods' class='small-section-header'> Methods from <a class="trait" href="../../std/ops/trait.Deref.html" title="trait std::ops::Deref">Deref</a><Target = <a class="primitive" href="../primitive.str.html">str</a>><a href='#deref-methods' class='anchor'></a> </h2> <div class='impl-items'><h4 id='method.len-1' class="method"><span id='len.v-1' class='invisible'><code>pub fn <a href='#method.len-1' class='fnname'>len</a>(&self) -> <a class="primitive" href="../primitive.usize.html">usize</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#223-225' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns the length of <code>self</code>.</p> <p>This length is in bytes, not <a href="primitive.char.html"><code>char</code></a>s or graphemes. In other words, it may not be what a human considers the length of the string.</p> <h1 id="examples-33" class="section-header"><a href="#examples-33">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">len</span> <span class="op">=</span> <span class="string">"foo"</span>.<span class="ident">len</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">3</span>, <span class="ident">len</span>); <span class="kw">let</span> <span class="ident">len</span> <span class="op">=</span> <span class="string">"ƒoo"</span>.<span class="ident">len</span>(); <span class="comment">// fancy f!</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">4</span>, <span class="ident">len</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20len%20%3D%20%22foo%22.len()%3B%0Aassert_eq!(3%2C%20len)%3B%0A%0Alet%20len%20%3D%20%22%C6%92oo%22.len()%3B%20%2F%2F%20fancy%20f!%0Aassert_eq!(4%2C%20len)%3B%0A%7D">Run</a></pre> </div><h4 id='method.is_empty-1' class="method"><span id='is_empty.v-1' class='invisible'><code>pub fn <a href='#method.is_empty-1' class='fnname'>is_empty</a>(&self) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#242-244' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns <code>true</code> if <code>self</code> has a length of zero bytes.</p> <h1 id="examples-34" class="section-header"><a href="#examples-34">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">""</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">is_empty</span>()); <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"not empty"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">s</span>.<span class="ident">is_empty</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22%22%3B%0Aassert!(s.is_empty())%3B%0A%0Alet%20s%20%3D%20%22not%20empty%22%3B%0Aassert!(!s.is_empty())%3B%0A%7D">Run</a></pre> </div><h4 id='method.is_char_boundary' class="method"><span id='is_char_boundary.v' class='invisible'><code>pub fn <a href='#method.is_char_boundary' class='fnname'>is_char_boundary</a>(&self, index: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.9.0'>1.9.0</div><a class='srclink' href='../../src/alloc/str.rs.html#272-274' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Checks that <code>index</code>-th byte lies at the start and/or end of a UTF-8 code point sequence.</p> <p>The start and end of the string (when <code>index == self.len()</code>) are considered to be boundaries.</p> <p>Returns <code>false</code> if <code>index</code> is greater than <code>self.len()</code>.</p> <h1 id="examples-35" class="section-header"><a href="#examples-35">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">is_char_boundary</span>(<span class="number">0</span>)); <span class="comment">// start of `老`</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">is_char_boundary</span>(<span class="number">6</span>)); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">is_char_boundary</span>(<span class="ident">s</span>.<span class="ident">len</span>())); <span class="comment">// second byte of `ö`</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">s</span>.<span class="ident">is_char_boundary</span>(<span class="number">2</span>)); <span class="comment">// third byte of `老`</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">s</span>.<span class="ident">is_char_boundary</span>(<span class="number">8</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0Aassert!(s.is_char_boundary(0))%3B%0A%2F%2F%20start%20of%20%60%E8%80%81%60%0Aassert!(s.is_char_boundary(6))%3B%0Aassert!(s.is_char_boundary(s.len()))%3B%0A%0A%2F%2F%20second%20byte%20of%20%60%C3%B6%60%0Aassert!(!s.is_char_boundary(2))%3B%0A%0A%2F%2F%20third%20byte%20of%20%60%E8%80%81%60%0Aassert!(!s.is_char_boundary(8))%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_bytes-1' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="primitive" href="../primitive.slice.html">&'a mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></code></div></div><span id='as_bytes.v-1' class='invisible'><code>pub fn <a href='#method.as_bytes-1' class='fnname'>as_bytes</a>(&self) -> <a class="primitive" href="../primitive.slice.html">&[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#291-293' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a string slice to a byte slice. To convert the byte slice back into a string slice, use the <a href="./str/fn.from_utf8.html"><code>str::from_utf8</code></a> function.</p> <h1 id="examples-36" class="section-header"><a href="#examples-36">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="string">"bors"</span>.<span class="ident">as_bytes</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b"bors"</span>, <span class="ident">bytes</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20bytes%20%3D%20%22bors%22.as_bytes()%3B%0Aassert_eq!(b%22bors%22%2C%20bytes)%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_bytes_mut' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="primitive" href="../primitive.slice.html">&'a mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></code></div></div><span id='as_bytes_mut.v' class='invisible'><code>pub unsafe fn <a href='#method.as_bytes_mut' class='fnname'>as_bytes_mut</a>(&mut self) -> <a class="primitive" href="../primitive.slice.html">&mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.20.0'>1.20.0</div><a class='srclink' href='../../src/alloc/str.rs.html#330-332' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a mutable string slice to a mutable byte slice. To convert the mutable byte slice back into a mutable string slice, use the <a href="./str/fn.from_utf8_mut.html"><code>str::from_utf8_mut</code></a> function.</p> <h1 id="examples-37" class="section-header"><a href="#examples-37">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"Hello"</span>); <span class="kw">let</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="kw">unsafe</span> { <span class="ident">s</span>.<span class="ident">as_bytes_mut</span>() }; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b"Hello"</span>, <span class="ident">bytes</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22Hello%22)%3B%0Alet%20bytes%20%3D%20unsafe%20%7B%20s.as_bytes_mut()%20%7D%3B%0A%0Aassert_eq!(b%22Hello%22%2C%20bytes)%3B%0A%7D">Run</a></pre> <p>Mutability:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"🗻∈🌏"</span>); <span class="kw">unsafe</span> { <span class="kw">let</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">as_bytes_mut</span>(); <span class="ident">bytes</span>[<span class="number">0</span>] <span class="op">=</span> <span class="number">0xF0</span>; <span class="ident">bytes</span>[<span class="number">1</span>] <span class="op">=</span> <span class="number">0x9F</span>; <span class="ident">bytes</span>[<span class="number">2</span>] <span class="op">=</span> <span class="number">0x8D</span>; <span class="ident">bytes</span>[<span class="number">3</span>] <span class="op">=</span> <span class="number">0x94</span>; } <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"🍔∈🌏"</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%0Alet%20mut%20s%20%3D%20String%3A%3Afrom(%22%F0%9F%97%BB%E2%88%88%F0%9F%8C%8F%22)%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20let%20bytes%20%3D%20s.as_bytes_mut()%3B%0A%0A%20%20%20%20bytes%5B0%5D%20%3D%200xF0%3B%0A%20%20%20%20bytes%5B1%5D%20%3D%200x9F%3B%0A%20%20%20%20bytes%5B2%5D%20%3D%200x8D%3B%0A%20%20%20%20bytes%5B3%5D%20%3D%200x94%3B%0A%7D%0A%0Aassert_eq!(%22%F0%9F%8D%94%E2%88%88%F0%9F%8C%8F%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.as_ptr' class="method"><span id='as_ptr.v' class='invisible'><code>pub fn <a href='#method.as_ptr' class='fnname'>as_ptr</a>(&self) -> <a class="primitive" href="../primitive.pointer.html">*const </a><a class="primitive" href="../primitive.u8.html">u8</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#352-354' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts a string slice to a raw pointer.</p> <p>As string slices are a slice of bytes, the raw pointer points to a <a href="primitive.u8.html"><code>u8</code></a>. This pointer will be pointing to the first byte of the string slice.</p> <h1 id="examples-38" class="section-header"><a href="#examples-38">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Hello"</span>; <span class="kw">let</span> <span class="ident">ptr</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">as_ptr</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22Hello%22%3B%0Alet%20ptr%20%3D%20s.as_ptr()%3B%0A%7D">Run</a></pre> </div><h4 id='method.get' class="method"><span id='get.v' class='invisible'><code>pub fn <a href='#method.get' class='fnname'>get</a><I>(&self, i: I) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><&<I as <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>>::<a class="type" href="../../std/slice/trait.SliceIndex.html#associatedtype.Output" title="type std::slice::SliceIndex::Output">Output</a>> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.20.0'>1.20.0</div><a class='srclink' href='../../src/alloc/str.rs.html#379-381' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a subslice of <code>str</code>.</p> <p>This is the non-panicking alternative to indexing the <code>str</code>. Returns <a href="option/enum.Option.html#variant.None"><code>None</code></a> whenever equivalent indexing operation would panic.</p> <h1 id="examples-39" class="section-header"><a href="#examples-39">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"🗻∈🌏"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"🗻"</span>), <span class="ident">v</span>.<span class="ident">get</span>(<span class="number">0</span>..<span class="number">4</span>)); <span class="comment">// indices not on UTF-8 sequence boundaries</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">get</span>(<span class="number">1</span>..).<span class="ident">is_none</span>()); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">get</span>(..<span class="number">8</span>).<span class="ident">is_none</span>()); <span class="comment">// out of bounds</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">get</span>(..<span class="number">42</span>).<span class="ident">is_none</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%20%3D%20String%3A%3Afrom(%22%F0%9F%97%BB%E2%88%88%F0%9F%8C%8F%22)%3B%0A%0Aassert_eq!(Some(%22%F0%9F%97%BB%22)%2C%20v.get(0..4))%3B%0A%0A%2F%2F%20indices%20not%20on%20UTF-8%20sequence%20boundaries%0Aassert!(v.get(1..).is_none())%3B%0Aassert!(v.get(..8).is_none())%3B%0A%0A%2F%2F%20out%20of%20bounds%0Aassert!(v.get(..42).is_none())%3B%0A%7D">Run</a></pre> </div><h4 id='method.get_mut' class="method"><span id='get_mut.v' class='invisible'><code>pub fn <a href='#method.get_mut' class='fnname'>get_mut</a><I>(<br> &mut self, <br> i: I<br>) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><&mut <I as <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>>::<a class="type" href="../../std/slice/trait.SliceIndex.html#associatedtype.Output" title="type std::slice::SliceIndex::Output">Output</a>> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.20.0'>1.20.0</div><a class='srclink' href='../../src/alloc/str.rs.html#413-415' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a mutable subslice of <code>str</code>.</p> <p>This is the non-panicking alternative to indexing the <code>str</code>. Returns <a href="option/enum.Option.html#variant.None"><code>None</code></a> whenever equivalent indexing operation would panic.</p> <h1 id="examples-40" class="section-header"><a href="#examples-40">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="comment">// correct length</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">get_mut</span>(<span class="number">0</span>..<span class="number">5</span>).<span class="ident">is_some</span>()); <span class="comment">// out of bounds</span> <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">get_mut</span>(..<span class="number">42</span>).<span class="ident">is_none</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"he"</span>), <span class="ident">v</span>.<span class="ident">get_mut</span>(<span class="number">0</span>..<span class="number">2</span>).<span class="ident">map</span>(<span class="op">|</span><span class="ident">v</span><span class="op">|</span> <span class="kw-2">&</span><span class="kw-2">*</span><span class="ident">v</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"hello"</span>, <span class="ident">v</span>); { <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">v</span>.<span class="ident">get_mut</span>(<span class="number">0</span>..<span class="number">2</span>); <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> { <span class="ident">s</span>.<span class="ident">make_ascii_uppercase</span>(); <span class="kw-2">&</span><span class="kw-2">*</span><span class="ident">s</span> }); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"HE"</span>), <span class="ident">s</span>); } <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"HEllo"</span>, <span class="ident">v</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20v%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0A%2F%2F%20correct%20length%0Aassert!(v.get_mut(0..5).is_some())%3B%0A%2F%2F%20out%20of%20bounds%0Aassert!(v.get_mut(..42).is_none())%3B%0Aassert_eq!(Some(%22he%22)%2C%20v.get_mut(0..2).map(%7Cv%7C%20%26*v))%3B%0A%0Aassert_eq!(%22hello%22%2C%20v)%3B%0A%7B%0A%20%20%20%20let%20s%20%3D%20v.get_mut(0..2)%3B%0A%20%20%20%20let%20s%20%3D%20s.map(%7Cs%7C%20%7B%0A%20%20%20%20%20%20%20%20s.make_ascii_uppercase()%3B%0A%20%20%20%20%20%20%20%20%26*s%0A%20%20%20%20%7D)%3B%0A%20%20%20%20assert_eq!(Some(%22HE%22)%2C%20s)%3B%0A%7D%0Aassert_eq!(%22HEllo%22%2C%20v)%3B%0A%7D">Run</a></pre> </div><h4 id='method.get_unchecked' class="method"><span id='get_unchecked.v' class='invisible'><code>pub unsafe fn <a href='#method.get_unchecked' class='fnname'>get_unchecked</a><I>(&self, i: I) -> &<I as <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>>::<a class="type" href="../../std/slice/trait.SliceIndex.html#associatedtype.Output" title="type std::slice::SliceIndex::Output">Output</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.20.0'>1.20.0</div><a class='srclink' href='../../src/alloc/str.rs.html#445-447' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a unchecked subslice of <code>str</code>.</p> <p>This is the unchecked alternative to indexing the <code>str</code>.</p> <h1 id="safety-3" class="section-header"><a href="#safety-3">Safety</a></h1> <p>Callers of this function are responsible that these preconditions are satisfied:</p> <ul> <li>The starting index must come before the ending index;</li> <li>Indexes must be within bounds of the original slice;</li> <li>Indexes must lie on UTF-8 sequence boundaries.</li> </ul> <p>Failing that, the returned string slice may reference invalid memory or violate the invariants communicated by the <code>str</code> type.</p> <h1 id="examples-41" class="section-header"><a href="#examples-41">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="string">"🗻∈🌏"</span>; <span class="kw">unsafe</span> { <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"🗻"</span>, <span class="ident">v</span>.<span class="ident">get_unchecked</span>(<span class="number">0</span>..<span class="number">4</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"∈"</span>, <span class="ident">v</span>.<span class="ident">get_unchecked</span>(<span class="number">4</span>..<span class="number">7</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"🌏"</span>, <span class="ident">v</span>.<span class="ident">get_unchecked</span>(<span class="number">7</span>..<span class="number">11</span>)); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%20%3D%20%22%F0%9F%97%BB%E2%88%88%F0%9F%8C%8F%22%3B%0Aunsafe%20%7B%0A%20%20%20%20assert_eq!(%22%F0%9F%97%BB%22%2C%20v.get_unchecked(0..4))%3B%0A%20%20%20%20assert_eq!(%22%E2%88%88%22%2C%20v.get_unchecked(4..7))%3B%0A%20%20%20%20assert_eq!(%22%F0%9F%8C%8F%22%2C%20v.get_unchecked(7..11))%3B%0A%7D%0A%7D">Run</a></pre> </div><h4 id='method.get_unchecked_mut' class="method"><span id='get_unchecked_mut.v' class='invisible'><code>pub unsafe fn <a href='#method.get_unchecked_mut' class='fnname'>get_unchecked_mut</a><I>(<br> &mut self, <br> i: I<br>) -> &mut <I as <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>>::<a class="type" href="../../std/slice/trait.SliceIndex.html#associatedtype.Output" title="type std::slice::SliceIndex::Output">Output</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/slice/trait.SliceIndex.html" title="trait std::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="../primitive.str.html">str</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.20.0'>1.20.0</div><a class='srclink' href='../../src/alloc/str.rs.html#477-479' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a mutable, unchecked subslice of <code>str</code>.</p> <p>This is the unchecked alternative to indexing the <code>str</code>.</p> <h1 id="safety-4" class="section-header"><a href="#safety-4">Safety</a></h1> <p>Callers of this function are responsible that these preconditions are satisfied:</p> <ul> <li>The starting index must come before the ending index;</li> <li>Indexes must be within bounds of the original slice;</li> <li>Indexes must lie on UTF-8 sequence boundaries.</li> </ul> <p>Failing that, the returned string slice may reference invalid memory or violate the invariants communicated by the <code>str</code> type.</p> <h1 id="examples-42" class="section-header"><a href="#examples-42">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"🗻∈🌏"</span>); <span class="kw">unsafe</span> { <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"🗻"</span>, <span class="ident">v</span>.<span class="ident">get_unchecked_mut</span>(<span class="number">0</span>..<span class="number">4</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"∈"</span>, <span class="ident">v</span>.<span class="ident">get_unchecked_mut</span>(<span class="number">4</span>..<span class="number">7</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"🌏"</span>, <span class="ident">v</span>.<span class="ident">get_unchecked_mut</span>(<span class="number">7</span>..<span class="number">11</span>)); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20v%20%3D%20String%3A%3Afrom(%22%F0%9F%97%BB%E2%88%88%F0%9F%8C%8F%22)%3B%0Aunsafe%20%7B%0A%20%20%20%20assert_eq!(%22%F0%9F%97%BB%22%2C%20v.get_unchecked_mut(0..4))%3B%0A%20%20%20%20assert_eq!(%22%E2%88%88%22%2C%20v.get_unchecked_mut(4..7))%3B%0A%20%20%20%20assert_eq!(%22%F0%9F%8C%8F%22%2C%20v.get_unchecked_mut(7..11))%3B%0A%7D%0A%7D">Run</a></pre> </div><h4 id='method.slice_unchecked' class="method"><span id='slice_unchecked.v' class='invisible'><code>pub unsafe fn <a href='#method.slice_unchecked' class='fnname'>slice_unchecked</a>(&self, begin: <a class="primitive" href="../primitive.usize.html">usize</a>, end: <a class="primitive" href="../primitive.usize.html">usize</a>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#526-528' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a string slice from another string slice, bypassing safety checks.</p> <p>This is generally not recommended, use with caution! For a safe alternative see <a href="primitive.str.html"><code>str</code></a> and <a href="ops/trait.Index.html"><code>Index</code></a>.</p> <p>This new slice goes from <code>begin</code> to <code>end</code>, including <code>begin</code> but excluding <code>end</code>.</p> <p>To get a mutable string slice instead, see the <a href="#method.slice_mut_unchecked"><code>slice_mut_unchecked</code></a> method.</p> <h1 id="safety-5" class="section-header"><a href="#safety-5">Safety</a></h1> <p>Callers of this function are responsible that three preconditions are satisfied:</p> <ul> <li><code>begin</code> must come before <code>end</code>.</li> <li><code>begin</code> and <code>end</code> must be byte positions within the string slice.</li> <li><code>begin</code> and <code>end</code> must lie on UTF-8 sequence boundaries.</li> </ul> <h1 id="examples-43" class="section-header"><a href="#examples-43">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="kw">unsafe</span> { <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"Löwe 老虎 Léopard"</span>, <span class="ident">s</span>.<span class="ident">slice_unchecked</span>(<span class="number">0</span>, <span class="number">21</span>)); } <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Hello, world!"</span>; <span class="kw">unsafe</span> { <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"world"</span>, <span class="ident">s</span>.<span class="ident">slice_unchecked</span>(<span class="number">7</span>, <span class="number">12</span>)); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20assert_eq!(%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%2C%20s.slice_unchecked(0%2C%2021))%3B%0A%7D%0A%0Alet%20s%20%3D%20%22Hello%2C%20world!%22%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20assert_eq!(%22world%22%2C%20s.slice_unchecked(7%2C%2012))%3B%0A%7D%0A%7D">Run</a></pre> </div><h4 id='method.slice_mut_unchecked' class="method"><span id='slice_mut_unchecked.v' class='invisible'><code>pub unsafe fn <a href='#method.slice_mut_unchecked' class='fnname'>slice_mut_unchecked</a>(<br> &mut self, <br> begin: <a class="primitive" href="../primitive.usize.html">usize</a>, <br> end: <a class="primitive" href="../primitive.usize.html">usize</a><br>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../src/alloc/str.rs.html#556-558' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a string slice from another string slice, bypassing safety checks. This is generally not recommended, use with caution! For a safe alternative see <a href="primitive.str.html"><code>str</code></a> and <a href="ops/trait.IndexMut.html"><code>IndexMut</code></a>.</p> <p>This new slice goes from <code>begin</code> to <code>end</code>, including <code>begin</code> but excluding <code>end</code>.</p> <p>To get an immutable string slice instead, see the <a href="#method.slice_unchecked"><code>slice_unchecked</code></a> method.</p> <h1 id="safety-6" class="section-header"><a href="#safety-6">Safety</a></h1> <p>Callers of this function are responsible that three preconditions are satisfied:</p> <ul> <li><code>begin</code> must come before <code>end</code>.</li> <li><code>begin</code> and <code>end</code> must be byte positions within the string slice.</li> <li><code>begin</code> and <code>end</code> must lie on UTF-8 sequence boundaries.</li> </ul> </div><h4 id='method.split_at' class="method"><span id='split_at.v' class='invisible'><code>pub fn <a href='#method.split_at' class='fnname'>split_at</a>(&self, mid: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="primitive" href="../primitive.tuple.html">(</a>&<a class="primitive" href="../primitive.str.html">str</a>, &<a class="primitive" href="../primitive.str.html">str</a><a class="primitive" href="../primitive.tuple.html">)</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.4.0'>1.4.0</div><a class='srclink' href='../../src/alloc/str.rs.html#592-594' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Divide one string slice into two at an index.</p> <p>The argument, <code>mid</code>, should be a byte offset from the start of the string. It must also be on the boundary of a UTF-8 code point.</p> <p>The two slices returned go from the start of the string slice to <code>mid</code>, and from <code>mid</code> to the end of the string slice.</p> <p>To get mutable string slices instead, see the <a href="#method.split_at_mut"><code>split_at_mut</code></a> method.</p> <h1 id="panics-9" class="section-header"><a href="#panics-9">Panics</a></h1> <p>Panics if <code>mid</code> is not on a UTF-8 code point boundary, or if it is beyond the last code point of the string slice.</p> <h1 id="examples-44" class="section-header"><a href="#examples-44">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Per Martin-Löf"</span>; <span class="kw">let</span> (<span class="ident">first</span>, <span class="ident">last</span>) <span class="op">=</span> <span class="ident">s</span>.<span class="ident">split_at</span>(<span class="number">3</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"Per"</span>, <span class="ident">first</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">" Martin-Löf"</span>, <span class="ident">last</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22Per%20Martin-L%C3%B6f%22%3B%0A%0Alet%20(first%2C%20last)%20%3D%20s.split_at(3)%3B%0A%0Aassert_eq!(%22Per%22%2C%20first)%3B%0Aassert_eq!(%22%20Martin-L%C3%B6f%22%2C%20last)%3B%0A%7D">Run</a></pre> </div><h4 id='method.split_at_mut' class="method"><span id='split_at_mut.v' class='invisible'><code>pub fn <a href='#method.split_at_mut' class='fnname'>split_at_mut</a>(&mut self, mid: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="primitive" href="../primitive.tuple.html">(</a>&mut <a class="primitive" href="../primitive.str.html">str</a>, &mut <a class="primitive" href="../primitive.str.html">str</a><a class="primitive" href="../primitive.tuple.html">)</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.4.0'>1.4.0</div><a class='srclink' href='../../src/alloc/str.rs.html#629-631' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Divide one mutable string slice into two at an index.</p> <p>The argument, <code>mid</code>, should be a byte offset from the start of the string. It must also be on the boundary of a UTF-8 code point.</p> <p>The two slices returned go from the start of the string slice to <code>mid</code>, and from <code>mid</code> to the end of the string slice.</p> <p>To get immutable string slices instead, see the <a href="#method.split_at"><code>split_at</code></a> method.</p> <h1 id="panics-10" class="section-header"><a href="#panics-10">Panics</a></h1> <p>Panics if <code>mid</code> is not on a UTF-8 code point boundary, or if it is beyond the last code point of the string slice.</p> <h1 id="examples-45" class="section-header"><a href="#examples-45">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Per Martin-Löf"</span>.<span class="ident">to_string</span>(); { <span class="kw">let</span> (<span class="ident">first</span>, <span class="ident">last</span>) <span class="op">=</span> <span class="ident">s</span>.<span class="ident">split_at_mut</span>(<span class="number">3</span>); <span class="ident">first</span>.<span class="ident">make_ascii_uppercase</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"PER"</span>, <span class="ident">first</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">" Martin-Löf"</span>, <span class="ident">last</span>); } <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"PER Martin-Löf"</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%0Alet%20mut%20s%20%3D%20%22Per%20Martin-L%C3%B6f%22.to_string()%3B%0A%7B%0A%20%20%20%20let%20(first%2C%20last)%20%3D%20s.split_at_mut(3)%3B%0A%20%20%20%20first.make_ascii_uppercase()%3B%0A%20%20%20%20assert_eq!(%22PER%22%2C%20first)%3B%0A%20%20%20%20assert_eq!(%22%20Martin-L%C3%B6f%22%2C%20last)%3B%0A%7D%0Aassert_eq!(%22PER%20Martin-L%C3%B6f%22%2C%20s)%3B%0A%7D">Run</a></pre> </div><h4 id='method.chars' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.Chars.html" title="struct std::str::Chars">Chars</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.Chars.html" title="struct std::str::Chars">Chars</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.Chars.html" title="struct std::str::Chars">Chars</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.char.html">char</a>;</span></code></div></div><span id='chars.v' class='invisible'><code>pub fn <a href='#method.chars' class='fnname'>chars</a>(&self) -> <a class="struct" href="../../std/str/struct.Chars.html" title="struct std::str::Chars">Chars</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#681-683' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns an iterator over the <a href="primitive.char.html"><code>char</code></a>s of a string slice.</p> <p>As a string slice consists of valid UTF-8, we can iterate through a string slice by <a href="primitive.char.html"><code>char</code></a>. This method returns such an iterator.</p> <p>It's important to remember that <a href="primitive.char.html"><code>char</code></a> represents a Unicode Scalar Value, and may not match your idea of what a 'character' is. Iteration over grapheme clusters may be what you actually want.</p> <h1 id="examples-46" class="section-header"><a href="#examples-46">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">word</span> <span class="op">=</span> <span class="string">"goodbye"</span>; <span class="kw">let</span> <span class="ident">count</span> <span class="op">=</span> <span class="ident">word</span>.<span class="ident">chars</span>().<span class="ident">count</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">7</span>, <span class="ident">count</span>); <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">chars</span> <span class="op">=</span> <span class="ident">word</span>.<span class="ident">chars</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'g'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'o'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'o'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'d'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'b'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'y'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'e'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">chars</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20word%20%3D%20%22goodbye%22%3B%0A%0Alet%20count%20%3D%20word.chars().count()%3B%0Aassert_eq!(7%2C%20count)%3B%0A%0Alet%20mut%20chars%20%3D%20word.chars()%3B%0A%0Aassert_eq!(Some('g')%2C%20chars.next())%3B%0Aassert_eq!(Some('o')%2C%20chars.next())%3B%0Aassert_eq!(Some('o')%2C%20chars.next())%3B%0Aassert_eq!(Some('d')%2C%20chars.next())%3B%0Aassert_eq!(Some('b')%2C%20chars.next())%3B%0Aassert_eq!(Some('y')%2C%20chars.next())%3B%0Aassert_eq!(Some('e')%2C%20chars.next())%3B%0A%0Aassert_eq!(None%2C%20chars.next())%3B%0A%7D">Run</a></pre> <p>Remember, <a href="primitive.char.html"><code>char</code></a>s may not match your human intuition about characters:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="string">"y̆"</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">chars</span> <span class="op">=</span> <span class="ident">y</span>.<span class="ident">chars</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'y'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="comment">// not 'y̆'</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'\u{0306}'</span>), <span class="ident">chars</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">chars</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20y%20%3D%20%22y%CC%86%22%3B%0A%0Alet%20mut%20chars%20%3D%20y.chars()%3B%0A%0Aassert_eq!(Some('y')%2C%20chars.next())%3B%20%2F%2F%20not%20'y%CC%86'%0Aassert_eq!(Some('%5Cu%7B0306%7D')%2C%20chars.next())%3B%0A%0Aassert_eq!(None%2C%20chars.next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.char_indices' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.CharIndices.html" title="struct std::str::CharIndices">CharIndices</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.CharIndices.html" title="struct std::str::CharIndices">CharIndices</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.CharIndices.html" title="struct std::str::CharIndices">CharIndices</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.tuple.html">(</a><a class="primitive" href="../primitive.usize.html">usize</a>, <a class="primitive" href="../primitive.char.html">char</a><a class="primitive" href="../primitive.tuple.html">)</a>;</span></code></div></div><span id='char_indices.v' class='invisible'><code>pub fn <a href='#method.char_indices' class='fnname'>char_indices</a>(&self) -> <a class="struct" href="../../std/str/struct.CharIndices.html" title="struct std::str::CharIndices">CharIndices</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#737-739' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns an iterator over the <a href="primitive.char.html"><code>char</code></a>s of a string slice, and their positions.</p> <p>As a string slice consists of valid UTF-8, we can iterate through a string slice by <a href="primitive.char.html"><code>char</code></a>. This method returns an iterator of both these <a href="primitive.char.html"><code>char</code></a>s, as well as their byte positions.</p> <p>The iterator yields tuples. The position is first, the <a href="primitive.char.html"><code>char</code></a> is second.</p> <h1 id="examples-47" class="section-header"><a href="#examples-47">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">word</span> <span class="op">=</span> <span class="string">"goodbye"</span>; <span class="kw">let</span> <span class="ident">count</span> <span class="op">=</span> <span class="ident">word</span>.<span class="ident">char_indices</span>().<span class="ident">count</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">7</span>, <span class="ident">count</span>); <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">char_indices</span> <span class="op">=</span> <span class="ident">word</span>.<span class="ident">char_indices</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">0</span>, <span class="string">'g'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">1</span>, <span class="string">'o'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">2</span>, <span class="string">'o'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">3</span>, <span class="string">'d'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">4</span>, <span class="string">'b'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">5</span>, <span class="string">'y'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">6</span>, <span class="string">'e'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">char_indices</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20word%20%3D%20%22goodbye%22%3B%0A%0Alet%20count%20%3D%20word.char_indices().count()%3B%0Aassert_eq!(7%2C%20count)%3B%0A%0Alet%20mut%20char_indices%20%3D%20word.char_indices()%3B%0A%0Aassert_eq!(Some((0%2C%20'g'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((1%2C%20'o'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((2%2C%20'o'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((3%2C%20'd'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((4%2C%20'b'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((5%2C%20'y'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((6%2C%20'e'))%2C%20char_indices.next())%3B%0A%0Aassert_eq!(None%2C%20char_indices.next())%3B%0A%7D">Run</a></pre> <p>Remember, <a href="primitive.char.html"><code>char</code></a>s may not match your human intuition about characters:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">yes</span> <span class="op">=</span> <span class="string">"y̆es"</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">char_indices</span> <span class="op">=</span> <span class="ident">yes</span>.<span class="ident">char_indices</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">0</span>, <span class="string">'y'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="comment">// not (0, 'y̆')</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">1</span>, <span class="string">'\u{0306}'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="comment">// note the 3 here - the last character took up two bytes</span> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">3</span>, <span class="string">'e'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>((<span class="number">4</span>, <span class="string">'s'</span>)), <span class="ident">char_indices</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">char_indices</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20yes%20%3D%20%22y%CC%86es%22%3B%0A%0Alet%20mut%20char_indices%20%3D%20yes.char_indices()%3B%0A%0Aassert_eq!(Some((0%2C%20'y'))%2C%20char_indices.next())%3B%20%2F%2F%20not%20(0%2C%20'y%CC%86')%0Aassert_eq!(Some((1%2C%20'%5Cu%7B0306%7D'))%2C%20char_indices.next())%3B%0A%0A%2F%2F%20note%20the%203%20here%20-%20the%20last%20character%20took%20up%20two%20bytes%0Aassert_eq!(Some((3%2C%20'e'))%2C%20char_indices.next())%3B%0Aassert_eq!(Some((4%2C%20's'))%2C%20char_indices.next())%3B%0A%0Aassert_eq!(None%2C%20char_indices.next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.bytes' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.Bytes.html" title="struct std::str::Bytes">Bytes</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.Bytes.html" title="struct std::str::Bytes">Bytes</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.Bytes.html" title="struct std::str::Bytes">Bytes</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.u8.html">u8</a>;</span></code></div></div><span id='bytes.v' class='invisible'><code>pub fn <a href='#method.bytes' class='fnname'>bytes</a>(&self) -> <a class="struct" href="../../std/str/struct.Bytes.html" title="struct std::str::Bytes">Bytes</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#762-764' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over the bytes of a string slice.</p> <p>As a string slice consists of a sequence of bytes, we can iterate through a string slice by byte. This method returns such an iterator.</p> <h1 id="examples-48" class="section-header"><a href="#examples-48">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="string">"bors"</span>.<span class="ident">bytes</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">b'b'</span>), <span class="ident">bytes</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">b'o'</span>), <span class="ident">bytes</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">b'r'</span>), <span class="ident">bytes</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">b's'</span>), <span class="ident">bytes</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">bytes</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20bytes%20%3D%20%22bors%22.bytes()%3B%0A%0Aassert_eq!(Some(b'b')%2C%20bytes.next())%3B%0Aassert_eq!(Some(b'o')%2C%20bytes.next())%3B%0Aassert_eq!(Some(b'r')%2C%20bytes.next())%3B%0Aassert_eq!(Some(b's')%2C%20bytes.next())%3B%0A%0Aassert_eq!(None%2C%20bytes.next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.split_whitespace' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.SplitWhitespace.html" title="struct std::str::SplitWhitespace">SplitWhitespace</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.SplitWhitespace.html" title="struct std::str::SplitWhitespace">SplitWhitespace</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.SplitWhitespace.html" title="struct std::str::SplitWhitespace">SplitWhitespace</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='split_whitespace.v' class='invisible'><code>pub fn <a href='#method.split_whitespace' class='fnname'>split_whitespace</a>(&self) -> <a class="struct" href="../../std/str/struct.SplitWhitespace.html" title="struct std::str::SplitWhitespace">SplitWhitespace</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.1.0'>1.1.0</div><a class='srclink' href='../../src/alloc/str.rs.html#802-804' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Split a string slice by whitespace.</p> <p>The iterator returned will return string slices that are sub-slices of the original string slice, separated by any amount of whitespace.</p> <p>'Whitespace' is defined according to the terms of the Unicode Derived Core Property <code>White_Space</code>.</p> <h1 id="examples-49" class="section-header"><a href="#examples-49">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="string">"A few words"</span>.<span class="ident">split_whitespace</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"A"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"few"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"words"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">iter</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20iter%20%3D%20%22A%20few%20words%22.split_whitespace()%3B%0A%0Aassert_eq!(Some(%22A%22)%2C%20iter.next())%3B%0Aassert_eq!(Some(%22few%22)%2C%20iter.next())%3B%0Aassert_eq!(Some(%22words%22)%2C%20iter.next())%3B%0A%0Aassert_eq!(None%2C%20iter.next())%3B%0A%7D">Run</a></pre> <p>All kinds of whitespace are considered:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="string">" Mary had\ta\u{2009}little \n\t lamb"</span>.<span class="ident">split_whitespace</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"Mary"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"had"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"a"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"little"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"lamb"</span>), <span class="ident">iter</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">iter</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20mut%20iter%20%3D%20%22%20Mary%20%20%20had%5Cta%5Cu%7B2009%7Dlittle%20%20%5Cn%5Ct%20lamb%22.split_whitespace()%3B%0Aassert_eq!(Some(%22Mary%22)%2C%20iter.next())%3B%0Aassert_eq!(Some(%22had%22)%2C%20iter.next())%3B%0Aassert_eq!(Some(%22a%22)%2C%20iter.next())%3B%0Aassert_eq!(Some(%22little%22)%2C%20iter.next())%3B%0Aassert_eq!(Some(%22lamb%22)%2C%20iter.next())%3B%0A%0Aassert_eq!(None%2C%20iter.next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.lines' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.Lines.html" title="struct std::str::Lines">Lines</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.Lines.html" title="struct std::str::Lines">Lines</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.Lines.html" title="struct std::str::Lines">Lines</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='lines.v' class='invisible'><code>pub fn <a href='#method.lines' class='fnname'>lines</a>(&self) -> <a class="struct" href="../../std/str/struct.Lines.html" title="struct std::str::Lines">Lines</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#844-846' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over the lines of a string, as string slices.</p> <p>Lines are ended with either a newline (<code>\n</code>) or a carriage return with a line feed (<code>\r\n</code>).</p> <p>The final line ending is optional.</p> <h1 id="examples-50" class="section-header"><a href="#examples-50">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">text</span> <span class="op">=</span> <span class="string">"foo\r\nbar\n\nbaz\n"</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lines</span> <span class="op">=</span> <span class="ident">text</span>.<span class="ident">lines</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"foo"</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"bar"</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">""</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"baz"</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">lines</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20text%20%3D%20%22foo%5Cr%5Cnbar%5Cn%5Cnbaz%5Cn%22%3B%0Alet%20mut%20lines%20%3D%20text.lines()%3B%0A%0Aassert_eq!(Some(%22foo%22)%2C%20lines.next())%3B%0Aassert_eq!(Some(%22bar%22)%2C%20lines.next())%3B%0Aassert_eq!(Some(%22%22)%2C%20lines.next())%3B%0Aassert_eq!(Some(%22baz%22)%2C%20lines.next())%3B%0A%0Aassert_eq!(None%2C%20lines.next())%3B%0A%7D">Run</a></pre> <p>The final line ending isn't required:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">text</span> <span class="op">=</span> <span class="string">"foo\nbar\n\r\nbaz"</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lines</span> <span class="op">=</span> <span class="ident">text</span>.<span class="ident">lines</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"foo"</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"bar"</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">""</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">"baz"</span>), <span class="ident">lines</span>.<span class="ident">next</span>()); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">lines</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20text%20%3D%20%22foo%5Cnbar%5Cn%5Cr%5Cnbaz%22%3B%0Alet%20mut%20lines%20%3D%20text.lines()%3B%0A%0Aassert_eq!(Some(%22foo%22)%2C%20lines.next())%3B%0Aassert_eq!(Some(%22bar%22)%2C%20lines.next())%3B%0Aassert_eq!(Some(%22%22)%2C%20lines.next())%3B%0Aassert_eq!(Some(%22baz%22)%2C%20lines.next())%3B%0A%0Aassert_eq!(None%2C%20lines.next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.lines_any' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.LinesAny.html" title="struct std::str::LinesAny">LinesAny</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.LinesAny.html" title="struct std::str::LinesAny">LinesAny</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.LinesAny.html" title="struct std::str::LinesAny">LinesAny</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='lines_any.v' class='invisible'><code>pub fn <a href='#method.lines_any' class='fnname'>lines_any</a>(&self) -> <a class="struct" href="../../std/str/struct.LinesAny.html" title="struct std::str::LinesAny">LinesAny</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#853-855' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab deprecated'>Deprecated since 1.4.0<p>: use lines() instead now</p> </div></div><div class='docblock'><p>An iterator over the lines of a string.</p> </div><h4 id='method.encode_utf16' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.EncodeUtf16.html" title="struct std::str::EncodeUtf16">EncodeUtf16</a><'a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.EncodeUtf16.html" title="struct std::str::EncodeUtf16">EncodeUtf16</a><'a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.EncodeUtf16.html" title="struct std::str::EncodeUtf16">EncodeUtf16</a><'a></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.u16.html">u16</a>;</span></code></div></div><span id='encode_utf16.v' class='invisible'><code>pub fn <a href='#method.encode_utf16' class='fnname'>encode_utf16</a>(&self) -> <a class="struct" href="../../std/str/struct.EncodeUtf16.html" title="struct std::str::EncodeUtf16">EncodeUtf16</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.8.0'>1.8.0</div><a class='srclink' href='../../src/alloc/str.rs.html#872-874' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns an iterator of <code>u16</code> over the string encoded as UTF-16.</p> <h1 id="examples-51" class="section-header"><a href="#examples-51">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">text</span> <span class="op">=</span> <span class="string">"Zażółć gęślą jaźń"</span>; <span class="kw">let</span> <span class="ident">utf8_len</span> <span class="op">=</span> <span class="ident">text</span>.<span class="ident">len</span>(); <span class="kw">let</span> <span class="ident">utf16_len</span> <span class="op">=</span> <span class="ident">text</span>.<span class="ident">encode_utf16</span>().<span class="ident">count</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">utf16_len</span> <span class="op"><=</span> <span class="ident">utf8_len</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20text%20%3D%20%22Za%C5%BC%C3%B3%C5%82%C4%87%20g%C4%99%C5%9Bl%C4%85%20ja%C5%BA%C5%84%22%3B%0A%0Alet%20utf8_len%20%3D%20text.len()%3B%0Alet%20utf16_len%20%3D%20text.encode_utf16().count()%3B%0A%0Aassert!(utf16_len%20%3C%3D%20utf8_len)%3B%0A%7D">Run</a></pre> </div><h4 id='method.contains' class="method"><span id='contains.v' class='invisible'><code>pub fn <a href='#method.contains' class='fnname'>contains</a><'a, P>(&'a self, pat: P) -> <a class="primitive" href="../primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#893-895' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns <code>true</code> if the given pattern matches a sub-slice of this string slice.</p> <p>Returns <code>false</code> if it does not.</p> <h1 id="examples-52" class="section-header"><a href="#examples-52">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">bananas</span> <span class="op">=</span> <span class="string">"bananas"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">bananas</span>.<span class="ident">contains</span>(<span class="string">"nana"</span>)); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">bananas</span>.<span class="ident">contains</span>(<span class="string">"apples"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20bananas%20%3D%20%22bananas%22%3B%0A%0Aassert!(bananas.contains(%22nana%22))%3B%0Aassert!(!bananas.contains(%22apples%22))%3B%0A%7D">Run</a></pre> </div><h4 id='method.starts_with' class="method"><span id='starts_with.v' class='invisible'><code>pub fn <a href='#method.starts_with' class='fnname'>starts_with</a><'a, P>(&'a self, pat: P) -> <a class="primitive" href="../primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#913-915' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns <code>true</code> if the given pattern matches a prefix of this string slice.</p> <p>Returns <code>false</code> if it does not.</p> <h1 id="examples-53" class="section-header"><a href="#examples-53">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">bananas</span> <span class="op">=</span> <span class="string">"bananas"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">bananas</span>.<span class="ident">starts_with</span>(<span class="string">"bana"</span>)); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">bananas</span>.<span class="ident">starts_with</span>(<span class="string">"nana"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20bananas%20%3D%20%22bananas%22%3B%0A%0Aassert!(bananas.starts_with(%22bana%22))%3B%0Aassert!(!bananas.starts_with(%22nana%22))%3B%0A%7D">Run</a></pre> </div><h4 id='method.ends_with' class="method"><span id='ends_with.v' class='invisible'><code>pub fn <a href='#method.ends_with' class='fnname'>ends_with</a><'a, P>(&'a self, pat: P) -> <a class="primitive" href="../primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#933-937' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns <code>true</code> if the given pattern matches a suffix of this string slice.</p> <p>Returns <code>false</code> if it does not.</p> <h1 id="examples-54" class="section-header"><a href="#examples-54">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">bananas</span> <span class="op">=</span> <span class="string">"bananas"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">bananas</span>.<span class="ident">ends_with</span>(<span class="string">"anas"</span>)); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">bananas</span>.<span class="ident">ends_with</span>(<span class="string">"nana"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20bananas%20%3D%20%22bananas%22%3B%0A%0Aassert!(bananas.ends_with(%22anas%22))%3B%0Aassert!(!bananas.ends_with(%22nana%22))%3B%0A%7D">Run</a></pre> </div><h4 id='method.find' class="method"><span id='find.v' class='invisible'><code>pub fn <a href='#method.find' class='fnname'>find</a><'a, P>(&'a self, pat: P) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="primitive" href="../primitive.usize.html">usize</a>> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#983-985' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns the byte index of the first character of this string slice that matches the pattern.</p> <p>Returns <a href="option/enum.Option.html#variant.None"><code>None</code></a> if the pattern doesn't match.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="examples-55" class="section-header"><a href="#examples-55">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="string">'L'</span>), <span class="prelude-val">Some</span>(<span class="number">0</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="string">'é'</span>), <span class="prelude-val">Some</span>(<span class="number">14</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="string">"Léopard"</span>), <span class="prelude-val">Some</span>(<span class="number">13</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0A%0Aassert_eq!(s.find('L')%2C%20Some(0))%3B%0Aassert_eq!(s.find('%C3%A9')%2C%20Some(14))%3B%0Aassert_eq!(s.find(%22L%C3%A9opard%22)%2C%20Some(13))%3B%0A%7D">Run</a></pre> <p>More complex patterns using point-free style and closures:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="ident">char</span>::<span class="ident">is_whitespace</span>), <span class="prelude-val">Some</span>(<span class="number">5</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="ident">char</span>::<span class="ident">is_lowercase</span>), <span class="prelude-val">Some</span>(<span class="number">1</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="op">|</span><span class="ident">c</span>: <span class="ident">char</span><span class="op">|</span> <span class="ident">c</span>.<span class="ident">is_whitespace</span>() <span class="op">||</span> <span class="ident">c</span>.<span class="ident">is_lowercase</span>()), <span class="prelude-val">Some</span>(<span class="number">1</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="op">|</span><span class="ident">c</span>: <span class="ident">char</span><span class="op">|</span> (<span class="ident">c</span> <span class="op"><</span> <span class="string">'o'</span>) <span class="op">&&</span> (<span class="ident">c</span> <span class="op">></span> <span class="string">'a'</span>)), <span class="prelude-val">Some</span>(<span class="number">4</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0A%0Aassert_eq!(s.find(char%3A%3Ais_whitespace)%2C%20Some(5))%3B%0Aassert_eq!(s.find(char%3A%3Ais_lowercase)%2C%20Some(1))%3B%0Aassert_eq!(s.find(%7Cc%3A%20char%7C%20c.is_whitespace()%20%7C%7C%20c.is_lowercase())%2C%20Some(1))%3B%0Aassert_eq!(s.find(%7Cc%3A%20char%7C%20(c%20%3C%20'o')%20%26%26%20(c%20%3E%20'a'))%2C%20Some(4))%3B%0A%7D">Run</a></pre> <p>Not finding the pattern:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="kw">let</span> <span class="ident">x</span>: <span class="kw-2">&</span>[_] <span class="op">=</span> <span class="kw-2">&</span>[<span class="string">'1'</span>, <span class="string">'2'</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">find</span>(<span class="ident">x</span>), <span class="prelude-val">None</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0Alet%20x%3A%20%26%5B_%5D%20%3D%20%26%5B'1'%2C%20'2'%5D%3B%0A%0Aassert_eq!(s.find(x)%2C%20None)%3B%0A%7D">Run</a></pre> </div><h4 id='method.rfind' class="method"><span id='rfind.v' class='invisible'><code>pub fn <a href='#method.rfind' class='fnname'>rfind</a><'a, P>(&'a self, pat: P) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="primitive" href="../primitive.usize.html">usize</a>> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1028-1032' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns the byte index of the last character of this string slice that matches the pattern.</p> <p>Returns <a href="option/enum.Option.html#variant.None"><code>None</code></a> if the pattern doesn't match.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="examples-56" class="section-header"><a href="#examples-56">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">rfind</span>(<span class="string">'L'</span>), <span class="prelude-val">Some</span>(<span class="number">13</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">rfind</span>(<span class="string">'é'</span>), <span class="prelude-val">Some</span>(<span class="number">14</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0A%0Aassert_eq!(s.rfind('L')%2C%20Some(13))%3B%0Aassert_eq!(s.rfind('%C3%A9')%2C%20Some(14))%3B%0A%7D">Run</a></pre> <p>More complex patterns with closures:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">rfind</span>(<span class="ident">char</span>::<span class="ident">is_whitespace</span>), <span class="prelude-val">Some</span>(<span class="number">12</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">rfind</span>(<span class="ident">char</span>::<span class="ident">is_lowercase</span>), <span class="prelude-val">Some</span>(<span class="number">20</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0A%0Aassert_eq!(s.rfind(char%3A%3Ais_whitespace)%2C%20Some(12))%3B%0Aassert_eq!(s.rfind(char%3A%3Ais_lowercase)%2C%20Some(20))%3B%0A%7D">Run</a></pre> <p>Not finding the pattern:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Löwe 老虎 Léopard"</span>; <span class="kw">let</span> <span class="ident">x</span>: <span class="kw-2">&</span>[_] <span class="op">=</span> <span class="kw-2">&</span>[<span class="string">'1'</span>, <span class="string">'2'</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">rfind</span>(<span class="ident">x</span>), <span class="prelude-val">None</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22L%C3%B6we%20%E8%80%81%E8%99%8E%20L%C3%A9opard%22%3B%0Alet%20x%3A%20%26%5B_%5D%20%3D%20%26%5B'1'%2C%20'2'%5D%3B%0A%0Aassert_eq!(s.rfind(x)%2C%20None)%3B%0A%7D">Run</a></pre> </div><h4 id='method.split' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.Split.html" title="struct std::str::Split">Split</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.Split.html" title="struct std::str::Split">Split</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.Split.html" title="struct std::str::Split">Split</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='split.v' class='invisible'><code>pub fn <a href='#method.split' class='fnname'>split</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.Split.html" title="struct std::str::Split">Split</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1142-1144' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over substrings of this string slice, separated by characters matched by a pattern.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines the split.</p> <h1 id="iterator-behavior" class="section-header"><a href="#iterator-behavior">Iterator behavior</a></h1> <p>The returned iterator will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if the pattern allows a reverse search and forward/reverse search yields the same elements. This is true for, eg, <a href="primitive.char.html"><code>char</code></a> but not for <code>&str</code>.</p> <p>If the pattern allows a reverse search but its results might differ from a forward search, the <a href="#method.rsplit"><code>rsplit</code></a> method can be used.</p> <h1 id="examples-57" class="section-header"><a href="#examples-57">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"Mary had a little lamb"</span>.<span class="ident">split</span>(<span class="string">' '</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"Mary"</span>, <span class="string">"had"</span>, <span class="string">"a"</span>, <span class="string">"little"</span>, <span class="string">"lamb"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">""</span>.<span class="ident">split</span>(<span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">""</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lionXXtigerXleopard"</span>.<span class="ident">split</span>(<span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"lion"</span>, <span class="string">""</span>, <span class="string">"tiger"</span>, <span class="string">"leopard"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lion::tiger::leopard"</span>.<span class="ident">split</span>(<span class="string">"::"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"lion"</span>, <span class="string">"tiger"</span>, <span class="string">"leopard"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abc1def2ghi"</span>.<span class="ident">split</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"abc"</span>, <span class="string">"def"</span>, <span class="string">"ghi"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lionXtigerXleopard"</span>.<span class="ident">split</span>(<span class="ident">char</span>::<span class="ident">is_uppercase</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"lion"</span>, <span class="string">"tiger"</span>, <span class="string">"leopard"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22Mary%20had%20a%20little%20lamb%22.split('%20').collect()%3B%0Aassert_eq!(v%2C%20%5B%22Mary%22%2C%20%22had%22%2C%20%22a%22%2C%20%22little%22%2C%20%22lamb%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22%22.split('X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lionXXtigerXleopard%22.split('X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22lion%22%2C%20%22%22%2C%20%22tiger%22%2C%20%22leopard%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lion%3A%3Atiger%3A%3Aleopard%22.split(%22%3A%3A%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22lion%22%2C%20%22tiger%22%2C%20%22leopard%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abc1def2ghi%22.split(char%3A%3Ais_numeric).collect()%3B%0Aassert_eq!(v%2C%20%5B%22abc%22%2C%20%22def%22%2C%20%22ghi%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lionXtigerXleopard%22.split(char%3A%3Ais_uppercase).collect()%3B%0Aassert_eq!(v%2C%20%5B%22lion%22%2C%20%22tiger%22%2C%20%22leopard%22%5D)%3B%0A%7D">Run</a></pre> <p>A more complex pattern, using a closure:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abc1defXghi"</span>.<span class="ident">split</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'1'</span> <span class="op">||</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"abc"</span>, <span class="string">"def"</span>, <span class="string">"ghi"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abc1defXghi%22.split(%7Cc%7C%20c%20%3D%3D%20'1'%20%7C%7C%20c%20%3D%3D%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22abc%22%2C%20%22def%22%2C%20%22ghi%22%5D)%3B%0A%7D">Run</a></pre> <p>If a string contains multiple contiguous separators, you will end up with empty strings in the output:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="string">"||||a||b|c"</span>.<span class="ident">to_string</span>(); <span class="kw">let</span> <span class="ident">d</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split</span>(<span class="string">'|'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">d</span>, <span class="kw-2">&</span>[<span class="string">""</span>, <span class="string">""</span>, <span class="string">""</span>, <span class="string">""</span>, <span class="string">"a"</span>, <span class="string">""</span>, <span class="string">"b"</span>, <span class="string">"c"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20x%20%3D%20%22%7C%7C%7C%7Ca%7C%7Cb%7Cc%22.to_string()%3B%0Alet%20d%3A%20Vec%3C_%3E%20%3D%20x.split('%7C').collect()%3B%0A%0Aassert_eq!(d%2C%20%26%5B%22%22%2C%20%22%22%2C%20%22%22%2C%20%22%22%2C%20%22a%22%2C%20%22%22%2C%20%22b%22%2C%20%22c%22%5D)%3B%0A%7D">Run</a></pre> <p>Contiguous separators are separated by the empty string.</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="string">"(///)"</span>.<span class="ident">to_string</span>(); <span class="kw">let</span> <span class="ident">d</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split</span>(<span class="string">'/'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">d</span>, <span class="kw-2">&</span>[<span class="string">"("</span>, <span class="string">""</span>, <span class="string">""</span>, <span class="string">")"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20x%20%3D%20%22(%2F%2F%2F)%22.to_string()%3B%0Alet%20d%3A%20Vec%3C_%3E%20%3D%20x.split('%2F').collect()%3B%0A%0Aassert_eq!(d%2C%20%26%5B%22(%22%2C%20%22%22%2C%20%22%22%2C%20%22)%22%5D)%3B%0A%7D">Run</a></pre> <p>Separators at the start or end of a string are neighbored by empty strings.</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">d</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"010"</span>.<span class="ident">split</span>(<span class="string">"0"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">d</span>, <span class="kw-2">&</span>[<span class="string">""</span>, <span class="string">"1"</span>, <span class="string">""</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20d%3A%20Vec%3C_%3E%20%3D%20%22010%22.split(%220%22).collect()%3B%0Aassert_eq!(d%2C%20%26%5B%22%22%2C%20%221%22%2C%20%22%22%5D)%3B%0A%7D">Run</a></pre> <p>When the empty string is used as a separator, it separates every character in the string, along with the beginning and end of the string.</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">f</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"rust"</span>.<span class="ident">split</span>(<span class="string">""</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f</span>, <span class="kw-2">&</span>[<span class="string">""</span>, <span class="string">"r"</span>, <span class="string">"u"</span>, <span class="string">"s"</span>, <span class="string">"t"</span>, <span class="string">""</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20f%3A%20Vec%3C_%3E%20%3D%20%22rust%22.split(%22%22).collect()%3B%0Aassert_eq!(f%2C%20%26%5B%22%22%2C%20%22r%22%2C%20%22u%22%2C%20%22s%22%2C%20%22t%22%2C%20%22%22%5D)%3B%0A%7D">Run</a></pre> <p>Contiguous separators can lead to possibly surprising behavior when whitespace is used as the separator. This code is correct:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="string">" a b c"</span>.<span class="ident">to_string</span>(); <span class="kw">let</span> <span class="ident">d</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split</span>(<span class="string">' '</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">d</span>, <span class="kw-2">&</span>[<span class="string">""</span>, <span class="string">""</span>, <span class="string">""</span>, <span class="string">""</span>, <span class="string">"a"</span>, <span class="string">""</span>, <span class="string">"b"</span>, <span class="string">"c"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20x%20%3D%20%22%20%20%20%20a%20%20b%20c%22.to_string()%3B%0Alet%20d%3A%20Vec%3C_%3E%20%3D%20x.split('%20').collect()%3B%0A%0Aassert_eq!(d%2C%20%26%5B%22%22%2C%20%22%22%2C%20%22%22%2C%20%22%22%2C%20%22a%22%2C%20%22%22%2C%20%22b%22%2C%20%22c%22%5D)%3B%0A%7D">Run</a></pre> <p>It does <em>not</em> give you:</p> <div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><pre class="rust rust-example-rendered ignore"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">d</span>, <span class="kw-2">&</span>[<span class="string">"a"</span>, <span class="string">"b"</span>, <span class="string">"c"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(d%2C%20%26%5B%22a%22%2C%20%22b%22%2C%20%22c%22%5D)%3B%0A%7D">Run</a></pre> <p>Use <a href="#method.split_whitespace"><code>split_whitespace</code></a> for this behavior.</p> </div><h4 id='method.rsplit' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.RSplit.html" title="struct std::str::RSplit">RSplit</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.RSplit.html" title="struct std::str::RSplit">RSplit</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.RSplit.html" title="struct std::str::RSplit">RSplit</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='rsplit.v' class='invisible'><code>pub fn <a href='#method.rsplit' class='fnname'>rsplit</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.RSplit.html" title="struct std::str::RSplit">RSplit</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1192-1196' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over substrings of the given string slice, separated by characters matched by a pattern and yielded in reverse order.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines the split.</p> <h1 id="iterator-behavior-1" class="section-header"><a href="#iterator-behavior-1">Iterator behavior</a></h1> <p>The returned iterator requires that the pattern supports a reverse search, and it will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if a forward/reverse search yields the same elements.</p> <p>For iterating from the front, the <a href="#method.split"><code>split</code></a> method can be used.</p> <h1 id="examples-58" class="section-header"><a href="#examples-58">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"Mary had a little lamb"</span>.<span class="ident">rsplit</span>(<span class="string">' '</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"lamb"</span>, <span class="string">"little"</span>, <span class="string">"a"</span>, <span class="string">"had"</span>, <span class="string">"Mary"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">""</span>.<span class="ident">rsplit</span>(<span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">""</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lionXXtigerXleopard"</span>.<span class="ident">rsplit</span>(<span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"leopard"</span>, <span class="string">"tiger"</span>, <span class="string">""</span>, <span class="string">"lion"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lion::tiger::leopard"</span>.<span class="ident">rsplit</span>(<span class="string">"::"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"leopard"</span>, <span class="string">"tiger"</span>, <span class="string">"lion"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22Mary%20had%20a%20little%20lamb%22.rsplit('%20').collect()%3B%0Aassert_eq!(v%2C%20%5B%22lamb%22%2C%20%22little%22%2C%20%22a%22%2C%20%22had%22%2C%20%22Mary%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22%22.rsplit('X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lionXXtigerXleopard%22.rsplit('X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22leopard%22%2C%20%22tiger%22%2C%20%22%22%2C%20%22lion%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lion%3A%3Atiger%3A%3Aleopard%22.rsplit(%22%3A%3A%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22leopard%22%2C%20%22tiger%22%2C%20%22lion%22%5D)%3B%0A%7D">Run</a></pre> <p>A more complex pattern, using a closure:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abc1defXghi"</span>.<span class="ident">rsplit</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'1'</span> <span class="op">||</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"ghi"</span>, <span class="string">"def"</span>, <span class="string">"abc"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abc1defXghi%22.rsplit(%7Cc%7C%20c%20%3D%3D%20'1'%20%7C%7C%20c%20%3D%3D%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22ghi%22%2C%20%22def%22%2C%20%22abc%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.split_terminator' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.SplitTerminator.html" title="struct std::str::SplitTerminator">SplitTerminator</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.SplitTerminator.html" title="struct std::str::SplitTerminator">SplitTerminator</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.SplitTerminator.html" title="struct std::str::SplitTerminator">SplitTerminator</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='split_terminator.v' class='invisible'><code>pub fn <a href='#method.split_terminator' class='fnname'>split_terminator</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.SplitTerminator.html" title="struct std::str::SplitTerminator">SplitTerminator</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1239-1241' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over substrings of the given string slice, separated by characters matched by a pattern.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines the split.</p> <p>Equivalent to <a href="#method.split"><code>split</code></a>, except that the trailing substring is skipped if empty.</p> <p>This method can be used for string data that is <em>terminated</em>, rather than <em>separated</em> by a pattern.</p> <h1 id="iterator-behavior-2" class="section-header"><a href="#iterator-behavior-2">Iterator behavior</a></h1> <p>The returned iterator will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if the pattern allows a reverse search and forward/reverse search yields the same elements. This is true for, eg, <a href="primitive.char.html"><code>char</code></a> but not for <code>&str</code>.</p> <p>If the pattern allows a reverse search but its results might differ from a forward search, the <a href="#method.rsplit_terminator"><code>rsplit_terminator</code></a> method can be used.</p> <h1 id="examples-59" class="section-header"><a href="#examples-59">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"A.B."</span>.<span class="ident">split_terminator</span>(<span class="string">'.'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"A"</span>, <span class="string">"B"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"A..B.."</span>.<span class="ident">split_terminator</span>(<span class="string">"."</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"A"</span>, <span class="string">""</span>, <span class="string">"B"</span>, <span class="string">""</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22A.B.%22.split_terminator('.').collect()%3B%0Aassert_eq!(v%2C%20%5B%22A%22%2C%20%22B%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22A..B..%22.split_terminator(%22.%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22A%22%2C%20%22%22%2C%20%22B%22%2C%20%22%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.rsplit_terminator' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.RSplitTerminator.html" title="struct std::str::RSplitTerminator">RSplitTerminator</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.RSplitTerminator.html" title="struct std::str::RSplitTerminator">RSplitTerminator</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.RSplitTerminator.html" title="struct std::str::RSplitTerminator">RSplitTerminator</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='rsplit_terminator.v' class='invisible'><code>pub fn <a href='#method.rsplit_terminator' class='fnname'>rsplit_terminator</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.RSplitTerminator.html" title="struct std::str::RSplitTerminator">RSplitTerminator</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1283-1287' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over substrings of <code>self</code>, separated by characters matched by a pattern and yielded in reverse order.</p> <p>The pattern can be a simple <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines the split. Additional libraries might provide more complex patterns like regular expressions.</p> <p>Equivalent to <a href="#method.split"><code>split</code></a>, except that the trailing substring is skipped if empty.</p> <p>This method can be used for string data that is <em>terminated</em>, rather than <em>separated</em> by a pattern.</p> <h1 id="iterator-behavior-3" class="section-header"><a href="#iterator-behavior-3">Iterator behavior</a></h1> <p>The returned iterator requires that the pattern supports a reverse search, and it will be double ended if a forward/reverse search yields the same elements.</p> <p>For iterating from the front, the <a href="#method.split_terminator"><code>split_terminator</code></a> method can be used.</p> <h1 id="examples-60" class="section-header"><a href="#examples-60">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"A.B."</span>.<span class="ident">rsplit_terminator</span>(<span class="string">'.'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"B"</span>, <span class="string">"A"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"A..B.."</span>.<span class="ident">rsplit_terminator</span>(<span class="string">"."</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">""</span>, <span class="string">"B"</span>, <span class="string">""</span>, <span class="string">"A"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22A.B.%22.rsplit_terminator('.').collect()%3B%0Aassert_eq!(v%2C%20%5B%22B%22%2C%20%22A%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22A..B..%22.rsplit_terminator(%22.%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22%22%2C%20%22B%22%2C%20%22%22%2C%20%22A%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.splitn' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.SplitN.html" title="struct std::str::SplitN">SplitN</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.SplitN.html" title="struct std::str::SplitN">SplitN</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.SplitN.html" title="struct std::str::SplitN">SplitN</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='splitn.v' class='invisible'><code>pub fn <a href='#method.splitn' class='fnname'>splitn</a><'a, P>(&'a self, n: <a class="primitive" href="../primitive.usize.html">usize</a>, pat: P) -> <a class="struct" href="../../std/str/struct.SplitN.html" title="struct std::str::SplitN">SplitN</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1336-1338' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over substrings of the given string slice, separated by a pattern, restricted to returning at most <code>n</code> items.</p> <p>If <code>n</code> substrings are returned, the last substring (the <code>n</code>th substring) will contain the remainder of the string.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines the split.</p> <h1 id="iterator-behavior-4" class="section-header"><a href="#iterator-behavior-4">Iterator behavior</a></h1> <p>The returned iterator will not be double ended, because it is not efficient to support.</p> <p>If the pattern allows a reverse search, the <a href="#method.rsplitn"><code>rsplitn</code></a> method can be used.</p> <h1 id="examples-61" class="section-header"><a href="#examples-61">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"Mary had a little lambda"</span>.<span class="ident">splitn</span>(<span class="number">3</span>, <span class="string">' '</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"Mary"</span>, <span class="string">"had"</span>, <span class="string">"a little lambda"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lionXXtigerXleopard"</span>.<span class="ident">splitn</span>(<span class="number">3</span>, <span class="string">"X"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"lion"</span>, <span class="string">""</span>, <span class="string">"tigerXleopard"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abcXdef"</span>.<span class="ident">splitn</span>(<span class="number">1</span>, <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"abcXdef"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">""</span>.<span class="ident">splitn</span>(<span class="number">1</span>, <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">""</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22Mary%20had%20a%20little%20lambda%22.splitn(3%2C%20'%20').collect()%3B%0Aassert_eq!(v%2C%20%5B%22Mary%22%2C%20%22had%22%2C%20%22a%20little%20lambda%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lionXXtigerXleopard%22.splitn(3%2C%20%22X%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22lion%22%2C%20%22%22%2C%20%22tigerXleopard%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abcXdef%22.splitn(1%2C%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22abcXdef%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22%22.splitn(1%2C%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22%22%5D)%3B%0A%7D">Run</a></pre> <p>A more complex pattern, using a closure:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abc1defXghi"</span>.<span class="ident">splitn</span>(<span class="number">2</span>, <span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'1'</span> <span class="op">||</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"abc"</span>, <span class="string">"defXghi"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abc1defXghi%22.splitn(2%2C%20%7Cc%7C%20c%20%3D%3D%20'1'%20%7C%7C%20c%20%3D%3D%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22abc%22%2C%20%22defXghi%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.rsplitn' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.RSplitN.html" title="struct std::str::RSplitN">RSplitN</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.RSplitN.html" title="struct std::str::RSplitN">RSplitN</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.RSplitN.html" title="struct std::str::RSplitN">RSplitN</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='rsplitn.v' class='invisible'><code>pub fn <a href='#method.rsplitn' class='fnname'>rsplitn</a><'a, P>(&'a self, n: <a class="primitive" href="../primitive.usize.html">usize</a>, pat: P) -> <a class="struct" href="../../std/str/struct.RSplitN.html" title="struct std::str::RSplitN">RSplitN</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1384-1388' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over substrings of this string slice, separated by a pattern, starting from the end of the string, restricted to returning at most <code>n</code> items.</p> <p>If <code>n</code> substrings are returned, the last substring (the <code>n</code>th substring) will contain the remainder of the string.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines the split.</p> <h1 id="iterator-behavior-5" class="section-header"><a href="#iterator-behavior-5">Iterator behavior</a></h1> <p>The returned iterator will not be double ended, because it is not efficient to support.</p> <p>For splitting from the front, the <a href="#method.splitn"><code>splitn</code></a> method can be used.</p> <h1 id="examples-62" class="section-header"><a href="#examples-62">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"Mary had a little lamb"</span>.<span class="ident">rsplitn</span>(<span class="number">3</span>, <span class="string">' '</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"lamb"</span>, <span class="string">"little"</span>, <span class="string">"Mary had a"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lionXXtigerXleopard"</span>.<span class="ident">rsplitn</span>(<span class="number">3</span>, <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"leopard"</span>, <span class="string">"tiger"</span>, <span class="string">"lionX"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"lion::tiger::leopard"</span>.<span class="ident">rsplitn</span>(<span class="number">2</span>, <span class="string">"::"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"leopard"</span>, <span class="string">"lion::tiger"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22Mary%20had%20a%20little%20lamb%22.rsplitn(3%2C%20'%20').collect()%3B%0Aassert_eq!(v%2C%20%5B%22lamb%22%2C%20%22little%22%2C%20%22Mary%20had%20a%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lionXXtigerXleopard%22.rsplitn(3%2C%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22leopard%22%2C%20%22tiger%22%2C%20%22lionX%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22lion%3A%3Atiger%3A%3Aleopard%22.rsplitn(2%2C%20%22%3A%3A%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22leopard%22%2C%20%22lion%3A%3Atiger%22%5D)%3B%0A%7D">Run</a></pre> <p>A more complex pattern, using a closure:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abc1defXghi"</span>.<span class="ident">rsplitn</span>(<span class="number">2</span>, <span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'1'</span> <span class="op">||</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'X'</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"ghi"</span>, <span class="string">"abc1def"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abc1defXghi%22.rsplitn(2%2C%20%7Cc%7C%20c%20%3D%3D%20'1'%20%7C%7C%20c%20%3D%3D%20'X').collect()%3B%0Aassert_eq!(v%2C%20%5B%22ghi%22%2C%20%22abc1def%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.matches' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.Matches.html" title="struct std::str::Matches">Matches</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.Matches.html" title="struct std::str::Matches">Matches</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.Matches.html" title="struct std::str::Matches">Matches</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='matches.v' class='invisible'><code>pub fn <a href='#method.matches' class='fnname'>matches</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.Matches.html" title="struct std::str::Matches">Matches</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1425-1427' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over the disjoint matches of a pattern within the given string slice.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="iterator-behavior-6" class="section-header"><a href="#iterator-behavior-6">Iterator behavior</a></h1> <p>The returned iterator will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if the pattern allows a reverse search and forward/reverse search yields the same elements. This is true for, eg, <a href="primitive.char.html"><code>char</code></a> but not for <code>&str</code>.</p> <p>If the pattern allows a reverse search but its results might differ from a forward search, the <a href="#method.rmatches"><code>rmatches</code></a> method can be used.</p> <h1 id="examples-63" class="section-header"><a href="#examples-63">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abcXXXabcYYYabc"</span>.<span class="ident">matches</span>(<span class="string">"abc"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"abc"</span>, <span class="string">"abc"</span>, <span class="string">"abc"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"1abc2abc3"</span>.<span class="ident">matches</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"1"</span>, <span class="string">"2"</span>, <span class="string">"3"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abcXXXabcYYYabc%22.matches(%22abc%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22abc%22%2C%20%22abc%22%2C%20%22abc%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%221abc2abc3%22.matches(char%3A%3Ais_numeric).collect()%3B%0Aassert_eq!(v%2C%20%5B%221%22%2C%20%222%22%2C%20%223%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.rmatches' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.RMatches.html" title="struct std::str::RMatches">RMatches</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.RMatches.html" title="struct std::str::RMatches">RMatches</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.RMatches.html" title="struct std::str::RMatches">RMatches</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &'a <a class="primitive" href="../primitive.str.html">str</a>;</span></code></div></div><span id='rmatches.v' class='invisible'><code>pub fn <a href='#method.rmatches' class='fnname'>rmatches</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.RMatches.html" title="struct std::str::RMatches">RMatches</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1462-1466' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over the disjoint matches of a pattern within this string slice, yielded in reverse order.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="iterator-behavior-7" class="section-header"><a href="#iterator-behavior-7">Iterator behavior</a></h1> <p>The returned iterator requires that the pattern supports a reverse search, and it will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if a forward/reverse search yields the same elements.</p> <p>For iterating from the front, the <a href="#method.matches"><code>matches</code></a> method can be used.</p> <h1 id="examples-64" class="section-header"><a href="#examples-64">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"abcXXXabcYYYabc"</span>.<span class="ident">rmatches</span>(<span class="string">"abc"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"abc"</span>, <span class="string">"abc"</span>, <span class="string">"abc"</span>]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span><span class="ident">str</span><span class="op">></span> <span class="op">=</span> <span class="string">"1abc2abc3"</span>.<span class="ident">rmatches</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="string">"3"</span>, <span class="string">"2"</span>, <span class="string">"1"</span>]);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%22abcXXXabcYYYabc%22.rmatches(%22abc%22).collect()%3B%0Aassert_eq!(v%2C%20%5B%22abc%22%2C%20%22abc%22%2C%20%22abc%22%5D)%3B%0A%0Alet%20v%3A%20Vec%3C%26str%3E%20%3D%20%221abc2abc3%22.rmatches(char%3A%3Ais_numeric).collect()%3B%0Aassert_eq!(v%2C%20%5B%223%22%2C%20%222%22%2C%20%221%22%5D)%3B%0A%7D">Run</a></pre> </div><h4 id='method.match_indices' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.MatchIndices.html" title="struct std::str::MatchIndices">MatchIndices</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.MatchIndices.html" title="struct std::str::MatchIndices">MatchIndices</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.MatchIndices.html" title="struct std::str::MatchIndices">MatchIndices</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.tuple.html">(</a><a class="primitive" href="../primitive.usize.html">usize</a>, &'a <a class="primitive" href="../primitive.str.html">str</a><a class="primitive" href="../primitive.tuple.html">)</a>;</span></code></div></div><span id='match_indices.v' class='invisible'><code>pub fn <a href='#method.match_indices' class='fnname'>match_indices</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.MatchIndices.html" title="struct std::str::MatchIndices">MatchIndices</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1508-1510' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over the disjoint matches of a pattern within this string slice as well as the index that the match starts at.</p> <p>For matches of <code>pat</code> within <code>self</code> that overlap, only the indices corresponding to the first match are returned.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="iterator-behavior-8" class="section-header"><a href="#iterator-behavior-8">Iterator behavior</a></h1> <p>The returned iterator will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if the pattern allows a reverse search and forward/reverse search yields the same elements. This is true for, eg, <a href="primitive.char.html"><code>char</code></a> but not for <code>&str</code>.</p> <p>If the pattern allows a reverse search but its results might differ from a forward search, the <a href="#method.rmatch_indices"><code>rmatch_indices</code></a> method can be used.</p> <h1 id="examples-65" class="section-header"><a href="#examples-65">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"abcXXXabcYYYabc"</span>.<span class="ident">match_indices</span>(<span class="string">"abc"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [(<span class="number">0</span>, <span class="string">"abc"</span>), (<span class="number">6</span>, <span class="string">"abc"</span>), (<span class="number">12</span>, <span class="string">"abc"</span>)]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"1abcabc2"</span>.<span class="ident">match_indices</span>(<span class="string">"abc"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [(<span class="number">1</span>, <span class="string">"abc"</span>), (<span class="number">4</span>, <span class="string">"abc"</span>)]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"ababa"</span>.<span class="ident">match_indices</span>(<span class="string">"aba"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [(<span class="number">0</span>, <span class="string">"aba"</span>)]); <span class="comment">// only the first `aba`</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C_%3E%20%3D%20%22abcXXXabcYYYabc%22.match_indices(%22abc%22).collect()%3B%0Aassert_eq!(v%2C%20%5B(0%2C%20%22abc%22)%2C%20(6%2C%20%22abc%22)%2C%20(12%2C%20%22abc%22)%5D)%3B%0A%0Alet%20v%3A%20Vec%3C_%3E%20%3D%20%221abcabc2%22.match_indices(%22abc%22).collect()%3B%0Aassert_eq!(v%2C%20%5B(1%2C%20%22abc%22)%2C%20(4%2C%20%22abc%22)%5D)%3B%0A%0Alet%20v%3A%20Vec%3C_%3E%20%3D%20%22ababa%22.match_indices(%22aba%22).collect()%3B%0Aassert_eq!(v%2C%20%5B(0%2C%20%22aba%22)%5D)%3B%20%2F%2F%20only%20the%20first%20%60aba%60%0A%7D">Run</a></pre> </div><h4 id='method.rmatch_indices' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/str/struct.RMatchIndices.html" title="struct std::str::RMatchIndices">RMatchIndices</a><'a, P></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/str/struct.RMatchIndices.html" title="struct std::str::RMatchIndices">RMatchIndices</a><'a, P></h3><code class="content"><span class="where fmt-newline">impl<'a, P> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/str/struct.RMatchIndices.html" title="struct std::str::RMatchIndices">RMatchIndices</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="../primitive.tuple.html">(</a><a class="primitive" href="../primitive.usize.html">usize</a>, &'a <a class="primitive" href="../primitive.str.html">str</a><a class="primitive" href="../primitive.tuple.html">)</a>;</span></code></div></div><span id='rmatch_indices.v' class='invisible'><code>pub fn <a href='#method.rmatch_indices' class='fnname'>rmatch_indices</a><'a, P>(&'a self, pat: P) -> <a class="struct" href="../../std/str/struct.RMatchIndices.html" title="struct std::str::RMatchIndices">RMatchIndices</a><'a, P> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1551-1555' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>An iterator over the disjoint matches of a pattern within <code>self</code>, yielded in reverse order along with the index of the match.</p> <p>For matches of <code>pat</code> within <code>self</code> that overlap, only the indices corresponding to the last match are returned.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="iterator-behavior-9" class="section-header"><a href="#iterator-behavior-9">Iterator behavior</a></h1> <p>The returned iterator requires that the pattern supports a reverse search, and it will be a <a href="iter/trait.DoubleEndedIterator.html"><code>DoubleEndedIterator</code></a> if a forward/reverse search yields the same elements.</p> <p>For iterating from the front, the <a href="#method.match_indices"><code>match_indices</code></a> method can be used.</p> <h1 id="examples-66" class="section-header"><a href="#examples-66">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"abcXXXabcYYYabc"</span>.<span class="ident">rmatch_indices</span>(<span class="string">"abc"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [(<span class="number">12</span>, <span class="string">"abc"</span>), (<span class="number">6</span>, <span class="string">"abc"</span>), (<span class="number">0</span>, <span class="string">"abc"</span>)]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"1abcabc2"</span>.<span class="ident">rmatch_indices</span>(<span class="string">"abc"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [(<span class="number">4</span>, <span class="string">"abc"</span>), (<span class="number">1</span>, <span class="string">"abc"</span>)]); <span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="string">"ababa"</span>.<span class="ident">rmatch_indices</span>(<span class="string">"aba"</span>).<span class="ident">collect</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [(<span class="number">2</span>, <span class="string">"aba"</span>)]); <span class="comment">// only the last `aba`</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20v%3A%20Vec%3C_%3E%20%3D%20%22abcXXXabcYYYabc%22.rmatch_indices(%22abc%22).collect()%3B%0Aassert_eq!(v%2C%20%5B(12%2C%20%22abc%22)%2C%20(6%2C%20%22abc%22)%2C%20(0%2C%20%22abc%22)%5D)%3B%0A%0Alet%20v%3A%20Vec%3C_%3E%20%3D%20%221abcabc2%22.rmatch_indices(%22abc%22).collect()%3B%0Aassert_eq!(v%2C%20%5B(4%2C%20%22abc%22)%2C%20(1%2C%20%22abc%22)%5D)%3B%0A%0Alet%20v%3A%20Vec%3C_%3E%20%3D%20%22ababa%22.rmatch_indices(%22aba%22).collect()%3B%0Aassert_eq!(v%2C%20%5B(2%2C%20%22aba%22)%5D)%3B%20%2F%2F%20only%20the%20last%20%60aba%60%0A%7D">Run</a></pre> </div><h4 id='method.trim' class="method"><span id='trim.v' class='invisible'><code>pub fn <a href='#method.trim' class='fnname'>trim</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1572-1574' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a string slice with leading and trailing whitespace removed.</p> <p>'Whitespace' is defined according to the terms of the Unicode Derived Core Property <code>White_Space</code>.</p> <h1 id="examples-67" class="section-header"><a href="#examples-67">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">" Hello\tworld\t"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"Hello\tworld"</span>, <span class="ident">s</span>.<span class="ident">trim</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22%20Hello%5Ctworld%5Ct%22%3B%0A%0Aassert_eq!(%22Hello%5Ctworld%22%2C%20s.trim())%3B%0A%7D">Run</a></pre> </div><h4 id='method.trim_left' class="method"><span id='trim_left.v' class='invisible'><code>pub fn <a href='#method.trim_left' class='fnname'>trim_left</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1608-1610' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a string slice with leading whitespace removed.</p> <p>'Whitespace' is defined according to the terms of the Unicode Derived Core Property <code>White_Space</code>.</p> <h1 id="text-directionality" class="section-header"><a href="#text-directionality">Text directionality</a></h1> <p>A string is a sequence of bytes. 'Left' in this context means the first position of that byte string; for a language like Arabic or Hebrew which are 'right to left' rather than 'left to right', this will be the <em>right</em> side, not the left.</p> <h1 id="examples-68" class="section-header"><a href="#examples-68">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">" Hello\tworld\t"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"Hello\tworld\t"</span>, <span class="ident">s</span>.<span class="ident">trim_left</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22%20Hello%5Ctworld%5Ct%22%3B%0A%0Aassert_eq!(%22Hello%5Ctworld%5Ct%22%2C%20s.trim_left())%3B%0A%7D">Run</a></pre> <p>Directionality:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">" English"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'E'</span>) <span class="op">==</span> <span class="ident">s</span>.<span class="ident">trim_left</span>().<span class="ident">chars</span>().<span class="ident">next</span>()); <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">" עברית"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'ע'</span>) <span class="op">==</span> <span class="ident">s</span>.<span class="ident">trim_left</span>().<span class="ident">chars</span>().<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22%20%20English%22%3B%0Aassert!(Some('E')%20%3D%3D%20s.trim_left().chars().next())%3B%0A%0Alet%20s%20%3D%20%22%20%20%D7%A2%D7%91%D7%A8%D7%99%D7%AA%22%3B%0Aassert!(Some('%D7%A2')%20%3D%3D%20s.trim_left().chars().next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.trim_right' class="method"><span id='trim_right.v' class='invisible'><code>pub fn <a href='#method.trim_right' class='fnname'>trim_right</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1644-1646' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a string slice with trailing whitespace removed.</p> <p>'Whitespace' is defined according to the terms of the Unicode Derived Core Property <code>White_Space</code>.</p> <h1 id="text-directionality-1" class="section-header"><a href="#text-directionality-1">Text directionality</a></h1> <p>A string is a sequence of bytes. 'Right' in this context means the last position of that byte string; for a language like Arabic or Hebrew which are 'right to left' rather than 'left to right', this will be the <em>left</em> side, not the right.</p> <h1 id="examples-69" class="section-header"><a href="#examples-69">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">" Hello\tworld\t"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">" Hello\tworld"</span>, <span class="ident">s</span>.<span class="ident">trim_right</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22%20Hello%5Ctworld%5Ct%22%3B%0A%0Aassert_eq!(%22%20Hello%5Ctworld%22%2C%20s.trim_right())%3B%0A%7D">Run</a></pre> <p>Directionality:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"English "</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'h'</span>) <span class="op">==</span> <span class="ident">s</span>.<span class="ident">trim_right</span>().<span class="ident">chars</span>().<span class="ident">rev</span>().<span class="ident">next</span>()); <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"עברית "</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="string">'ת'</span>) <span class="op">==</span> <span class="ident">s</span>.<span class="ident">trim_right</span>().<span class="ident">chars</span>().<span class="ident">rev</span>().<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22English%20%20%22%3B%0Aassert!(Some('h')%20%3D%3D%20s.trim_right().chars().rev().next())%3B%0A%0Alet%20s%20%3D%20%22%D7%A2%D7%91%D7%A8%D7%99%D7%AA%20%20%22%3B%0Aassert!(Some('%D7%AA')%20%3D%3D%20s.trim_right().chars().rev().next())%3B%0A%7D">Run</a></pre> </div><h4 id='method.trim_matches' class="method"><span id='trim_matches.v' class='invisible'><code>pub fn <a href='#method.trim_matches' class='fnname'>trim_matches</a><'a, P>(&'a self, pat: P) -> &'a <a class="primitive" href="../primitive.str.html">str</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.DoubleEndedSearcher.html" title="trait std::str::pattern::DoubleEndedSearcher">DoubleEndedSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1674-1678' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a string slice with all prefixes and suffixes that match a pattern repeatedly removed.</p> <p>The pattern can be a <a href="primitive.char.html"><code>char</code></a> or a closure that determines if a character matches.</p> <h1 id="examples-70" class="section-header"><a href="#examples-70">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"11foo1bar11"</span>.<span class="ident">trim_matches</span>(<span class="string">'1'</span>), <span class="string">"foo1bar"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"123foo1bar123"</span>.<span class="ident">trim_matches</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>), <span class="string">"foo1bar"</span>); <span class="kw">let</span> <span class="ident">x</span>: <span class="kw-2">&</span>[_] <span class="op">=</span> <span class="kw-2">&</span>[<span class="string">'1'</span>, <span class="string">'2'</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"12foo1bar12"</span>.<span class="ident">trim_matches</span>(<span class="ident">x</span>), <span class="string">"foo1bar"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(%2211foo1bar11%22.trim_matches('1')%2C%20%22foo1bar%22)%3B%0Aassert_eq!(%22123foo1bar123%22.trim_matches(char%3A%3Ais_numeric)%2C%20%22foo1bar%22)%3B%0A%0Alet%20x%3A%20%26%5B_%5D%20%3D%20%26%5B'1'%2C%20'2'%5D%3B%0Aassert_eq!(%2212foo1bar12%22.trim_matches(x)%2C%20%22foo1bar%22)%3B%0A%7D">Run</a></pre> <p>A more complex pattern, using a closure:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"1foo1barXX"</span>.<span class="ident">trim_matches</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'1'</span> <span class="op">||</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'X'</span>), <span class="string">"foo1bar"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(%221foo1barXX%22.trim_matches(%7Cc%7C%20c%20%3D%3D%20'1'%20%7C%7C%20c%20%3D%3D%20'X')%2C%20%22foo1bar%22)%3B%0A%7D">Run</a></pre> </div><h4 id='method.trim_left_matches' class="method"><span id='trim_left_matches.v' class='invisible'><code>pub fn <a href='#method.trim_left_matches' class='fnname'>trim_left_matches</a><'a, P>(&'a self, pat: P) -> &'a <a class="primitive" href="../primitive.str.html">str</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1707-1709' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a string slice with all prefixes that match a pattern repeatedly removed.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="text-directionality-2" class="section-header"><a href="#text-directionality-2">Text directionality</a></h1> <p>A string is a sequence of bytes. 'Left' in this context means the first position of that byte string; for a language like Arabic or Hebrew which are 'right to left' rather than 'left to right', this will be the <em>right</em> side, not the left.</p> <h1 id="examples-71" class="section-header"><a href="#examples-71">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"11foo1bar11"</span>.<span class="ident">trim_left_matches</span>(<span class="string">'1'</span>), <span class="string">"foo1bar11"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"123foo1bar123"</span>.<span class="ident">trim_left_matches</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>), <span class="string">"foo1bar123"</span>); <span class="kw">let</span> <span class="ident">x</span>: <span class="kw-2">&</span>[_] <span class="op">=</span> <span class="kw-2">&</span>[<span class="string">'1'</span>, <span class="string">'2'</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"12foo1bar12"</span>.<span class="ident">trim_left_matches</span>(<span class="ident">x</span>), <span class="string">"foo1bar12"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(%2211foo1bar11%22.trim_left_matches('1')%2C%20%22foo1bar11%22)%3B%0Aassert_eq!(%22123foo1bar123%22.trim_left_matches(char%3A%3Ais_numeric)%2C%20%22foo1bar123%22)%3B%0A%0Alet%20x%3A%20%26%5B_%5D%20%3D%20%26%5B'1'%2C%20'2'%5D%3B%0Aassert_eq!(%2212foo1bar12%22.trim_left_matches(x)%2C%20%22foo1bar12%22)%3B%0A%7D">Run</a></pre> </div><h4 id='method.trim_right_matches' class="method"><span id='trim_right_matches.v' class='invisible'><code>pub fn <a href='#method.trim_right_matches' class='fnname'>trim_right_matches</a><'a, P>(&'a self, pat: P) -> &'a <a class="primitive" href="../primitive.str.html">str</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>,<br> <P as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1744-1748' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a string slice with all suffixes that match a pattern repeatedly removed.</p> <p>The pattern can be a <code>&str</code>, <a href="primitive.char.html"><code>char</code></a>, or a closure that determines if a character matches.</p> <h1 id="text-directionality-3" class="section-header"><a href="#text-directionality-3">Text directionality</a></h1> <p>A string is a sequence of bytes. 'Right' in this context means the last position of that byte string; for a language like Arabic or Hebrew which are 'right to left' rather than 'left to right', this will be the <em>left</em> side, not the right.</p> <h1 id="examples-72" class="section-header"><a href="#examples-72">Examples</a></h1> <p>Simple patterns:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"11foo1bar11"</span>.<span class="ident">trim_right_matches</span>(<span class="string">'1'</span>), <span class="string">"11foo1bar"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"123foo1bar123"</span>.<span class="ident">trim_right_matches</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>), <span class="string">"123foo1bar"</span>); <span class="kw">let</span> <span class="ident">x</span>: <span class="kw-2">&</span>[_] <span class="op">=</span> <span class="kw-2">&</span>[<span class="string">'1'</span>, <span class="string">'2'</span>]; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"12foo1bar12"</span>.<span class="ident">trim_right_matches</span>(<span class="ident">x</span>), <span class="string">"12foo1bar"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(%2211foo1bar11%22.trim_right_matches('1')%2C%20%2211foo1bar%22)%3B%0Aassert_eq!(%22123foo1bar123%22.trim_right_matches(char%3A%3Ais_numeric)%2C%20%22123foo1bar%22)%3B%0A%0Alet%20x%3A%20%26%5B_%5D%20%3D%20%26%5B'1'%2C%20'2'%5D%3B%0Aassert_eq!(%2212foo1bar12%22.trim_right_matches(x)%2C%20%2212foo1bar%22)%3B%0A%7D">Run</a></pre> <p>A more complex pattern, using a closure:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"1fooX"</span>.<span class="ident">trim_right_matches</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'1'</span> <span class="op">||</span> <span class="ident">c</span> <span class="op">==</span> <span class="string">'X'</span>), <span class="string">"1foo"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(%221fooX%22.trim_right_matches(%7Cc%7C%20c%20%3D%3D%20'1'%20%7C%7C%20c%20%3D%3D%20'X')%2C%20%221foo%22)%3B%0A%7D">Run</a></pre> </div><h4 id='method.parse' class="method"><span id='parse.v' class='invisible'><code>pub fn <a href='#method.parse' class='fnname'>parse</a><F>(&self) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><F, <F as <a class="trait" href="../../std/str/trait.FromStr.html" title="trait std::str::FromStr">FromStr</a>>::<a class="type" href="../../std/str/trait.FromStr.html#associatedtype.Err" title="type std::str::FromStr::Err">Err</a>> <span class="where fmt-newline">where<br> F: <a class="trait" href="../../std/str/trait.FromStr.html" title="trait std::str::FromStr">FromStr</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1796-1798' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Parses this string slice into another type.</p> <p>Because <code>parse</code> is so general, it can cause problems with type inference. As such, <code>parse</code> is one of the few times you'll see the syntax affectionately known as the 'turbofish': <code>::<></code>. This helps the inference algorithm understand specifically which type you're trying to parse into.</p> <p><code>parse</code> can parse any type that implements the <a href="str/trait.FromStr.html"><code>FromStr</code></a> trait.</p> <h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1> <p>Will return <a href="str/trait.FromStr.html#associatedtype.Err"><code>Err</code></a> if it's not possible to parse this string slice into the desired type.</p> <h1 id="examples-73" class="section-header"><a href="#examples-73">Examples</a></h1> <p>Basic usage</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">four</span>: <span class="ident">u32</span> <span class="op">=</span> <span class="string">"4"</span>.<span class="ident">parse</span>().<span class="ident">unwrap</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">4</span>, <span class="ident">four</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20four%3A%20u32%20%3D%20%224%22.parse().unwrap()%3B%0A%0Aassert_eq!(4%2C%20four)%3B%0A%7D">Run</a></pre> <p>Using the 'turbofish' instead of annotating <code>four</code>:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">four</span> <span class="op">=</span> <span class="string">"4"</span>.<span class="ident">parse</span>::<span class="op"><</span><span class="ident">u32</span><span class="op">></span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Ok</span>(<span class="number">4</span>), <span class="ident">four</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20four%20%3D%20%224%22.parse%3A%3A%3Cu32%3E()%3B%0A%0Aassert_eq!(Ok(4)%2C%20four)%3B%0A%7D">Run</a></pre> <p>Failing to parse:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">nope</span> <span class="op">=</span> <span class="string">"j"</span>.<span class="ident">parse</span>::<span class="op"><</span><span class="ident">u32</span><span class="op">></span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">nope</span>.<span class="ident">is_err</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20nope%20%3D%20%22j%22.parse%3A%3A%3Cu32%3E()%3B%0A%0Aassert!(nope.is_err())%3B%0A%7D">Run</a></pre> </div><h4 id='method.replace' class="method"><span id='replace.v' class='invisible'><code>pub fn <a href='#method.replace' class='fnname'>replace</a><'a, P>(&'a self, from: P, to: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#1843-1853' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Replaces all matches of a pattern with another string.</p> <p><code>replace</code> creates a new <a href="string/struct.String.html"><code>String</code></a>, and copies the data from this string slice into it. While doing so, it attempts to find matches of a pattern. If it finds any, it replaces them with the replacement string slice.</p> <h1 id="examples-74" class="section-header"><a href="#examples-74">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"this is old"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"this is new"</span>, <span class="ident">s</span>.<span class="ident">replace</span>(<span class="string">"old"</span>, <span class="string">"new"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22this%20is%20old%22%3B%0A%0Aassert_eq!(%22this%20is%20new%22%2C%20s.replace(%22old%22%2C%20%22new%22))%3B%0A%7D">Run</a></pre> <p>When the pattern doesn't match:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"this is old"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="ident">s</span>.<span class="ident">replace</span>(<span class="string">"cookie monster"</span>, <span class="string">"little lamb"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22this%20is%20old%22%3B%0Aassert_eq!(s%2C%20s.replace(%22cookie%20monster%22%2C%20%22little%20lamb%22))%3B%0A%7D">Run</a></pre> </div><h4 id='method.replacen' class="method"><span id='replacen.v' class='invisible'><code>pub fn <a href='#method.replacen' class='fnname'>replacen</a><'a, P>(&'a self, pat: P, to: &<a class="primitive" href="../primitive.str.html">str</a>, count: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> P: <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.16.0'>1.16.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1881-1892' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Replaces first N matches of a pattern with another string.</p> <p><code>replacen</code> creates a new <a href="string/struct.String.html"><code>String</code></a>, and copies the data from this string slice into it. While doing so, it attempts to find matches of a pattern. If it finds any, it replaces them with the replacement string slice at most <code>count</code> times.</p> <h1 id="examples-75" class="section-header"><a href="#examples-75">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"foo foo 123 foo"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"new new 123 foo"</span>, <span class="ident">s</span>.<span class="ident">replacen</span>(<span class="string">"foo"</span>, <span class="string">"new"</span>, <span class="number">2</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"faa fao 123 foo"</span>, <span class="ident">s</span>.<span class="ident">replacen</span>(<span class="string">'o'</span>, <span class="string">"a"</span>, <span class="number">3</span>)); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"foo foo new23 foo"</span>, <span class="ident">s</span>.<span class="ident">replacen</span>(<span class="ident">char</span>::<span class="ident">is_numeric</span>, <span class="string">"new"</span>, <span class="number">1</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22foo%20foo%20123%20foo%22%3B%0Aassert_eq!(%22new%20new%20123%20foo%22%2C%20s.replacen(%22foo%22%2C%20%22new%22%2C%202))%3B%0Aassert_eq!(%22faa%20fao%20123%20foo%22%2C%20s.replacen('o'%2C%20%22a%22%2C%203))%3B%0Aassert_eq!(%22foo%20foo%20new23%20foo%22%2C%20s.replacen(char%3A%3Ais_numeric%2C%20%22new%22%2C%201))%3B%0A%7D">Run</a></pre> <p>When the pattern doesn't match:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"this is old"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, <span class="ident">s</span>.<span class="ident">replacen</span>(<span class="string">"cookie monster"</span>, <span class="string">"little lamb"</span>, <span class="number">10</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22this%20is%20old%22%3B%0Aassert_eq!(s%2C%20s.replacen(%22cookie%20monster%22%2C%20%22little%20lamb%22%2C%2010))%3B%0A%7D">Run</a></pre> </div><h4 id='method.to_lowercase' class="method"><span id='to_lowercase.v' class='invisible'><code>pub fn <a href='#method.to_lowercase' class='fnname'>to_lowercase</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1936-1968' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns the lowercase equivalent of this string slice, as a new <a href="string/struct.String.html"><code>String</code></a>.</p> <p>'Lowercase' is defined according to the terms of the Unicode Derived Core Property <code>Lowercase</code>.</p> <p>Since some characters can expand into multiple characters when changing the case, this function returns a <a href="string/struct.String.html"><code>String</code></a> instead of modifying the parameter in-place.</p> <h1 id="examples-76" class="section-header"><a href="#examples-76">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"HELLO"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"hello"</span>, <span class="ident">s</span>.<span class="ident">to_lowercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22HELLO%22%3B%0A%0Aassert_eq!(%22hello%22%2C%20s.to_lowercase())%3B%0A%7D">Run</a></pre> <p>A tricky example, with sigma:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">sigma</span> <span class="op">=</span> <span class="string">"Σ"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"σ"</span>, <span class="ident">sigma</span>.<span class="ident">to_lowercase</span>()); <span class="comment">// but at the end of a word, it's ς, not σ:</span> <span class="kw">let</span> <span class="ident">odysseus</span> <span class="op">=</span> <span class="string">"ὈΔΥΣΣΕΎΣ"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"ὀδυσσεύς"</span>, <span class="ident">odysseus</span>.<span class="ident">to_lowercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20sigma%20%3D%20%22%CE%A3%22%3B%0A%0Aassert_eq!(%22%CF%83%22%2C%20sigma.to_lowercase())%3B%0A%0A%2F%2F%20but%20at%20the%20end%20of%20a%20word%2C%20it's%20%CF%82%2C%20not%20%CF%83%3A%0Alet%20odysseus%20%3D%20%22%E1%BD%88%CE%94%CE%A5%CE%A3%CE%A3%CE%95%CE%8E%CE%A3%22%3B%0A%0Aassert_eq!(%22%E1%BD%80%CE%B4%CF%85%CF%83%CF%83%CE%B5%CF%8D%CF%82%22%2C%20odysseus.to_lowercase())%3B%0A%7D">Run</a></pre> <p>Languages without case are not changed:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">new_year</span> <span class="op">=</span> <span class="string">"农历新年"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">new_year</span>, <span class="ident">new_year</span>.<span class="ident">to_lowercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20new_year%20%3D%20%22%E5%86%9C%E5%8E%86%E6%96%B0%E5%B9%B4%22%3B%0A%0Aassert_eq!(new_year%2C%20new_year.to_lowercase())%3B%0A%7D">Run</a></pre> </div><h4 id='method.to_uppercase' class="method"><span id='to_uppercase.v' class='invisible'><code>pub fn <a href='#method.to_uppercase' class='fnname'>to_uppercase</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/alloc/str.rs.html#1999-2003' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns the uppercase equivalent of this string slice, as a new <a href="string/struct.String.html"><code>String</code></a>.</p> <p>'Uppercase' is defined according to the terms of the Unicode Derived Core Property <code>Uppercase</code>.</p> <p>Since some characters can expand into multiple characters when changing the case, this function returns a <a href="string/struct.String.html"><code>String</code></a> instead of modifying the parameter in-place.</p> <h1 id="examples-77" class="section-header"><a href="#examples-77">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"hello"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"HELLO"</span>, <span class="ident">s</span>.<span class="ident">to_uppercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22hello%22%3B%0A%0Aassert_eq!(%22HELLO%22%2C%20s.to_uppercase())%3B%0A%7D">Run</a></pre> <p>Scripts without case are not changed:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">new_year</span> <span class="op">=</span> <span class="string">"农历新年"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">new_year</span>, <span class="ident">new_year</span>.<span class="ident">to_uppercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20new_year%20%3D%20%22%E5%86%9C%E5%8E%86%E6%96%B0%E5%B9%B4%22%3B%0A%0Aassert_eq!(new_year%2C%20new_year.to_uppercase())%3B%0A%7D">Run</a></pre> </div><h4 id='method.escape_debug' class="method"><span id='escape_debug.v' class='invisible'><code>pub fn <a href='#method.escape_debug' class='fnname'>escape_debug</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#2011-2013' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>str_escape </code><a href="https://github.com/rust-lang/rust/issues/27791">#27791</a>)</summary><p>return type may change to be an iterator</p> </details></div></div><div class='docblock'><p>Escapes each char in <code>s</code> with <a href="primitive.char.html#method.escape_debug"><code>char::escape_debug</code></a>.</p> </div><h4 id='method.escape_default' class="method"><span id='escape_default.v' class='invisible'><code>pub fn <a href='#method.escape_default' class='fnname'>escape_default</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#2021-2023' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>str_escape </code><a href="https://github.com/rust-lang/rust/issues/27791">#27791</a>)</summary><p>return type may change to be an iterator</p> </details></div></div><div class='docblock'><p>Escapes each char in <code>s</code> with <a href="primitive.char.html#method.escape_default"><code>char::escape_default</code></a>.</p> </div><h4 id='method.escape_unicode' class="method"><span id='escape_unicode.v' class='invisible'><code>pub fn <a href='#method.escape_unicode' class='fnname'>escape_unicode</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#2031-2033' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>str_escape </code><a href="https://github.com/rust-lang/rust/issues/27791">#27791</a>)</summary><p>return type may change to be an iterator</p> </details></div></div><div class='docblock'><p>Escapes each char in <code>s</code> with <a href="primitive.char.html#method.escape_unicode"><code>char::escape_unicode</code></a>.</p> </div><h4 id='method.repeat' class="method"><span id='repeat.v' class='invisible'><code>pub fn <a href='#method.repeat' class='fnname'>repeat</a>(&self, n: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.16.0'>1.16.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2068-2072' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Create a <a href="string/struct.String.html"><code>String</code></a> by repeating a string <code>n</code> times.</p> <h1 id="examples-78" class="section-header"><a href="#examples-78">Examples</a></h1> <p>Basic usage:</p> <pre class="rust rust-example-rendered"> <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"abc"</span>.<span class="ident">repeat</span>(<span class="number">4</span>), <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"abcabcabcabc"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert_eq!(%22abc%22.repeat(4)%2C%20String%3A%3Afrom(%22abcabcabcabc%22))%3B%0A%7D">Run</a></pre> </div><h4 id='method.is_ascii' class="method"><span id='is_ascii.v' class='invisible'><code>pub fn <a href='#method.is_ascii' class='fnname'>is_ascii</a>(&self) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.23.0'>1.23.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2087-2092' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Checks if all characters in this string are within the ASCII range.</p> <h1 id="examples-79" class="section-header"><a href="#examples-79">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">ascii</span> <span class="op">=</span> <span class="string">"hello!\n"</span>; <span class="kw">let</span> <span class="ident">non_ascii</span> <span class="op">=</span> <span class="string">"Grüße, Jürgen ❤"</span>; <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">ascii</span>.<span class="ident">is_ascii</span>()); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">non_ascii</span>.<span class="ident">is_ascii</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20ascii%20%3D%20%22hello!%5Cn%22%3B%0Alet%20non_ascii%20%3D%20%22Gr%C3%BC%C3%9Fe%2C%20J%C3%BCrgen%20%E2%9D%A4%22%3B%0A%0Aassert!(ascii.is_ascii())%3B%0Aassert!(!non_ascii.is_ascii())%3B%0A%7D">Run</a></pre> </div><h4 id='method.to_ascii_uppercase' class="method"><span id='to_ascii_uppercase.v' class='invisible'><code>pub fn <a href='#method.to_ascii_uppercase' class='fnname'>to_ascii_uppercase</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.23.0'>1.23.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2117-2122' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a copy of this string where each character is mapped to its ASCII upper case equivalent.</p> <p>ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters are unchanged.</p> <p>To uppercase the value in-place, use <a href="#method.make_ascii_uppercase"><code>make_ascii_uppercase</code></a>.</p> <p>To uppercase ASCII characters in addition to non-ASCII characters, use <a href="#method.to_uppercase"><code>to_uppercase</code></a>.</p> <h1 id="examples-80" class="section-header"><a href="#examples-80">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Grüße, Jürgen ❤"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"GRüßE, JüRGEN ❤"</span>, <span class="ident">s</span>.<span class="ident">to_ascii_uppercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22Gr%C3%BC%C3%9Fe%2C%20J%C3%BCrgen%20%E2%9D%A4%22%3B%0A%0Aassert_eq!(%22GR%C3%BC%C3%9FE%2C%20J%C3%BCRGEN%20%E2%9D%A4%22%2C%20s.to_ascii_uppercase())%3B%0A%7D">Run</a></pre> </div><h4 id='method.to_ascii_lowercase' class="method"><span id='to_ascii_lowercase.v' class='invisible'><code>pub fn <a href='#method.to_ascii_lowercase' class='fnname'>to_ascii_lowercase</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.23.0'>1.23.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2147-2152' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a copy of this string where each character is mapped to its ASCII lower case equivalent.</p> <p>ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters are unchanged.</p> <p>To lowercase the value in-place, use <a href="#method.make_ascii_lowercase"><code>make_ascii_lowercase</code></a>.</p> <p>To lowercase ASCII characters in addition to non-ASCII characters, use <a href="#method.to_lowercase"><code>to_lowercase</code></a>.</p> <h1 id="examples-81" class="section-header"><a href="#examples-81">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> <span class="string">"Grüße, Jürgen ❤"</span>; <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">"grüße, jürgen ❤"</span>, <span class="ident">s</span>.<span class="ident">to_ascii_lowercase</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20s%20%3D%20%22Gr%C3%BC%C3%9Fe%2C%20J%C3%BCrgen%20%E2%9D%A4%22%3B%0A%0Aassert_eq!(%22gr%C3%BC%C3%9Fe%2C%20j%C3%BCrgen%20%E2%9D%A4%22%2C%20s.to_ascii_lowercase())%3B%0A%7D">Run</a></pre> </div><h4 id='method.eq_ignore_ascii_case' class="method"><span id='eq_ignore_ascii_case.v' class='invisible'><code>pub fn <a href='#method.eq_ignore_ascii_case' class='fnname'>eq_ignore_ascii_case</a>(&self, other: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.23.0'>1.23.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2168-2170' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Checks that two strings are an ASCII case-insensitive match.</p> <p>Same as <code>to_ascii_lowercase(a) == to_ascii_lowercase(b)</code>, but without allocating and copying temporaries.</p> <h1 id="examples-82" class="section-header"><a href="#examples-82">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="macro">assert</span><span class="macro">!</span>(<span class="string">"Ferris"</span>.<span class="ident">eq_ignore_ascii_case</span>(<span class="string">"FERRIS"</span>)); <span class="macro">assert</span><span class="macro">!</span>(<span class="string">"Ferrös"</span>.<span class="ident">eq_ignore_ascii_case</span>(<span class="string">"FERRöS"</span>)); <span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="string">"Ferrös"</span>.<span class="ident">eq_ignore_ascii_case</span>(<span class="string">"FERRÖS"</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Aassert!(%22Ferris%22.eq_ignore_ascii_case(%22FERRIS%22))%3B%0Aassert!(%22Ferr%C3%B6s%22.eq_ignore_ascii_case(%22FERR%C3%B6S%22))%3B%0Aassert!(!%22Ferr%C3%B6s%22.eq_ignore_ascii_case(%22FERR%C3%96S%22))%3B%0A%7D">Run</a></pre> </div><h4 id='method.make_ascii_uppercase' class="method"><span id='make_ascii_uppercase.v' class='invisible'><code>pub fn <a href='#method.make_ascii_uppercase' class='fnname'>make_ascii_uppercase</a>(&mut self)</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.23.0'>1.23.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2182-2185' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts this string to its ASCII upper case equivalent in-place.</p> <p>ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters are unchanged.</p> <p>To return a new uppercased value without modifying the existing one, use <a href="#method.to_ascii_uppercase"><code>to_ascii_uppercase</code></a>.</p> </div><h4 id='method.make_ascii_lowercase' class="method"><span id='make_ascii_lowercase.v' class='invisible'><code>pub fn <a href='#method.make_ascii_lowercase' class='fnname'>make_ascii_lowercase</a>(&mut self)</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.23.0'>1.23.0</div><a class='srclink' href='../../src/alloc/str.rs.html#2197-2200' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts this string to its ASCII lower case equivalent in-place.</p> <p>ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters are unchanged.</p> <p>To return a new lowercased value without modifying the existing one, use <a href="#method.to_ascii_lowercase"><code>to_ascii_lowercase</code></a>.</p> </div></div> <h2 id='implementations' class='small-section-header'> Trait Implementations<a href='#implementations' class='anchor'></a> </h2> <h3 id='impl-Pattern%3C%27a%3E' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a> for &'b <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Pattern%3C%27a%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1692-1708' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p>A convenience impl that delegates to the impl for <code>&str</code></p> </div><div class='impl-items'><h4 id='associatedtype.Searcher' class="type"><span id='Searcher.t' class='invisible'><code>type <a href='../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher' class="type">Searcher</a> = <&'b <a class="primitive" href="../primitive.str.html">str</a> as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a></code></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>pattern </code><a href="https://github.com/rust-lang/rust/issues/27721">#27721</a>)</summary><p>API not fully fleshed out and ready to be stabilized</p> </details></div></div><div class='docblock'><p>Associated searcher for this pattern</p> </div><h4 id='method.into_searcher' class="method"><span id='into_searcher.v' class='invisible'><code>fn <a href='../../std/str/pattern/trait.Pattern.html#tymethod.into_searcher' class='fnname'>into_searcher</a>(self, haystack: &'a <a class="primitive" href="../primitive.str.html">str</a>) -> <&'b <a class="primitive" href="../primitive.str.html">str</a> as <a class="trait" href="../../std/str/pattern/trait.Pattern.html" title="trait std::str::pattern::Pattern">Pattern</a><'a>>::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1695-1697' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>pattern </code><a href="https://github.com/rust-lang/rust/issues/27721">#27721</a>)</summary><p>API not fully fleshed out and ready to be stabilized</p> </details></div></div><div class='docblock'><p>Constructs the associated searcher from <code>self</code> and the <code>haystack</code> to search in. <a href="../../std/str/pattern/trait.Pattern.html#tymethod.into_searcher">Read more</a></p> </div><h4 id='method.is_contained_in' class="method"><span id='is_contained_in.v' class='invisible'><code>fn <a href='../../std/str/pattern/trait.Pattern.html#method.is_contained_in' class='fnname'>is_contained_in</a>(self, haystack: &'a <a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1700-1702' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>pattern </code><a href="https://github.com/rust-lang/rust/issues/27721">#27721</a>)</summary><p>API not fully fleshed out and ready to be stabilized</p> </details></div></div><div class='docblock'><p>Checks whether the pattern matches anywhere in the haystack</p> </div><h4 id='method.is_prefix_of' class="method"><span id='is_prefix_of.v' class='invisible'><code>fn <a href='../../std/str/pattern/trait.Pattern.html#method.is_prefix_of' class='fnname'>is_prefix_of</a>(self, haystack: &'a <a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1705-1707' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>pattern </code><a href="https://github.com/rust-lang/rust/issues/27721">#27721</a>)</summary><p>API not fully fleshed out and ready to be stabilized</p> </details></div></div><div class='docblock'><p>Checks whether the pattern matches at the front of the haystack</p> </div><h4 id='method.is_suffix_of' class="method"><span id='is_suffix_of.v' class='invisible'><code>fn <a href='../../std/str/pattern/trait.Pattern.html#method.is_suffix_of' class='fnname'>is_suffix_of</a>(self, haystack: &'a <a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> Self::<a class="type" href="../../std/str/pattern/trait.Pattern.html#associatedtype.Searcher" title="type std::str::pattern::Pattern::Searcher">Searcher</a>: <a class="trait" href="../../std/str/pattern/trait.ReverseSearcher.html" title="trait std::str::pattern::ReverseSearcher">ReverseSearcher</a><'a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/str/pattern.rs.html#63-70' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>pattern </code><a href="https://github.com/rust-lang/rust/issues/27721">#27721</a>)</summary><p>API not fully fleshed out and ready to be stabilized</p> </details></div></div><div class='docblock'><p>Checks whether the pattern matches at the back of the haystack</p> </div></div><h3 id='impl-From%3C%26%27a%20str%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><&'a <a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-From%3C%26%27a%20str%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2102-2106' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from' class="method"><span id='from.v' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: &'a <a class="primitive" href="../primitive.str.html">str</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2103-2105' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CCow%3C%27a%2C%20str%3E%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-From%3CCow%3C%27a%2C%20str%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.14.0'>1.14.0</div><a class='srclink' href='../../src/alloc/string.rs.html#2125-2129' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-1' class="method"><span id='from.v-1' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2126-2128' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="primitive" href="../primitive.str.html">str</a>></code><a href='#impl-From%3CString%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.20.0'>1.20.0</div><a class='srclink' href='../../src/alloc/string.rs.html#2118-2122' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-2' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></h3><code class="content"><span class="where fmt-newline">impl<I> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <I as <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a>>::<a class="type" href="../../std/iter/trait.Iterator.html#associatedtype.Item" title="type std::iter::Iterator::Item">Item</a>;</span><span class="where fmt-newline">impl<R: <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><R></span><span class="where fmt-newline">impl<W: <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><W></span></code></div></div><span id='from.v-2' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="primitive" href="../primitive.str.html">str</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2119-2121' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E-1' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/rc/struct.Rc.html" title="struct std::rc::Rc">Rc</a><<a class="primitive" href="../primitive.str.html">str</a>></code><a href='#impl-From%3CString%3E-1' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.21.0'>1.21.0</div><a class='srclink' href='../../src/alloc/rc.rs.html#1108-1113' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-3' class="method"><span id='from.v-3' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(v: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/rc/struct.Rc.html" title="struct std::rc::Rc">Rc</a><<a class="primitive" href="../primitive.str.html">str</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/rc.rs.html#1110-1112' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CBox%3Cstr%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="primitive" href="../primitive.str.html">str</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-From%3CBox%3Cstr%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.18.0'>1.18.0</div><a class='srclink' href='../../src/alloc/string.rs.html#2111-2115' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-4' class="method"><span id='from.v-4' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="primitive" href="../primitive.str.html">str</a>>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2112-2114' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E-2' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></code><a href='#impl-From%3CString%3E-2' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.14.0'>1.14.0</div><a class='srclink' href='../../src/alloc/string.rs.html#2169-2173' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-5' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></h3><code class="content"><span class="where fmt-newline">impl <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></span></code></div></div><span id='from.v-5' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(string: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><<a class="primitive" href="../primitive.u8.html">u8</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2170-2172' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E-3' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/sync/struct.Arc.html" title="struct std::sync::Arc">Arc</a><<a class="primitive" href="../primitive.str.html">str</a>></code><a href='#impl-From%3CString%3E-3' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.21.0'>1.21.0</div><a class='srclink' href='../../src/alloc/arc.rs.html#1386-1391' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-6' class="method"><span id='from.v-6' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(v: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/sync/struct.Arc.html" title="struct std::sync::Arc">Arc</a><<a class="primitive" href="../primitive.str.html">str</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/arc.rs.html#1388-1390' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E-4' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>></code><a href='#impl-From%3CString%3E-4' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2140-2145' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-7' class="method"><span id='from.v-7' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2142-2144' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-IndexMut%3CRangeFrom%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.IndexMut.html" title="trait std::ops::IndexMut">IndexMut</a><<a class="struct" href="../../std/ops/struct.RangeFrom.html" title="struct std::ops::RangeFrom">RangeFrom</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-IndexMut%3CRangeFrom%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.3.0'>1.3.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1913-1918' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.index_mut' class="method"><span id='index_mut.v' class='invisible'><code>fn <a href='../../std/ops/trait.IndexMut.html#tymethod.index_mut' class='fnname'>index_mut</a>(&mut self, index: <a class="struct" href="../../std/ops/struct.RangeFrom.html" title="struct std::ops::RangeFrom">RangeFrom</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1915-1917' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the mutable indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-IndexMut%3CRangeToInclusive%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.IndexMut.html" title="trait std::ops::IndexMut">IndexMut</a><<a class="struct" href="../../std/ops/struct.RangeToInclusive.html" title="struct std::ops::RangeToInclusive">RangeToInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-IndexMut%3CRangeToInclusive%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1934-1939' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.index_mut-1' class="method"><span id='index_mut.v-1' class='invisible'><code>fn <a href='../../std/ops/trait.IndexMut.html#tymethod.index_mut' class='fnname'>index_mut</a>(&mut self, index: <a class="struct" href="../../std/ops/struct.RangeToInclusive.html" title="struct std::ops::RangeToInclusive">RangeToInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1936-1938' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the mutable indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-IndexMut%3CRangeTo%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.IndexMut.html" title="trait std::ops::IndexMut">IndexMut</a><<a class="struct" href="../../std/ops/struct.RangeTo.html" title="struct std::ops::RangeTo">RangeTo</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-IndexMut%3CRangeTo%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.3.0'>1.3.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1906-1911' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.index_mut-2' class="method"><span id='index_mut.v-2' class='invisible'><code>fn <a href='../../std/ops/trait.IndexMut.html#tymethod.index_mut' class='fnname'>index_mut</a>(&mut self, index: <a class="struct" href="../../std/ops/struct.RangeTo.html" title="struct std::ops::RangeTo">RangeTo</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1908-1910' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the mutable indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-IndexMut%3CRangeInclusive%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.IndexMut.html" title="trait std::ops::IndexMut">IndexMut</a><<a class="struct" href="../../std/ops/struct.RangeInclusive.html" title="struct std::ops::RangeInclusive">RangeInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-IndexMut%3CRangeInclusive%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1927-1932' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.index_mut-3' class="method"><span id='index_mut.v-3' class='invisible'><code>fn <a href='../../std/ops/trait.IndexMut.html#tymethod.index_mut' class='fnname'>index_mut</a>(&mut self, index: <a class="struct" href="../../std/ops/struct.RangeInclusive.html" title="struct std::ops::RangeInclusive">RangeInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1929-1931' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the mutable indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-IndexMut%3CRange%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.IndexMut.html" title="trait std::ops::IndexMut">IndexMut</a><<a class="struct" href="../../std/ops/struct.Range.html" title="struct std::ops::Range">Range</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-IndexMut%3CRange%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.3.0'>1.3.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1899-1904' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.index_mut-4' class="method"><span id='index_mut.v-4' class='invisible'><code>fn <a href='../../std/ops/trait.IndexMut.html#tymethod.index_mut' class='fnname'>index_mut</a>(&mut self, index: <a class="struct" href="../../std/ops/struct.Range.html" title="struct std::ops::Range">Range</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1901-1903' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the mutable indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-IndexMut%3CRangeFull%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.IndexMut.html" title="trait std::ops::IndexMut">IndexMut</a><<a class="struct" href="../../std/ops/struct.RangeFull.html" title="struct std::ops::RangeFull">RangeFull</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-IndexMut%3CRangeFull%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.3.0'>1.3.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1920-1925' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.index_mut-5' class="method"><span id='index_mut.v-5' class='invisible'><code>fn <a href='../../std/ops/trait.IndexMut.html#tymethod.index_mut' class='fnname'>index_mut</a>(&mut self, _index: <a class="struct" href="../../std/ops/struct.RangeFull.html" title="struct std::ops::RangeFull">RangeFull</a>) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1922-1924' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the mutable indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-FromStr' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/str/trait.FromStr.html" title="trait std::str::FromStr">FromStr</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-FromStr' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1976-1982' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Err' class="type"><span id='Err.t' class='invisible'><code>type <a href='../../std/str/trait.FromStr.html#associatedtype.Err' class="type">Err</a> = <a class="enum" href="../../std/string/enum.ParseError.html" title="enum std::string::ParseError">ParseError</a></code></span></h4> <div class='docblock'><p>The associated error which can be returned from parsing.</p> </div><h4 id='method.from_str' class="method"><span id='from_str.v' class='invisible'><code>fn <a href='../../std/str/trait.FromStr.html#tymethod.from_str' class='fnname'>from_str</a>(s: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>, <a class="enum" href="../../std/string/enum.ParseError.html" title="enum std::string::ParseError">ParseError</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1979-1981' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Parses a string <code>s</code> to return a value of this type. <a href="../../std/str/trait.FromStr.html#tymethod.from_str">Read more</a></p> </div></div><h3 id='impl-ToString' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/string/trait.ToString.html" title="trait std::string::ToString">ToString</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-ToString' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.17.0'>1.17.0</div><a class='srclink' href='../../src/alloc/string.rs.html#2078-2083' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.to_string' class="method"><span id='to_string.v' class='invisible'><code>fn <a href='../../std/string/trait.ToString.html#tymethod.to_string' class='fnname'>to_string</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2080-2082' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts the given value to a <code>String</code>. <a href="../../std/string/trait.ToString.html#tymethod.to_string">Read more</a></p> </div></div><h3 id='impl-Borrow%3Cstr%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/borrow/trait.Borrow.html" title="trait std::borrow::Borrow">Borrow</a><<a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Borrow%3Cstr%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#178-183' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.borrow' class="method"><span id='borrow.v' class='invisible'><code>fn <a href='../../std/borrow/trait.Borrow.html#tymethod.borrow' class='fnname'>borrow</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/str.rs.html#180-182' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Immutably borrows from an owned value. <a href="../../std/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></p> </div></div><h3 id='impl-Ord' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Ord' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.cmp' class="method"><span id='cmp.v' class='invisible'><code>fn <a href='../../std/cmp/trait.Ord.html#tymethod.cmp' class='fnname'>cmp</a>(&self, __arg_0: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="enum" href="../../std/cmp/enum.Ordering.html" title="enum std::cmp::Ordering">Ordering</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method returns an <code>Ordering</code> between <code>self</code> and <code>other</code>. <a href="../../std/cmp/trait.Ord.html#tymethod.cmp">Read more</a></p> </div><h4 id='method.max' class="method"><span id='max.v' class='invisible'><code>fn <a href='../../std/cmp/trait.Ord.html#method.max' class='fnname'>max</a>(self, other: Self) -> Self</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.21.0'>1.21.0</div><a class='srclink' href='../../src/core/cmp.rs.html#460-463' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Compares and returns the maximum of two values. <a href="../../std/cmp/trait.Ord.html#method.max">Read more</a></p> </div><h4 id='method.min' class="method"><span id='min.v' class='invisible'><code>fn <a href='../../std/cmp/trait.Ord.html#method.min' class='fnname'>min</a>(self, other: Self) -> Self</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.21.0'>1.21.0</div><a class='srclink' href='../../src/core/cmp.rs.html#476-479' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Compares and returns the minimum of two values. <a href="../../std/cmp/trait.Ord.html#method.min">Read more</a></p> </div></div><h3 id='impl-Clone' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/clone/trait.Clone.html" title="trait std::clone::Clone">Clone</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Clone' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1587-1595' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.clone' class="method"><span id='clone.v' class='invisible'><code>fn <a href='../../std/clone/trait.Clone.html#tymethod.clone' class='fnname'>clone</a>(&self) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1588-1590' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Returns a copy of the value. <a href="../../std/clone/trait.Clone.html#tymethod.clone">Read more</a></p> </div><h4 id='method.clone_from' class="method"><span id='clone_from.v' class='invisible'><code>fn <a href='../../std/clone/trait.Clone.html#method.clone_from' class='fnname'>clone_from</a>(&mut self, source: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1592-1594' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs copy-assignment from <code>source</code>. <a href="../../std/clone/trait.Clone.html#method.clone_from">Read more</a></p> </div></div><h3 id='impl-DerefMut' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.DerefMut.html" title="trait std::ops::DerefMut">DerefMut</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-DerefMut' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.3.0'>1.3.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1952-1957' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.deref_mut' class="method"><span id='deref_mut.v' class='invisible'><code>fn <a href='../../std/ops/trait.DerefMut.html#tymethod.deref_mut' class='fnname'>deref_mut</a>(&mut self) -> &mut <a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1954-1956' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Mutably dereferences the value.</p> </div></div><h3 id='impl-Extend%3CCow%3C%27a%2C%20str%3E%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><<a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Extend%3CCow%3C%27a%2C%20str%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.19.0'>1.19.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1680-1686' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.extend' class="method"><span id='extend.v' class='invisible'><code>fn <a href='../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1681-1685' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-Extend%3C%26%27a%20str%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><&'a <a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Extend%3C%26%27a%20str%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1662-1668' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.extend-1' class="method"><span id='extend.v-1' class='invisible'><code>fn <a href='../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = &'a <a class="primitive" href="../primitive.str.html">str</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1663-1667' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-Extend%3C%26%27a%20char%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><&'a <a class="primitive" href="../primitive.char.html">char</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Extend%3C%26%27a%20char%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1655-1659' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.extend-2' class="method"><span id='extend.v-2' class='invisible'><code>fn <a href='../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = &'a <a class="primitive" href="../primitive.char.html">char</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1656-1658' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-Extend%3CString%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Extend%3CString%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.4.0'>1.4.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1671-1677' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.extend-3' class="method"><span id='extend.v-3' class='invisible'><code>fn <a href='../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1672-1676' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-Extend%3Cchar%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/iter/trait.Extend.html" title="trait std::iter::Extend">Extend</a><<a class="primitive" href="../primitive.char.html">char</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Extend%3Cchar%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1643-1652' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.extend-4' class="method"><span id='extend.v-4' class='invisible'><code>fn <a href='../../std/iter/trait.Extend.html#tymethod.extend' class='fnname'>extend</a><I>(&mut self, iter: I) <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="primitive" href="../primitive.char.html">char</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1644-1651' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extends a collection with the contents of an iterator. <a href="../../std/iter/trait.Extend.html#tymethod.extend">Read more</a></p> </div></div><h3 id='impl-Hash' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/hash/trait.Hash.html" title="trait std::hash::Hash">Hash</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Hash' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1775-1780' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.hash' class="method"><span id='hash.v' class='invisible'><code>fn <a href='../../std/hash/trait.Hash.html#tymethod.hash' class='fnname'>hash</a><H>(&self, hasher: <a class="primitive" href="../primitive.reference.html">&mut </a>H) <span class="where fmt-newline">where<br> H: <a class="trait" href="../../std/hash/trait.Hasher.html" title="trait std::hash::Hasher">Hasher</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1777-1779' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Feeds this value into the given [<code>Hasher</code>]. <a href="../../std/hash/trait.Hash.html#tymethod.hash">Read more</a></p> </div><h4 id='method.hash_slice' class="method"><span id='hash_slice.v' class='invisible'><code>fn <a href='../../std/hash/trait.Hash.html#method.hash_slice' class='fnname'>hash_slice</a><H>(data: <a class="primitive" href="../primitive.slice.html">&[Self]</a>, state: <a class="primitive" href="../primitive.reference.html">&mut </a>H) <span class="where fmt-newline">where<br> H: <a class="trait" href="../../std/hash/trait.Hasher.html" title="trait std::hash::Hasher">Hasher</a>, </span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.3.0'>1.3.0</div><a class='srclink' href='../../src/core/hash/mod.rs.html#202-208' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Feeds a slice of this type into the given [<code>Hasher</code>]. <a href="../../std/hash/trait.Hash.html#method.hash_slice">Read more</a></p> </div></div><h3 id='impl-Index%3CRangeTo%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Index.html" title="trait std::ops::Index">Index</a><<a class="struct" href="../../std/ops/struct.RangeTo.html" title="struct std::ops::RangeTo">RangeTo</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Index%3CRangeTo%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1853-1860' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Output' class="type"><span id='Output.t' class='invisible'><code>type <a href='../../std/ops/trait.Index.html#associatedtype.Output' class="type">Output</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The returned type after indexing.</p> </div><h4 id='method.index' class="method"><span id='index.v' class='invisible'><code>fn <a href='../../std/ops/trait.Index.html#tymethod.index' class='fnname'>index</a>(&self, index: <a class="struct" href="../../std/ops/struct.RangeTo.html" title="struct std::ops::RangeTo">RangeTo</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1857-1859' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-Index%3CRangeToInclusive%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Index.html" title="trait std::ops::Index">Index</a><<a class="struct" href="../../std/ops/struct.RangeToInclusive.html" title="struct std::ops::RangeToInclusive">RangeToInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Index%3CRangeToInclusive%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1889-1896' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Output-1' class="type"><span id='Output.t-1' class='invisible'><code>type <a href='../../std/ops/trait.Index.html#associatedtype.Output' class="type">Output</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The returned type after indexing.</p> </div><h4 id='method.index-1' class="method"><span id='index.v-1' class='invisible'><code>fn <a href='../../std/ops/trait.Index.html#tymethod.index' class='fnname'>index</a>(&self, index: <a class="struct" href="../../std/ops/struct.RangeToInclusive.html" title="struct std::ops::RangeToInclusive">RangeToInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1893-1895' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-Index%3CRangeFull%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Index.html" title="trait std::ops::Index">Index</a><<a class="struct" href="../../std/ops/struct.RangeFull.html" title="struct std::ops::RangeFull">RangeFull</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Index%3CRangeFull%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1871-1878' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Output-2' class="type"><span id='Output.t-2' class='invisible'><code>type <a href='../../std/ops/trait.Index.html#associatedtype.Output' class="type">Output</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The returned type after indexing.</p> </div><h4 id='method.index-2' class="method"><span id='index.v-2' class='invisible'><code>fn <a href='../../std/ops/trait.Index.html#tymethod.index' class='fnname'>index</a>(&self, _index: <a class="struct" href="../../std/ops/struct.RangeFull.html" title="struct std::ops::RangeFull">RangeFull</a>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1875-1877' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-Index%3CRangeInclusive%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Index.html" title="trait std::ops::Index">Index</a><<a class="struct" href="../../std/ops/struct.RangeInclusive.html" title="struct std::ops::RangeInclusive">RangeInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Index%3CRangeInclusive%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1880-1887' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Output-3' class="type"><span id='Output.t-3' class='invisible'><code>type <a href='../../std/ops/trait.Index.html#associatedtype.Output' class="type">Output</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The returned type after indexing.</p> </div><h4 id='method.index-3' class="method"><span id='index.v-3' class='invisible'><code>fn <a href='../../std/ops/trait.Index.html#tymethod.index' class='fnname'>index</a>(&self, index: <a class="struct" href="../../std/ops/struct.RangeInclusive.html" title="struct std::ops::RangeInclusive">RangeInclusive</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1884-1886' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-Index%3CRange%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Index.html" title="trait std::ops::Index">Index</a><<a class="struct" href="../../std/ops/struct.Range.html" title="struct std::ops::Range">Range</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Index%3CRange%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1844-1851' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Output-4' class="type"><span id='Output.t-4' class='invisible'><code>type <a href='../../std/ops/trait.Index.html#associatedtype.Output' class="type">Output</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The returned type after indexing.</p> </div><h4 id='method.index-4' class="method"><span id='index.v-4' class='invisible'><code>fn <a href='../../std/ops/trait.Index.html#tymethod.index' class='fnname'>index</a>(&self, index: <a class="struct" href="../../std/ops/struct.Range.html" title="struct std::ops::Range">Range</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1848-1850' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-Index%3CRangeFrom%3Cusize%3E%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Index.html" title="trait std::ops::Index">Index</a><<a class="struct" href="../../std/ops/struct.RangeFrom.html" title="struct std::ops::RangeFrom">RangeFrom</a><<a class="primitive" href="../primitive.usize.html">usize</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Index%3CRangeFrom%3Cusize%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1862-1869' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Output-5' class="type"><span id='Output.t-5' class='invisible'><code>type <a href='../../std/ops/trait.Index.html#associatedtype.Output' class="type">Output</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The returned type after indexing.</p> </div><h4 id='method.index-5' class="method"><span id='index.v-5' class='invisible'><code>fn <a href='../../std/ops/trait.Index.html#tymethod.index' class='fnname'>index</a>(&self, index: <a class="struct" href="../../std/ops/struct.RangeFrom.html" title="struct std::ops::RangeFrom">RangeFrom</a><<a class="primitive" href="../primitive.usize.html">usize</a>>) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1866-1868' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the indexing (<code>container[index]</code>) operation.</p> </div></div><h3 id='impl-PartialEq%3Cstr%3E' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><<a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-PartialEq%3Cstr%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1725-1730' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq' class="method"><span id='eq.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1727' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne' class="method"><span id='ne.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1729' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-PartialEq%3C%26%27a%20str%3E' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><&'a <a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-PartialEq%3C%26%27a%20str%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1725-1730' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq-1' class="method"><span id='eq.v-1' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &&'a <a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1727' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne-1' class="method"><span id='ne.v-1' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &&'a <a class="primitive" href="../primitive.str.html">str</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1729' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-PartialEq%3CString%3E' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>></code><a href='#impl-PartialEq%3CString%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1725-1730' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq-2' class="method"><span id='eq.v-2' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1727' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne-2' class="method"><span id='ne.v-2' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1729' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-PartialEq%3CCow%3C%27a%2C%20str%3E%3E' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><<a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-PartialEq%3CCow%3C%27a%2C%20str%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1733-1738' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq-3' class="method"><span id='eq.v-3' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &<a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1735' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne-3' class="method"><span id='ne.v-3' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &<a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1737' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-PartialEq%3CString%3E-1' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="primitive" href="../primitive.str.html">str</a></code><a href='#impl-PartialEq%3CString%3E-1' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1733-1738' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq-4' class="method"><span id='eq.v-4' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1735' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne-4' class="method"><span id='ne.v-4' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1737' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-PartialEq%3CString%3E-2' class='impl'><span class='in-band'><code>impl<'a, 'b> <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for &'a <a class="primitive" href="../primitive.str.html">str</a></code><a href='#impl-PartialEq%3CString%3E-2' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1733-1738' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq-5' class="method"><span id='eq.v-5' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1735' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne-5' class="method"><span id='ne.v-5' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1737' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-PartialEq%3CString%3E-3' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/cmp/trait.PartialEq.html" title="trait std::cmp::PartialEq">PartialEq</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-PartialEq%3CString%3E-3' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1711-1720' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.eq-6' class="method"><span id='eq.v-6' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#tymethod.eq' class='fnname'>eq</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1713-1715' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>self</code> and <code>other</code> values to be equal, and is used by <code>==</code>. <a href="../../std/cmp/trait.PartialEq.html#tymethod.eq">Read more</a></p> </div><h4 id='method.ne-6' class="method"><span id='ne.v-6' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialEq.html#method.ne' class='fnname'>ne</a>(&self, other: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1717-1719' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests for <code>!=</code>.</p> </div></div><h3 id='impl-Add%3C%26%27a%20str%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/ops/trait.Add.html" title="trait std::ops::Add">Add</a><&'a <a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Add%3C%26%27a%20str%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1820-1828' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p>Implements the <code>+</code> operator for concatenating two strings.</p> <p>This consumes the <code>String</code> on the left-hand side and re-uses its buffer (growing it if necessary). This is done to avoid allocating a new <code>String</code> and copying the entire contents on every operation, which would lead to <code>O(n^2)</code> running time when building an <code>n</code>-byte string by repeated concatenation.</p> <p>The string on the right-hand side is only borrowed; its contents are copied into the returned <code>String</code>.</p> <h1 id="examples-83" class="section-header"><a href="#examples-83">Examples</a></h1> <p>Concatenating two <code>String</code>s takes the first by value and borrows the second:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">" world"</span>); <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">a</span> <span class="op">+</span> <span class="kw-2">&</span><span class="ident">b</span>; <span class="comment">// `a` is moved and can no longer be used here.</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20a%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0Alet%20b%20%3D%20String%3A%3Afrom(%22%20world%22)%3B%0Alet%20c%20%3D%20a%20%2B%20%26b%3B%0A%2F%2F%20%60a%60%20is%20moved%20and%20can%20no%20longer%20be%20used%20here.%0A%7D">Run</a></pre> <p>If you want to keep using the first <code>String</code>, you can clone it and append to the clone instead:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"hello"</span>); <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">" world"</span>); <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">a</span>.<span class="ident">clone</span>() <span class="op">+</span> <span class="kw-2">&</span><span class="ident">b</span>; <span class="comment">// `a` is still valid here.</span><a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20a%20%3D%20String%3A%3Afrom(%22hello%22)%3B%0Alet%20b%20%3D%20String%3A%3Afrom(%22%20world%22)%3B%0Alet%20c%20%3D%20a.clone()%20%2B%20%26b%3B%0A%2F%2F%20%60a%60%20is%20still%20valid%20here.%0A%7D">Run</a></pre> <p>Concatenating <code>&str</code> slices can be done by converting the first to a <code>String</code>:</p> <pre class="rust rust-example-rendered"> <span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="string">"hello"</span>; <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="string">" world"</span>; <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">a</span>.<span class="ident">to_string</span>() <span class="op">+</span> <span class="ident">b</span>;<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20a%20%3D%20%22hello%22%3B%0Alet%20b%20%3D%20%22%20world%22%3B%0Alet%20c%20%3D%20a.to_string()%20%2B%20b%3B%0A%7D">Run</a></pre> </div><div class='impl-items'><h4 id='associatedtype.Output-6' class="type"><span id='Output.t-6' class='invisible'><code>type <a href='../../std/ops/trait.Add.html#associatedtype.Output' class="type">Output</a> = <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span></h4> <div class='docblock'><p>The resulting type after applying the <code>+</code> operator.</p> </div><h4 id='method.add' class="method"><span id='add.v' class='invisible'><code>fn <a href='../../std/ops/trait.Add.html#tymethod.add' class='fnname'>add</a>(self, other: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1824-1827' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the <code>+</code> operation.</p> </div></div><h3 id='impl-Eq' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/cmp/trait.Eq.html" title="trait std::cmp::Eq">Eq</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Eq' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h3> <div class='impl-items'></div><h3 id='impl-Display' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/fmt/trait.Display.html" title="trait std::fmt::Display">Display</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Display' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1759-1764' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.fmt' class="method"><span id='fmt.v' class='invisible'><code>fn <a href='../../std/fmt/trait.Display.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class="struct" href="../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../primitive.unit.html">()</a>, <a class="struct" href="../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1761-1763' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Formats the value using the given formatter. <a href="../../std/fmt/trait.Display.html#tymethod.fmt">Read more</a></p> </div></div><h3 id='impl-AddAssign%3C%26%27a%20str%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/ops/trait.AddAssign.html" title="trait std::ops::AddAssign">AddAssign</a><&'a <a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-AddAssign%3C%26%27a%20str%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.12.0'>1.12.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1836-1841' title='goto source code'>[src]</a></span></h3> <div class='docblock'><p>Implements the <code>+=</code> operator for appending to a <code>String</code>.</p> <p>This has the same behavior as the <a href="struct.String.html#method.push_str"><code>push_str</code></a> method.</p> </div><div class='impl-items'><h4 id='method.add_assign' class="method"><span id='add_assign.v' class='invisible'><code>fn <a href='../../std/ops/trait.AddAssign.html#tymethod.add_assign' class='fnname'>add_assign</a>(&mut self, other: &<a class="primitive" href="../primitive.str.html">str</a>)</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1838-1840' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the <code>+=</code> operation.</p> </div></div><h3 id='impl-AsRef%3C%5Bu8%5D%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.AsRef.html" title="trait std::convert::AsRef">AsRef</a><<a class="primitive" href="../primitive.slice.html">[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-AsRef%3C%5Bu8%5D%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2094-2099' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.as_ref' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></h3><code class="content"><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="primitive" href="../primitive.slice.html">&'a [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span><span class="where fmt-newline">impl<'a> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="primitive" href="../primitive.slice.html">&'a mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></span></code></div></div><span id='as_ref.v' class='invisible'><code>fn <a href='../../std/convert/trait.AsRef.html#tymethod.as_ref' class='fnname'>as_ref</a>(&self) -> <a class="primitive" href="../primitive.slice.html">&[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2096-2098' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-AsRef%3Cstr%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.AsRef.html" title="trait std::convert::AsRef">AsRef</a><<a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-AsRef%3Cstr%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2086-2091' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.as_ref-1' class="method"><span id='as_ref.v-1' class='invisible'><code>fn <a href='../../std/convert/trait.AsRef.html#tymethod.as_ref' class='fnname'>as_ref</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2088-2090' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-Default' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/default/trait.Default.html" title="trait std::default::Default">Default</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Default' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1750-1756' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.default' class="method"><span id='default.v' class='invisible'><code>fn <a href='../../std/default/trait.Default.html#tymethod.default' class='fnname'>default</a>() -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1753-1755' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates an empty <code>String</code>.</p> </div></div><h3 id='impl-FromIterator%3CCow%3C%27a%2C%20str%3E%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><<a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-FromIterator%3CCow%3C%27a%2C%20str%3E%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.19.0'>1.19.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1634-1640' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from_iter' class="method"><span id='from_iter.v' class='invisible'><code>fn <a href='../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(iter: I) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1635-1639' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a value from an iterator. <a href="../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-FromIterator%3C%26%27a%20str%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><&'a <a class="primitive" href="../primitive.str.html">str</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-FromIterator%3C%26%27a%20str%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1616-1622' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from_iter-1' class="method"><span id='from_iter.v-1' class='invisible'><code>fn <a href='../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(iter: I) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = &'a <a class="primitive" href="../primitive.str.html">str</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1617-1621' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a value from an iterator. <a href="../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-FromIterator%3C%26%27a%20char%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><&'a <a class="primitive" href="../primitive.char.html">char</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-FromIterator%3C%26%27a%20char%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.17.0'>1.17.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1607-1613' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from_iter-2' class="method"><span id='from_iter.v-2' class='invisible'><code>fn <a href='../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(iter: I) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = &'a <a class="primitive" href="../primitive.char.html">char</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1608-1612' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a value from an iterator. <a href="../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-FromIterator%3Cchar%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><<a class="primitive" href="../primitive.char.html">char</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-FromIterator%3Cchar%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1598-1604' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from_iter-3' class="method"><span id='from_iter.v-3' class='invisible'><code>fn <a href='../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(iter: I) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="primitive" href="../primitive.char.html">char</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1599-1603' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a value from an iterator. <a href="../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-FromIterator%3CString%3E' class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>></code><a href='#impl-FromIterator%3CString%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.12.0'>1.12.0</div><a class='srclink' href='../../src/alloc/string.rs.html#2162-2166' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from_iter-4' class="method"><span id='from_iter.v-4' class='invisible'><code>fn <a href='../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(it: I) -> <a class="enum" href="../../std/borrow/enum.Cow.html" title="enum std::borrow::Cow">Cow</a><'a, <a class="primitive" href="../primitive.str.html">str</a>> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2163-2165' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a value from an iterator. <a href="../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-FromIterator%3CString%3E-1' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/iter/trait.FromIterator.html" title="trait std::iter::FromIterator">FromIterator</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-FromIterator%3CString%3E-1' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.4.0'>1.4.0</div><a class='srclink' href='../../src/alloc/string.rs.html#1625-1631' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from_iter-5' class="method"><span id='from_iter.v-5' class='invisible'><code>fn <a href='../../std/iter/trait.FromIterator.html#tymethod.from_iter' class='fnname'>from_iter</a><I>(iter: I) -> <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.IntoIterator.html" title="trait std::iter::IntoIterator">IntoIterator</a><Item = <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>>, </span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1626-1630' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a value from an iterator. <a href="../../std/iter/trait.FromIterator.html#tymethod.from_iter">Read more</a></p> </div></div><h3 id='impl-Debug' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Debug' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1767-1772' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.fmt-1' class="method"><span id='fmt.v-1' class='invisible'><code>fn <a href='../../std/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class="struct" href="../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../primitive.unit.html">()</a>, <a class="struct" href="../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1769-1771' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Formats the value using the given formatter. <a href="../../std/fmt/trait.Debug.html#tymethod.fmt">Read more</a></p> </div></div><h3 id='impl-Deref' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Deref.html" title="trait std::ops::Deref">Deref</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Deref' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1942-1949' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Target' class="type"><span id='Target.t' class='invisible'><code>type <a href='../../std/ops/trait.Deref.html#associatedtype.Target' class="type">Target</a> = <a class="primitive" href="../primitive.str.html">str</a></code></span></h4> <div class='docblock'><p>The resulting type after dereferencing.</p> </div><h4 id='method.deref' class="method"><span id='deref.v' class='invisible'><code>fn <a href='../../std/ops/trait.Deref.html#tymethod.deref' class='fnname'>deref</a>(&self) -> &<a class="primitive" href="../primitive.str.html">str</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#1946-1948' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Dereferences the value.</p> </div></div><h3 id='impl-PartialOrd%3CString%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/cmp/trait.PartialOrd.html" title="trait std::cmp::PartialOrd">PartialOrd</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-PartialOrd%3CString%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.partial_cmp' class="method"><span id='partial_cmp.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialOrd.html#tymethod.partial_cmp' class='fnname'>partial_cmp</a>(&self, __arg_0: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="enum" href="../../std/option/enum.Option.html" title="enum std::option::Option">Option</a><<a class="enum" href="../../std/cmp/enum.Ordering.html" title="enum std::cmp::Ordering">Ordering</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method returns an ordering between <code>self</code> and <code>other</code> values if one exists. <a href="../../std/cmp/trait.PartialOrd.html#tymethod.partial_cmp">Read more</a></p> </div><h4 id='method.lt' class="method"><span id='lt.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialOrd.html#method.lt' class='fnname'>lt</a>(&self, __arg_0: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests less than (for <code>self</code> and <code>other</code>) and is used by the <code><</code> operator. <a href="../../std/cmp/trait.PartialOrd.html#method.lt">Read more</a></p> </div><h4 id='method.le' class="method"><span id='le.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialOrd.html#method.le' class='fnname'>le</a>(&self, __arg_0: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests less than or equal to (for <code>self</code> and <code>other</code>) and is used by the <code><=</code> operator. <a href="../../std/cmp/trait.PartialOrd.html#method.le">Read more</a></p> </div><h4 id='method.gt' class="method"><span id='gt.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialOrd.html#method.gt' class='fnname'>gt</a>(&self, __arg_0: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests greater than (for <code>self</code> and <code>other</code>) and is used by the <code>></code> operator. <a href="../../std/cmp/trait.PartialOrd.html#method.gt">Read more</a></p> </div><h4 id='method.ge' class="method"><span id='ge.v' class='invisible'><code>fn <a href='../../std/cmp/trait.PartialOrd.html#method.ge' class='fnname'>ge</a>(&self, __arg_0: &<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#292' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>This method tests greater than or equal to (for <code>self</code> and <code>other</code>) and is used by the <code>>=</code> operator. <a href="../../std/cmp/trait.PartialOrd.html#method.ge">Read more</a></p> </div></div><h3 id='impl-Write' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/fmt/trait.Write.html" title="trait std::fmt::Write">Write</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-Write' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2176-2188' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.write_str' class="method"><span id='write_str.v' class='invisible'><code>fn <a href='../../std/fmt/trait.Write.html#tymethod.write_str' class='fnname'>write_str</a>(&mut self, s: &<a class="primitive" href="../primitive.str.html">str</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../primitive.unit.html">()</a>, <a class="struct" href="../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2178-2181' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Writes a slice of bytes into this writer, returning whether the write succeeded. <a href="../../std/fmt/trait.Write.html#tymethod.write_str">Read more</a></p> </div><h4 id='method.write_char' class="method"><span id='write_char.v' class='invisible'><code>fn <a href='../../std/fmt/trait.Write.html#method.write_char' class='fnname'>write_char</a>(&mut self, c: <a class="primitive" href="../primitive.char.html">char</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../primitive.unit.html">()</a>, <a class="struct" href="../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/alloc/string.rs.html#2184-2187' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Writes a [<code>char</code>] into this writer, returning whether the write succeeded. <a href="../../std/fmt/trait.Write.html#method.write_char">Read more</a></p> </div><h4 id='method.write_fmt' class="method"><span id='write_fmt.v' class='invisible'><code>fn <a href='../../std/fmt/trait.Write.html#method.write_fmt' class='fnname'>write_fmt</a>(&mut self, args: <a class="struct" href="../../std/fmt/struct.Arguments.html" title="struct std::fmt::Arguments">Arguments</a>) -> <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a><<a class="primitive" href="../primitive.unit.html">()</a>, <a class="struct" href="../../std/fmt/struct.Error.html" title="struct std::fmt::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/core/fmt/mod.rs.html#204-227' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Glue for usage of the [<code>write!</code>] macro with implementors of this trait. <a href="../../std/fmt/trait.Write.html#method.write_fmt">Read more</a></p> </div></div><h3 id='impl-From%3CString%3E-5' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="trait" href="../../std/error/trait.Error.html" title="trait std::error::Error">Error</a> + <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> + <a class="trait" href="../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a>></code><a href='#impl-From%3CString%3E-5' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/error.rs.html#181-198' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-8' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></h3><code class="content"><span class="where fmt-newline">impl<I> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <I as <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a>>::<a class="type" href="../../std/iter/trait.Iterator.html#associatedtype.Item" title="type std::iter::Iterator::Item">Item</a>;</span><span class="where fmt-newline">impl<R: <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><R></span><span class="where fmt-newline">impl<W: <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><W></span></code></div></div><span id='from.v-8' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(err: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="trait" href="../../std/error/trait.Error.html" title="trait std::error::Error">Error</a> + <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> + <a class="trait" href="../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/error.rs.html#182-197' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E-6' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="trait" href="../../std/error/trait.Error.html" title="trait std::error::Error">Error</a>></code><a href='#impl-From%3CString%3E-6' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../src/std/error.rs.html#201-207' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-9' class="method"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I></h3><code class="content"><span class="where fmt-newline">impl<I> <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><I> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></span><span class="where fmt-newline"> type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <I as <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a>>::<a class="type" href="../../std/iter/trait.Iterator.html#associatedtype.Item" title="type std::iter::Iterator::Item">Item</a>;</span><span class="where fmt-newline">impl<R: <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><R></span><span class="where fmt-newline">impl<W: <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><W></span></code></div></div><span id='from.v-9' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(str_err: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><<a class="trait" href="../../std/error/trait.Error.html" title="trait std::error::Error">Error</a>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/error.rs.html#202-206' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-From%3CString%3E-7' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/ffi/struct.OsString.html" title="struct std::ffi::OsString">OsString</a></code><a href='#impl-From%3CString%3E-7' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/ffi/os_str.rs.html#319-323' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-10' class="method"><span id='from.v-10' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/ffi/struct.OsString.html" title="struct std::ffi::OsString">OsString</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/ffi/os_str.rs.html#320-322' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-AsRef%3COsStr%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.AsRef.html" title="trait std::convert::AsRef">AsRef</a><<a class="struct" href="../../std/ffi/struct.OsStr.html" title="struct std::ffi::OsStr">OsStr</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-AsRef%3COsStr%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/ffi/os_str.rs.html#808-812' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.as_ref-2' class="method"><span id='as_ref.v-2' class='invisible'><code>fn <a href='../../std/convert/trait.AsRef.html#tymethod.as_ref' class='fnname'>as_ref</a>(&self) -> &<a class="struct" href="../../std/ffi/struct.OsStr.html" title="struct std::ffi::OsStr">OsStr</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/ffi/os_str.rs.html#809-811' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-ToSocketAddrs' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/net/trait.ToSocketAddrs.html" title="trait std::net::ToSocketAddrs">ToSocketAddrs</a> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-ToSocketAddrs' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.16.0'>1.16.0</div><a class='srclink' href='../../src/std/net/addr.rs.html#924-929' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='associatedtype.Iter' class="type"><span id='Iter.t' class='invisible'><code>type <a href='../../std/net/trait.ToSocketAddrs.html#associatedtype.Iter' class="type">Iter</a> = <a class="struct" href="../../std/vec/struct.IntoIter.html" title="struct std::vec::IntoIter">IntoIter</a><<a class="enum" href="../../std/net/enum.SocketAddr.html" title="enum std::net::SocketAddr">SocketAddr</a>></code></span></h4> <div class='docblock'><p>Returned iterator over socket addresses which this type may correspond to. <a href="../../std/net/trait.ToSocketAddrs.html#associatedtype.Iter">Read more</a></p> </div><h4 id='method.to_socket_addrs' class="method"><span id='to_socket_addrs.v' class='invisible'><code>fn <a href='../../std/net/trait.ToSocketAddrs.html#tymethod.to_socket_addrs' class='fnname'>to_socket_addrs</a>(&self) -> <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a><<a class="struct" href="../../std/vec/struct.IntoIter.html" title="struct std::vec::IntoIter">IntoIter</a><<a class="enum" href="../../std/net/enum.SocketAddr.html" title="enum std::net::SocketAddr">SocketAddr</a>>></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/net/addr.rs.html#926-928' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Converts this object to an iterator of resolved <code>SocketAddr</code>s. <a href="../../std/net/trait.ToSocketAddrs.html#tymethod.to_socket_addrs">Read more</a></p> </div></div><h3 id='impl-From%3CString%3E-8' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a><<a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>> for <a class="struct" href="../../std/path/struct.PathBuf.html" title="struct std::path::PathBuf">PathBuf</a></code><a href='#impl-From%3CString%3E-8' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/path.rs.html#1394-1398' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.from-11' class="method"><span id='from.v-11' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(s: <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>) -> <a class="struct" href="../../std/path/struct.PathBuf.html" title="struct std::path::PathBuf">PathBuf</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/path.rs.html#1395-1397' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div><h3 id='impl-AsRef%3CPath%3E' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/convert/trait.AsRef.html" title="trait std::convert::AsRef">AsRef</a><<a class="struct" href="../../std/path/struct.Path.html" title="struct std::path::Path">Path</a>> for <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a></code><a href='#impl-AsRef%3CPath%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/path.rs.html#2496-2500' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.as_ref-3' class="method"><span id='as_ref.v-3' class='invisible'><code>fn <a href='../../std/convert/trait.AsRef.html#tymethod.as_ref' class='fnname'>as_ref</a>(&self) -> &<a class="struct" href="../../std/path/struct.Path.html" title="struct std::path::Path">Path</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/path.rs.html#2497-2499' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Performs the conversion.</p> </div></div></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>