994 lines
128 KiB
HTML
994 lines
128 KiB
HTML
|
<!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 `ImageSurfaceData` struct in crate `cairo`.">
|
|||
|
<meta name="keywords" content="rust, rustlang, rust-lang, ImageSurfaceData">
|
|||
|
|
|||
|
<title>cairo::ImageSurfaceData - Rust</title>
|
|||
|
|
|||
|
<link rel="stylesheet" type="text/css" href="../normalize.css">
|
|||
|
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
|||
|
<link rel="stylesheet" type="text/css" href="../main.css">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</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">
|
|||
|
|
|||
|
<p class='location'>Struct ImageSurfaceData</p><div class="block items"><ul><li><a href="#deref-methods">Methods from Deref<Target=[u8]></a></li><li><a href="#implementations">Trait Implementations</a></li></ul></div><p class='location'><a href='index.html'>cairo</a></p><script>window.sidebarCurrent = {name: 'ImageSurfaceData', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
|||
|
</nav>
|
|||
|
|
|||
|
<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'>cairo</a>::<wbr><a class="struct" href=''>ImageSurfaceData</a></span><span class='out-of-band'><span id='render-detail'>
|
|||
|
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
|
|||
|
[<span class='inner'>−</span>]
|
|||
|
</a>
|
|||
|
</span><a class='srclink' href='../src/cairo/image_surface.rs.html#138-142' title='goto source code'>[src]</a></span></h1>
|
|||
|
<pre class='rust struct'>pub struct ImageSurfaceData<'a> { /* fields omitted */ }</pre><h2 id='deref-methods'>Methods from <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html" title="trait core::ops::deref::Deref">Deref</a><Target = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>></h2><div class='impl-items'><h4 id='method.len' class="method"><span id='len.v' class='invisible'><code>fn <a href='#method.len' class='fnname'>len</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the number of elements in the slice.</p>
|
|||
|
|
|||
|
<h1 id='example' class='section-header'><a href='#example'>Example</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</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>);</pre>
|
|||
|
</div><h4 id='method.is_empty' class="method"><span id='is_empty.v' class='invisible'><code>fn <a href='#method.is_empty' class='fnname'>is_empty</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns <code>true</code> if the slice has a length of 0.</p>
|
|||
|
|
|||
|
<h1 id='example-1' class='section-header'><a href='#example-1'>Example</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">a</span>.<span class="ident">is_empty</span>());</pre>
|
|||
|
</div><h4 id='method.first' class="method"><span id='first.v' class='invisible'><code>fn <a href='#method.first' class='fnname'>first</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the first element of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples' class='section-header'><a href='#examples'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">10</span>), <span class="ident">v</span>.<span class="ident">first</span>());
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">w</span>: <span class="kw-2">&</span>[<span class="ident">i32</span>] <span class="op">=</span> <span class="kw-2">&</span>[];
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">w</span>.<span class="ident">first</span>());</pre>
|
|||
|
</div><h4 id='method.first_mut' class="method"><span id='first_mut.v' class='invisible'><code>fn <a href='#method.first_mut' class='fnname'>first_mut</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&mut T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a mutable pointer to the first element of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples-1' class='section-header'><a href='#examples-1'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">first</span>) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">first_mut</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">first</span> <span class="op">=</span> <span class="number">5</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">5</span>, <span class="number">1</span>, <span class="number">2</span>]);</pre>
|
|||
|
</div><h4 id='method.split_first' class="method"><span id='split_first.v' class='invisible'><code>fn <a href='#method.split_first' class='fnname'>split_first</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>&T, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>></code><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the first and all the rest of the elements of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples-2' class='section-header'><a href='#examples-2'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>((<span class="ident">first</span>, <span class="ident">elements</span>)) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split_first</span>() {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">first</span>, <span class="kw-2">&</span><span class="number">0</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">elements</span>, <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>]);
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.split_first_mut' class="method"><span id='split_first_mut.v' class='invisible'><code>fn <a href='#method.split_first_mut' class='fnname'>split_first_mut</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>&mut T, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [T]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>></code><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the first and all the rest of the elements of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples-3' class='section-header'><a href='#examples-3'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>((<span class="ident">first</span>, <span class="ident">elements</span>)) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split_first_mut</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">first</span> <span class="op">=</span> <span class="number">3</span>;
|
|||
|
<span class="ident">elements</span>[<span class="number">0</span>] <span class="op">=</span> <span class="number">4</span>;
|
|||
|
<span class="ident">elements</span>[<span class="number">1</span>] <span class="op">=</span> <span class="number">5</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</pre>
|
|||
|
</div><h4 id='method.split_last' class="method"><span id='split_last.v' class='invisible'><code>fn <a href='#method.split_last' class='fnname'>split_last</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>&T, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>></code><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the last and all the rest of the elements of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples-4' class='section-header'><a href='#examples-4'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>((<span class="ident">last</span>, <span class="ident">elements</span>)) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split_last</span>() {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">last</span>, <span class="kw-2">&</span><span class="number">2</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">elements</span>, <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">1</span>]);
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.split_last_mut' class="method"><span id='split_last_mut.v' class='invisible'><code>fn <a href='#method.split_last_mut' class='fnname'>split_last_mut</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>&mut T, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [T]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>></code><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the last and all the rest of the elements of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples-5' class='section-header'><a href='#examples-5'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>((<span class="ident">last</span>, <span class="ident">elements</span>)) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">split_last_mut</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">last</span> <span class="op">=</span> <span class="number">3</span>;
|
|||
|
<span class="ident">elements</span>[<span class="number">0</span>] <span class="op">=</span> <span class="number">4</span>;
|
|||
|
<span class="ident">elements</span>[<span class="number">1</span>] <span class="op">=</span> <span class="number">5</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">3</span>]);</pre>
|
|||
|
</div><h4 id='method.last' class="method"><span id='last.v' class='invisible'><code>fn <a href='#method.last' class='fnname'>last</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns the last element of the slice, or <code>None</code> if it is empty.</p>
|
|||
|
|
|||
|
<h1 id='examples-6' class='section-header'><a href='#examples-6'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">30</span>), <span class="ident">v</span>.<span class="ident">last</span>());
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">w</span>: <span class="kw-2">&</span>[<span class="ident">i32</span>] <span class="op">=</span> <span class="kw-2">&</span>[];
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">w</span>.<span class="ident">last</span>());</pre>
|
|||
|
</div><h4 id='method.last_mut' class="method"><span id='last_mut.v' class='invisible'><code>fn <a href='#method.last_mut' class='fnname'>last_mut</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&mut T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a mutable pointer to the last item in the slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-7' class='section-header'><a href='#examples-7'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">last</span>) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">last_mut</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">last</span> <span class="op">=</span> <span class="number">10</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">10</span>]);</pre>
|
|||
|
</div><h4 id='method.get' class="method"><span id='get.v' class='invisible'><code>fn <a href='#method.get' class='fnname'>get</a><I>(&self, index: I) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&<I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a>> <span class="where fmt-newline">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a reference to an element or subslice depending on the type of
|
|||
|
index.</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>If given a position, returns a reference to the element at that
|
|||
|
position or <code>None</code> if out of bounds.</li>
|
|||
|
<li>If given a range, returns the subslice corresponding to that range,
|
|||
|
or <code>None</code> if out of bounds.</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h1 id='examples-8' class='section-header'><a href='#examples-8'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">40</span>), <span class="ident">v</span>.<span class="ident">get</span>(<span class="number">1</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">40</span>][..]), <span class="ident">v</span>.<span class="ident">get</span>(<span class="number">0</span>..<span class="number">2</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">v</span>.<span class="ident">get</span>(<span class="number">3</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">None</span>, <span class="ident">v</span>.<span class="ident">get</span>(<span class="number">0</span>..<span class="number">4</span>));</pre>
|
|||
|
</div><h4 id='method.get_mut' class="method"><span id='get_mut.v' class='invisible'><code>fn <a href='#method.get_mut' class='fnname'>get_mut</a><I>(<br> &mut self, <br> index: I<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&mut <I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a>> <span class="where fmt-newline">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a mutable reference to an element or subslice depending on the
|
|||
|
type of index (see <a href="#method.get"><code>get</code></a>) or <code>None</code> if the index is out of bounds.</p>
|
|||
|
|
|||
|
<h1 id='examples-9' class='section-header'><a href='#examples-9'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">elem</span>) <span class="op">=</span> <span class="ident">x</span>.<span class="ident">get_mut</span>(<span class="number">1</span>) {
|
|||
|
<span class="kw-2">*</span><span class="ident">elem</span> <span class="op">=</span> <span class="number">42</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">42</span>, <span class="number">2</span>]);</pre>
|
|||
|
</div><h4 id='method.get_unchecked' class="method"><span id='get_unchecked.v' class='invisible'><code>unsafe fn <a href='#method.get_unchecked' class='fnname'>get_unchecked</a><I>(&self, index: I) -> &<I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a reference to an element or subslice, without doing bounds
|
|||
|
checking.</p>
|
|||
|
|
|||
|
<p>This is generally not recommended, use with caution! For a safe
|
|||
|
alternative see <a href="#method.get"><code>get</code></a>.</p>
|
|||
|
|
|||
|
<h1 id='examples-10' class='section-header'><a href='#examples-10'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>];
|
|||
|
|
|||
|
<span class="kw">unsafe</span> {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>.<span class="ident">get_unchecked</span>(<span class="number">1</span>), <span class="kw-2">&</span><span class="number">2</span>);
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.get_unchecked_mut' class="method"><span id='get_unchecked_mut.v' class='invisible'><code>unsafe fn <a href='#method.get_unchecked_mut' class='fnname'>get_unchecked_mut</a><I>(<br> &mut self, <br> index: I<br>) -> &mut <I as <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>>::<a class="type" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html#associatedtype.Output" title="type core::slice::SliceIndex::Output">Output</a> <span class="where fmt-newline">where<br> I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/slice/trait.SliceIndex.html" title="trait core::slice::SliceIndex">SliceIndex</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a mutable reference to an element or subslice, without doing
|
|||
|
bounds checking.</p>
|
|||
|
|
|||
|
<p>This is generally not recommended, use with caution! For a safe
|
|||
|
alternative see <a href="#method.get_mut"><code>get_mut</code></a>.</p>
|
|||
|
|
|||
|
<h1 id='examples-11' class='section-header'><a href='#examples-11'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>];
|
|||
|
|
|||
|
<span class="kw">unsafe</span> {
|
|||
|
<span class="kw">let</span> <span class="ident">elem</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">get_unchecked_mut</span>(<span class="number">1</span>);
|
|||
|
<span class="kw-2">*</span><span class="ident">elem</span> <span class="op">=</span> <span class="number">13</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">13</span>, <span class="number">4</span>]);</pre>
|
|||
|
</div><h4 id='method.as_ptr' class="method"><span id='as_ptr.v' class='invisible'><code>fn <a href='#method.as_ptr' class='fnname'>as_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.pointer.html">*const T</a></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns a raw pointer to the slice's buffer.</p>
|
|||
|
|
|||
|
<p>The caller must ensure that the slice outlives the pointer this
|
|||
|
function returns, or else it will end up pointing to garbage.</p>
|
|||
|
|
|||
|
<p>Modifying the container referenced by this slice may cause its buffer
|
|||
|
to be reallocated, which would also make any pointers to it invalid.</p>
|
|||
|
|
|||
|
<h1 id='examples-12' class='section-header'><a href='#examples-12'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>];
|
|||
|
<span class="kw">let</span> <span class="ident">x_ptr</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">as_ptr</span>();
|
|||
|
|
|||
|
<span class="kw">unsafe</span> {
|
|||
|
<span class="kw">for</span> <span class="ident">i</span> <span class="kw">in</span> <span class="number">0</span>..<span class="ident">x</span>.<span class="ident">len</span>() {
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>.<span class="ident">get_unchecked</span>(<span class="ident">i</span>), <span class="kw-2">&</span><span class="kw-2">*</span><span class="ident">x_ptr</span>.<span class="ident">offset</span>(<span class="ident">i</span> <span class="kw">as</span> <span class="ident">isize</span>));
|
|||
|
}
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.as_mut_ptr' class="method"><span id='as_mut_ptr.v' class='invisible'><code>fn <a href='#method.as_mut_ptr' class='fnname'>as_mut_ptr</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.pointer.html">*mut T</a></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an unsafe mutable pointer to the slice's buffer.</p>
|
|||
|
|
|||
|
<p>The caller must ensure that the slice outlives the pointer this
|
|||
|
function returns, or else it will end up pointing to garbage.</p>
|
|||
|
|
|||
|
<p>Modifying the container referenced by this slice may cause its buffer
|
|||
|
to be reallocated, which would also make any pointers to it invalid.</p>
|
|||
|
|
|||
|
<h1 id='examples-13' class='section-header'><a href='#examples-13'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>];
|
|||
|
<span class="kw">let</span> <span class="ident">x_ptr</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">as_mut_ptr</span>();
|
|||
|
|
|||
|
<span class="kw">unsafe</span> {
|
|||
|
<span class="kw">for</span> <span class="ident">i</span> <span class="kw">in</span> <span class="number">0</span>..<span class="ident">x</span>.<span class="ident">len</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">x_ptr</span>.<span class="ident">offset</span>(<span class="ident">i</span> <span class="kw">as</span> <span class="ident">isize</span>) <span class="op">+=</span> <span class="number">2</span>;
|
|||
|
}
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>]);</pre>
|
|||
|
</div><h4 id='method.swap' class="method"><span id='swap.v' class='invisible'><code>fn <a href='#method.swap' class='fnname'>swap</a>(&mut self, a: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, b: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>)</code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Swaps two elements in the slice.</p>
|
|||
|
|
|||
|
<h1 id='arguments' class='section-header'><a href='#arguments'>Arguments</a></h1>
|
|||
|
<ul>
|
|||
|
<li>a - The index of the first element</li>
|
|||
|
<li>b - The index of the second element</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h1 id='panics' class='section-header'><a href='#panics'>Panics</a></h1>
|
|||
|
<p>Panics if <code>a</code> or <code>b</code> are out of bounds.</p>
|
|||
|
|
|||
|
<h1 id='examples-14' class='section-header'><a href='#examples-14'>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="string">"a"</span>, <span class="string">"b"</span>, <span class="string">"c"</span>, <span class="string">"d"</span>];
|
|||
|
<span class="ident">v</span>.<span class="ident">swap</span>(<span class="number">1</span>, <span class="number">3</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="string">"a"</span>, <span class="string">"d"</span>, <span class="string">"c"</span>, <span class="string">"b"</span>]);</pre>
|
|||
|
</div><h4 id='method.reverse' class="method"><span id='reverse.v' class='invisible'><code>fn <a href='#method.reverse' class='fnname'>reverse</a>(&mut self)</code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Reverses the order of elements in the slice, in place.</p>
|
|||
|
|
|||
|
<h1 id='example-2' class='section-header'><a href='#example-2'>Example</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="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
|
|||
|
<span class="ident">v</span>.<span class="ident">reverse</span>();
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>]);</pre>
|
|||
|
</div><h4 id='method.iter' class="method"><span id='iter.v' class='invisible'><code>fn <a href='#method.iter' class='fnname'>iter</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.Iter.html" title="struct core::slice::Iter">Iter</a><T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over the slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-15' class='section-header'><a href='#examples-15'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iterator</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">iter</span>();
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iterator</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">1</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iterator</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">2</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iterator</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">4</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iterator</span>.<span class="ident">next</span>(), <span class="prelude-val">None</span>);</pre>
|
|||
|
</div><h4 id='method.iter_mut' class="method"><span id='iter_mut.v' class='invisible'><code>fn <a href='#method.iter_mut' class='fnname'>iter_mut</a>(&mut self) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.IterMut.html" title="struct core::slice::IterMut">IterMut</a><T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator that allows modifying each value.</p>
|
|||
|
|
|||
|
<h1 id='examples-16' class='section-header'><a href='#examples-16'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>];
|
|||
|
<span class="kw">for</span> <span class="ident">elem</span> <span class="kw">in</span> <span class="ident">x</span>.<span class="ident">iter_mut</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">elem</span> <span class="op">+=</span> <span class="number">2</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="kw-2">&</span>[<span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>]);</pre>
|
|||
|
</div><h4 id='method.windows' class="method"><span id='windows.v' class='invisible'><code>fn <a href='#method.windows' class='fnname'>windows</a>(&self, size: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.Windows.html" title="struct core::slice::Windows">Windows</a><T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over all contiguous windows of length
|
|||
|
<code>size</code>. The windows overlap. If the slice is shorter than
|
|||
|
<code>size</code>, the iterator returns no values.</p>
|
|||
|
|
|||
|
<h1 id='panics-1' class='section-header'><a href='#panics-1'>Panics</a></h1>
|
|||
|
<p>Panics if <code>size</code> is 0.</p>
|
|||
|
|
|||
|
<h1 id='example-3' class='section-header'><a href='#example-3'>Example</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="string">'r'</span>, <span class="string">'u'</span>, <span class="string">'s'</span>, <span class="string">'t'</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">windows</span>(<span class="number">2</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="string">'r'</span>, <span class="string">'u'</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="string">'u'</span>, <span class="string">'s'</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="string">'s'</span>, <span class="string">'t'</span>]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">is_none</span>());</pre>
|
|||
|
|
|||
|
<p>If the slice is shorter than <code>size</code>:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="string">'f'</span>, <span class="string">'o'</span>, <span class="string">'o'</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">windows</span>(<span class="number">4</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">is_none</span>());</pre>
|
|||
|
</div><h4 id='method.chunks' class="method"><span id='chunks.v' class='invisible'><code>fn <a href='#method.chunks' class='fnname'>chunks</a>(&self, size: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.Chunks.html" title="struct core::slice::Chunks">Chunks</a><T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over <code>size</code> elements of the slice at a
|
|||
|
time. The chunks are slices and do not overlap. If <code>size</code> does
|
|||
|
not divide the length of the slice, then the last chunk will
|
|||
|
not have length <code>size</code>.</p>
|
|||
|
|
|||
|
<h1 id='panics-2' class='section-header'><a href='#panics-2'>Panics</a></h1>
|
|||
|
<p>Panics if <code>size</code> is 0.</p>
|
|||
|
|
|||
|
<h1 id='example-4' class='section-header'><a href='#example-4'>Example</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="string">'l'</span>, <span class="string">'o'</span>, <span class="string">'r'</span>, <span class="string">'e'</span>, <span class="string">'m'</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">chunks</span>(<span class="number">2</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="string">'l'</span>, <span class="string">'o'</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="string">'r'</span>, <span class="string">'e'</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="string">'m'</span>]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">is_none</span>());</pre>
|
|||
|
</div><h4 id='method.chunks_mut' class="method"><span id='chunks_mut.v' class='invisible'><code>fn <a href='#method.chunks_mut' class='fnname'>chunks_mut</a>(&mut self, chunk_size: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.ChunksMut.html" title="struct core::slice::ChunksMut">ChunksMut</a><T></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over <code>chunk_size</code> elements of the slice at a time.
|
|||
|
The chunks are mutable slices, and do not overlap. If <code>chunk_size</code> does
|
|||
|
not divide the length of the slice, then the last chunk will not
|
|||
|
have length <code>chunk_size</code>.</p>
|
|||
|
|
|||
|
<h1 id='panics-3' class='section-header'><a href='#panics-3'>Panics</a></h1>
|
|||
|
<p>Panics if <code>chunk_size</code> is 0.</p>
|
|||
|
|
|||
|
<h1 id='examples-17' class='section-header'><a href='#examples-17'>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="kw-2">&</span><span class="kw-2">mut</span> [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">count</span> <span class="op">=</span> <span class="number">1</span>;
|
|||
|
|
|||
|
<span class="kw">for</span> <span class="ident">chunk</span> <span class="kw">in</span> <span class="ident">v</span>.<span class="ident">chunks_mut</span>(<span class="number">2</span>) {
|
|||
|
<span class="kw">for</span> <span class="ident">elem</span> <span class="kw">in</span> <span class="ident">chunk</span>.<span class="ident">iter_mut</span>() {
|
|||
|
<span class="kw-2">*</span><span class="ident">elem</span> <span class="op">+=</span> <span class="ident">count</span>;
|
|||
|
}
|
|||
|
<span class="ident">count</span> <span class="op">+=</span> <span class="number">1</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>]);</pre>
|
|||
|
</div><h4 id='method.split_at' class="method"><span id='split_at.v' class='invisible'><code>fn <a href='#method.split_at' class='fnname'>split_at</a>(&self, mid: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Divides one slice into two at an index.</p>
|
|||
|
|
|||
|
<p>The first will contain all indices from <code>[0, mid)</code> (excluding
|
|||
|
the index <code>mid</code> itself) and the second will contain all
|
|||
|
indices from <code>[mid, len)</code> (excluding the index <code>len</code> itself).</p>
|
|||
|
|
|||
|
<h1 id='panics-4' class='section-header'><a href='#panics-4'>Panics</a></h1>
|
|||
|
<p>Panics if <code>mid > len</code>.</p>
|
|||
|
|
|||
|
<h1 id='examples-18' class='section-header'><a href='#examples-18'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">50</span>];
|
|||
|
<span class="kw">let</span> (<span class="ident">v1</span>, <span class="ident">v2</span>) <span class="op">=</span> <span class="ident">v</span>.<span class="ident">split_at</span>(<span class="number">2</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>([<span class="number">10</span>, <span class="number">40</span>], <span class="ident">v1</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>([<span class="number">30</span>, <span class="number">20</span>, <span class="number">50</span>], <span class="ident">v2</span>);</pre>
|
|||
|
</div><h4 id='method.split_at_mut' class="method"><span id='split_at_mut.v' class='invisible'><code>fn <a href='#method.split_at_mut' class='fnname'>split_at_mut</a>(&mut self, mid: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [T]</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [T]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Divides one <code>&mut</code> into two at an index.</p>
|
|||
|
|
|||
|
<p>The first will contain all indices from <code>[0, mid)</code> (excluding
|
|||
|
the index <code>mid</code> itself) and the second will contain all
|
|||
|
indices from <code>[mid, len)</code> (excluding the index <code>len</code> itself).</p>
|
|||
|
|
|||
|
<h1 id='panics-5' class='section-header'><a href='#panics-5'>Panics</a></h1>
|
|||
|
<p>Panics if <code>mid > len</code>.</p>
|
|||
|
|
|||
|
<h1 id='examples-19' class='section-header'><a href='#examples-19'>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="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
|
|||
|
|
|||
|
<span class="comment">// scoped to restrict the lifetime of the borrows</span>
|
|||
|
{
|
|||
|
<span class="kw">let</span> (<span class="ident">left</span>, <span class="ident">right</span>) <span class="op">=</span> <span class="ident">v</span>.<span class="ident">split_at_mut</span>(<span class="number">0</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">left</span> <span class="op">==</span> []);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">right</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);
|
|||
|
}
|
|||
|
|
|||
|
{
|
|||
|
<span class="kw">let</span> (<span class="ident">left</span>, <span class="ident">right</span>) <span class="op">=</span> <span class="ident">v</span>.<span class="ident">split_at_mut</span>(<span class="number">2</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">left</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">right</span> <span class="op">==</span> [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);
|
|||
|
}
|
|||
|
|
|||
|
{
|
|||
|
<span class="kw">let</span> (<span class="ident">left</span>, <span class="ident">right</span>) <span class="op">=</span> <span class="ident">v</span>.<span class="ident">split_at_mut</span>(<span class="number">6</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">left</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">right</span> <span class="op">==</span> []);
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.split' class="method"><span id='split.v' class='invisible'><code>fn <a href='#method.split' class='fnname'>split</a><F>(&self, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.Split.html" title="struct core::slice::Split">Split</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over subslices separated by elements that match
|
|||
|
<code>pred</code>. The matched element is not contained in the subslices.</p>
|
|||
|
|
|||
|
<h1 id='examples-20' class='section-header'><a href='#examples-20'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">40</span>, <span class="number">33</span>, <span class="number">20</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">split</span>(<span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">40</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">20</span>]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">is_none</span>());</pre>
|
|||
|
|
|||
|
<p>If the first element is matched, an empty slice will be the first item
|
|||
|
returned by the iterator. Similarly, if the last element in the slice
|
|||
|
is matched, an empty slice will be the last item returned by the
|
|||
|
iterator:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">40</span>, <span class="number">33</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">split</span>(<span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">40</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">is_none</span>());</pre>
|
|||
|
|
|||
|
<p>If two matched elements are directly adjacent, an empty slice will be
|
|||
|
present between them:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">6</span>, <span class="number">33</span>, <span class="number">20</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">split</span>(<span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">10</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">20</span>]);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">is_none</span>());</pre>
|
|||
|
</div><h4 id='method.split_mut' class="method"><span id='split_mut.v' class='invisible'><code>fn <a href='#method.split_mut' class='fnname'>split_mut</a><F>(&mut self, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.SplitMut.html" title="struct core::slice::SplitMut">SplitMut</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over mutable subslices separated by elements that
|
|||
|
match <code>pred</code>. The matched element is not contained in the subslices.</p>
|
|||
|
|
|||
|
<h1 id='examples-21' class='section-header'><a href='#examples-21'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">50</span>];
|
|||
|
|
|||
|
<span class="kw">for</span> <span class="ident">group</span> <span class="kw">in</span> <span class="ident">v</span>.<span class="ident">split_mut</span>(<span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>) {
|
|||
|
<span class="ident">group</span>[<span class="number">0</span>] <span class="op">=</span> <span class="number">1</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="number">1</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">1</span>, <span class="number">60</span>, <span class="number">1</span>]);</pre>
|
|||
|
</div><h4 id='method.rsplit' class="method"><span id='rsplit.v' class='invisible'><code>fn <a href='#method.rsplit' class='fnname'>rsplit</a><F>(&self, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.RSplit.html" title="struct core::slice::RSplit">RSplit</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code></span></h4>
|
|||
|
<div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>slice_rsplit</code>)</div></div><div class='docblock'><p>Returns an iterator over subslices separated by elements that match
|
|||
|
<code>pred</code>, starting at the end of the slice and working backwards.
|
|||
|
The matched element is not contained in the subslices.</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="op">!</span>[<span class="ident">feature</span>(<span class="ident">slice_rsplit</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">slice</span> <span class="op">=</span> [<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>, <span class="number">0</span>, <span class="number">44</span>, <span class="number">55</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">iter</span> <span class="op">=</span> <span class="ident">slice</span>.<span class="ident">rsplit</span>(<span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">==</span> <span class="number">0</span>);
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">44</span>, <span class="number">55</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">iter</span>.<span class="ident">next</span>(), <span class="prelude-val">None</span>);</pre>
|
|||
|
|
|||
|
<p>As with <code>split()</code>, if the first or last element is matched, an empty
|
|||
|
slice will be the first (or last) item returned by the iterator.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#<span class="op">!</span>[<span class="ident">feature</span>(<span class="ident">slice_rsplit</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">8</span>];
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">it</span> <span class="op">=</span> <span class="ident">v</span>.<span class="ident">rsplit</span>(<span class="op">|</span><span class="ident">n</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">n</span> <span class="op">%</span> <span class="number">2</span> <span class="op">==</span> <span class="number">0</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">it</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">it</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">3</span>, <span class="number">5</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">it</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">1</span>]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">it</span>.<span class="ident">next</span>().<span class="ident">unwrap</span>(), <span class="kw-2">&</span>[]);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">it</span>.<span class="ident">next</span>(), <span class="prelude-val">None</span>);</pre>
|
|||
|
</div><h4 id='method.rsplit_mut' class="method"><span id='rsplit_mut.v' class='invisible'><code>fn <a href='#method.rsplit_mut' class='fnname'>rsplit_mut</a><F>(&mut self, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.RSplitMut.html" title="struct core::slice::RSplitMut">RSplitMut</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code></span></h4>
|
|||
|
<div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>slice_rsplit</code>)</div></div><div class='docblock'><p>Returns an iterator over mutable subslices separated by elements that
|
|||
|
match <code>pred</code>, starting at the end of the slice and working
|
|||
|
backwards. The matched element is not contained in the subslices.</p>
|
|||
|
|
|||
|
<h1 id='examples-23' class='section-header'><a href='#examples-23'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#<span class="op">!</span>[<span class="ident">feature</span>(<span class="ident">slice_rsplit</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> [<span class="number">100</span>, <span class="number">400</span>, <span class="number">300</span>, <span class="number">200</span>, <span class="number">600</span>, <span class="number">500</span>];
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">count</span> <span class="op">=</span> <span class="number">0</span>;
|
|||
|
<span class="kw">for</span> <span class="ident">group</span> <span class="kw">in</span> <span class="ident">v</span>.<span class="ident">rsplit_mut</span>(<span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>) {
|
|||
|
<span class="ident">count</span> <span class="op">+=</span> <span class="number">1</span>;
|
|||
|
<span class="ident">group</span>[<span class="number">0</span>] <span class="op">=</span> <span class="ident">count</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="number">3</span>, <span class="number">400</span>, <span class="number">300</span>, <span class="number">2</span>, <span class="number">600</span>, <span class="number">1</span>]);</pre>
|
|||
|
</div><h4 id='method.splitn' class="method"><span id='splitn.v' class='invisible'><code>fn <a href='#method.splitn' class='fnname'>splitn</a><F>(&self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.SplitN.html" title="struct core::slice::SplitN">SplitN</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over subslices separated by elements that match
|
|||
|
<code>pred</code>, limited to returning at most <code>n</code> items. The matched element is
|
|||
|
not contained in the subslices.</p>
|
|||
|
|
|||
|
<p>The last element returned, if any, will contain the remainder of the
|
|||
|
slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-24' class='section-header'><a href='#examples-24'>Examples</a></h1>
|
|||
|
<p>Print the slice split once by numbers divisible by 3 (i.e. <code>[10, 40]</code>,
|
|||
|
<code>[20, 60, 50]</code>):</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">50</span>];
|
|||
|
|
|||
|
<span class="kw">for</span> <span class="ident">group</span> <span class="kw">in</span> <span class="ident">v</span>.<span class="ident">splitn</span>(<span class="number">2</span>, <span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>) {
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">group</span>);
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.splitn_mut' class="method"><span id='splitn_mut.v' class='invisible'><code>fn <a href='#method.splitn_mut' class='fnname'>splitn_mut</a><F>(&mut self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.SplitNMut.html" title="struct core::slice::SplitNMut">SplitNMut</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over subslices separated by elements that match
|
|||
|
<code>pred</code>, limited to returning at most <code>n</code> items. The matched element is
|
|||
|
not contained in the subslices.</p>
|
|||
|
|
|||
|
<p>The last element returned, if any, will contain the remainder of the
|
|||
|
slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-25' class='section-header'><a href='#examples-25'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">50</span>];
|
|||
|
|
|||
|
<span class="kw">for</span> <span class="ident">group</span> <span class="kw">in</span> <span class="ident">v</span>.<span class="ident">splitn_mut</span>(<span class="number">2</span>, <span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>) {
|
|||
|
<span class="ident">group</span>[<span class="number">0</span>] <span class="op">=</span> <span class="number">1</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, [<span class="number">1</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">1</span>, <span class="number">60</span>, <span class="number">50</span>]);</pre>
|
|||
|
</div><h4 id='method.rsplitn' class="method"><span id='rsplitn.v' class='invisible'><code>fn <a href='#method.rsplitn' class='fnname'>rsplitn</a><F>(&self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.RSplitN.html" title="struct core::slice::RSplitN">RSplitN</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over subslices separated by elements that match
|
|||
|
<code>pred</code> limited to returning at most <code>n</code> items. This starts at the end of
|
|||
|
the slice and works backwards. The matched element is not contained in
|
|||
|
the subslices.</p>
|
|||
|
|
|||
|
<p>The last element returned, if any, will contain the remainder of the
|
|||
|
slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-26' class='section-header'><a href='#examples-26'>Examples</a></h1>
|
|||
|
<p>Print the slice split once, starting from the end, by numbers divisible
|
|||
|
by 3 (i.e. <code>[50]</code>, <code>[10, 40, 30, 20]</code>):</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">50</span>];
|
|||
|
|
|||
|
<span class="kw">for</span> <span class="ident">group</span> <span class="kw">in</span> <span class="ident">v</span>.<span class="ident">rsplitn</span>(<span class="number">2</span>, <span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>) {
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">group</span>);
|
|||
|
}</pre>
|
|||
|
</div><h4 id='method.rsplitn_mut' class="method"><span id='rsplitn_mut.v' class='invisible'><code>fn <a href='#method.rsplitn_mut' class='fnname'>rsplitn_mut</a><F>(&mut self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, pred: F) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/slice/struct.RSplitNMut.html" title="struct core::slice::RSplitNMut">RSplitNMut</a><T, F> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns an iterator over subslices separated by elements that match
|
|||
|
<code>pred</code> limited to returning at most <code>n</code> items. This starts at the end of
|
|||
|
the slice and works backwards. The matched element is not contained in
|
|||
|
the subslices.</p>
|
|||
|
|
|||
|
<p>The last element returned, if any, will contain the remainder of the
|
|||
|
slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-27' class='section-header'><a href='#examples-27'>Examples</a></h1>
|
|||
|
<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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">50</span>];
|
|||
|
|
|||
|
<span class="kw">for</span> <span class="ident">group</span> <span class="kw">in</span> <span class="ident">s</span>.<span class="ident">rsplitn_mut</span>(<span class="number">2</span>, <span class="op">|</span><span class="ident">num</span><span class="op">|</span> <span class="kw-2">*</span><span class="ident">num</span> <span class="op">%</span> <span class="number">3</span> <span class="op">==</span> <span class="number">0</span>) {
|
|||
|
<span class="ident">group</span>[<span class="number">0</span>] <span class="op">=</span> <span class="number">1</span>;
|
|||
|
}
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>, [<span class="number">1</span>, <span class="number">40</span>, <span class="number">30</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">1</span>]);</pre>
|
|||
|
</div><h4 id='method.contains' class="method"><span id='contains.v' class='invisible'><code>fn <a href='#method.contains' class='fnname'>contains</a>(&self, x: &T) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html" title="trait core::cmp::PartialEq">PartialEq</a><T>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns <code>true</code> if the slice contains an element with the given value.</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="ident">v</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">contains</span>(<span class="kw-2">&</span><span class="number">30</span>));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">v</span>.<span class="ident">contains</span>(<span class="kw-2">&</span><span class="number">50</span>));</pre>
|
|||
|
</div><h4 id='method.starts_with' class="method"><span id='starts_with.v' class='invisible'><code>fn <a href='#method.starts_with' class='fnname'>starts_with</a>(&self, needle: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html" title="trait core::cmp::PartialEq">PartialEq</a><T>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns <code>true</code> if <code>needle</code> is a prefix of the slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-29' class='section-header'><a href='#examples-29'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">starts_with</span>(<span class="kw-2">&</span>[<span class="number">10</span>]));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">starts_with</span>(<span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">40</span>]));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">v</span>.<span class="ident">starts_with</span>(<span class="kw-2">&</span>[<span class="number">50</span>]));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">v</span>.<span class="ident">starts_with</span>(<span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">50</span>]));</pre>
|
|||
|
|
|||
|
<p>Always returns <code>true</code> if <code>needle</code> is an empty slice:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">starts_with</span>(<span class="kw-2">&</span>[]));
|
|||
|
<span class="kw">let</span> <span class="ident">v</span>: <span class="kw-2">&</span>[<span class="ident">u8</span>] <span class="op">=</span> <span class="kw-2">&</span>[];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">starts_with</span>(<span class="kw-2">&</span>[]));</pre>
|
|||
|
</div><h4 id='method.ends_with' class="method"><span id='ends_with.v' class='invisible'><code>fn <a href='#method.ends_with' class='fnname'>ends_with</a>(&self, needle: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html" title="trait core::cmp::PartialEq">PartialEq</a><T>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Returns <code>true</code> if <code>needle</code> is a suffix of the slice.</p>
|
|||
|
|
|||
|
<h1 id='examples-30' class='section-header'><a href='#examples-30'>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="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">ends_with</span>(<span class="kw-2">&</span>[<span class="number">30</span>]));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">ends_with</span>(<span class="kw-2">&</span>[<span class="number">40</span>, <span class="number">30</span>]));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">v</span>.<span class="ident">ends_with</span>(<span class="kw-2">&</span>[<span class="number">50</span>]));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">v</span>.<span class="ident">ends_with</span>(<span class="kw-2">&</span>[<span class="number">50</span>, <span class="number">30</span>]));</pre>
|
|||
|
|
|||
|
<p>Always returns <code>true</code> if <code>needle</code> is an empty slice:</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">ends_with</span>(<span class="kw-2">&</span>[]));
|
|||
|
<span class="kw">let</span> <span class="ident">v</span>: <span class="kw-2">&</span>[<span class="ident">u8</span>] <span class="op">=</span> <span class="kw-2">&</span>[];
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span>.<span class="ident">ends_with</span>(<span class="kw-2">&</span>[]));</pre>
|
|||
|
</div><h4 id='method.binary_search' class="method"><span id='binary_search.v' class='invisible'><code>fn <a href='#method.binary_search' class='fnname'>binary_search</a>(&self, x: &T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Binary searches this sorted slice for a given element.</p>
|
|||
|
|
|||
|
<p>If the value is found then <code>Ok</code> is returned, containing the
|
|||
|
index of the matching element; if the value is not found then
|
|||
|
<code>Err</code> is returned, containing the index where a matching
|
|||
|
element could be inserted while maintaining sorted order.</p>
|
|||
|
|
|||
|
<h1 id='example-5' class='section-header'><a href='#example-5'>Example</a></h1>
|
|||
|
<p>Looks up a series of four elements. The first is found, with a
|
|||
|
uniquely determined position; the second and third are not
|
|||
|
found; the fourth could match any position in <code>[1, 4]</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">13</span>, <span class="number">21</span>, <span class="number">34</span>, <span class="number">55</span>];
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search</span>(<span class="kw-2">&</span><span class="number">13</span>), <span class="prelude-val">Ok</span>(<span class="number">9</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search</span>(<span class="kw-2">&</span><span class="number">4</span>), <span class="prelude-val">Err</span>(<span class="number">7</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search</span>(<span class="kw-2">&</span><span class="number">100</span>), <span class="prelude-val">Err</span>(<span class="number">13</span>));
|
|||
|
<span class="kw">let</span> <span class="ident">r</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">binary_search</span>(<span class="kw-2">&</span><span class="number">1</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="kw">match</span> <span class="ident">r</span> { <span class="prelude-val">Ok</span>(<span class="number">1</span>...<span class="number">4</span>) <span class="op">=></span> <span class="bool-val">true</span>, _ <span class="op">=></span> <span class="bool-val">false</span>, });</pre>
|
|||
|
</div><h4 id='method.binary_search_by' class="method"><span id='binary_search_by.v' class='invisible'><code>fn <a href='#method.binary_search_by' class='fnname'>binary_search_by</a><'a, F>(&'a self, f: F) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&'a T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/cmp/enum.Ordering.html" title="enum core::cmp::Ordering">Ordering</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Binary searches this sorted slice with a comparator function.</p>
|
|||
|
|
|||
|
<p>The comparator function should implement an order consistent
|
|||
|
with the sort order of the underlying slice, returning an
|
|||
|
order code that indicates whether its argument is <code>Less</code>,
|
|||
|
<code>Equal</code> or <code>Greater</code> the desired target.</p>
|
|||
|
|
|||
|
<p>If a matching value is found then returns <code>Ok</code>, containing
|
|||
|
the index for the matched element; if no match is found then
|
|||
|
<code>Err</code> is returned, containing the index where a matching
|
|||
|
element could be inserted while maintaining sorted order.</p>
|
|||
|
|
|||
|
<h1 id='example-6' class='section-header'><a href='#example-6'>Example</a></h1>
|
|||
|
<p>Looks up a series of four elements. The first is found, with a
|
|||
|
uniquely determined position; the second and third are not
|
|||
|
found; the fourth could match any position in <code>[1, 4]</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> [<span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">13</span>, <span class="number">21</span>, <span class="number">34</span>, <span class="number">55</span>];
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">seek</span> <span class="op">=</span> <span class="number">13</span>;
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search_by</span>(<span class="op">|</span><span class="ident">probe</span><span class="op">|</span> <span class="ident">probe</span>.<span class="ident">cmp</span>(<span class="kw-2">&</span><span class="ident">seek</span>)), <span class="prelude-val">Ok</span>(<span class="number">9</span>));
|
|||
|
<span class="kw">let</span> <span class="ident">seek</span> <span class="op">=</span> <span class="number">4</span>;
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search_by</span>(<span class="op">|</span><span class="ident">probe</span><span class="op">|</span> <span class="ident">probe</span>.<span class="ident">cmp</span>(<span class="kw-2">&</span><span class="ident">seek</span>)), <span class="prelude-val">Err</span>(<span class="number">7</span>));
|
|||
|
<span class="kw">let</span> <span class="ident">seek</span> <span class="op">=</span> <span class="number">100</span>;
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search_by</span>(<span class="op">|</span><span class="ident">probe</span><span class="op">|</span> <span class="ident">probe</span>.<span class="ident">cmp</span>(<span class="kw-2">&</span><span class="ident">seek</span>)), <span class="prelude-val">Err</span>(<span class="number">13</span>));
|
|||
|
<span class="kw">let</span> <span class="ident">seek</span> <span class="op">=</span> <span class="number">1</span>;
|
|||
|
<span class="kw">let</span> <span class="ident">r</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">binary_search_by</span>(<span class="op">|</span><span class="ident">probe</span><span class="op">|</span> <span class="ident">probe</span>.<span class="ident">cmp</span>(<span class="kw-2">&</span><span class="ident">seek</span>));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="kw">match</span> <span class="ident">r</span> { <span class="prelude-val">Ok</span>(<span class="number">1</span>...<span class="number">4</span>) <span class="op">=></span> <span class="bool-val">true</span>, _ <span class="op">=></span> <span class="bool-val">false</span>, });</pre>
|
|||
|
</div><h4 id='method.binary_search_by_key' class="method"><span id='binary_search_by_key.v' class='invisible'><code>fn <a href='#method.binary_search_by_key' class='fnname'>binary_search_by_key</a><'a, B, F>(&'a self, b: &B, f: F) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>> <span class="where fmt-newline">where<br> B: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&'a T) -> B, </span></code><div class='since' title='Stable since Rust version 1.10.0'>1.10.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Binary searches this sorted slice with a key extraction function.</p>
|
|||
|
|
|||
|
<p>Assumes that the slice is sorted by the key, for instance with
|
|||
|
<a href="#method.sort_by_key"><code>sort_by_key</code></a> using the same key extraction function.</p>
|
|||
|
|
|||
|
<p>If a matching value is found then returns <code>Ok</code>, containing the
|
|||
|
index for the matched element; if no match is found then <code>Err</code>
|
|||
|
is returned, containing the index where a matching element could
|
|||
|
be inserted while maintaining sorted order.</p>
|
|||
|
|
|||
|
<h1 id='examples-31' class='section-header'><a href='#examples-31'>Examples</a></h1>
|
|||
|
<p>Looks up a series of four elements in a slice of pairs sorted by
|
|||
|
their second elements. The first is found, with a uniquely
|
|||
|
determined position; the second and third are not found; the
|
|||
|
fourth could match any position in <code>[1, 4]</code>.</p>
|
|||
|
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="ident">s</span> <span class="op">=</span> [(<span class="number">0</span>, <span class="number">0</span>), (<span class="number">2</span>, <span class="number">1</span>), (<span class="number">4</span>, <span class="number">1</span>), (<span class="number">5</span>, <span class="number">1</span>), (<span class="number">3</span>, <span class="number">1</span>),
|
|||
|
(<span class="number">1</span>, <span class="number">2</span>), (<span class="number">2</span>, <span class="number">3</span>), (<span class="number">4</span>, <span class="number">5</span>), (<span class="number">5</span>, <span class="number">8</span>), (<span class="number">3</span>, <span class="number">13</span>),
|
|||
|
(<span class="number">1</span>, <span class="number">21</span>), (<span class="number">2</span>, <span class="number">34</span>), (<span class="number">4</span>, <span class="number">55</span>)];
|
|||
|
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search_by_key</span>(<span class="kw-2">&</span><span class="number">13</span>, <span class="op">|</span><span class="kw-2">&</span>(<span class="ident">a</span>,<span class="ident">b</span>)<span class="op">|</span> <span class="ident">b</span>), <span class="prelude-val">Ok</span>(<span class="number">9</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search_by_key</span>(<span class="kw-2">&</span><span class="number">4</span>, <span class="op">|</span><span class="kw-2">&</span>(<span class="ident">a</span>,<span class="ident">b</span>)<span class="op">|</span> <span class="ident">b</span>), <span class="prelude-val">Err</span>(<span class="number">7</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">s</span>.<span class="ident">binary_search_by_key</span>(<span class="kw-2">&</span><span class="number">100</span>, <span class="op">|</span><span class="kw-2">&</span>(<span class="ident">a</span>,<span class="ident">b</span>)<span class="op">|</span> <span class="ident">b</span>), <span class="prelude-val">Err</span>(<span class="number">13</span>));
|
|||
|
<span class="kw">let</span> <span class="ident">r</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">binary_search_by_key</span>(<span class="kw-2">&</span><span class="number">1</span>, <span class="op">|</span><span class="kw-2">&</span>(<span class="ident">a</span>,<span class="ident">b</span>)<span class="op">|</span> <span class="ident">b</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="kw">match</span> <span class="ident">r</span> { <span class="prelude-val">Ok</span>(<span class="number">1</span>...<span class="number">4</span>) <span class="op">=></span> <span class="bool-val">true</span>, _ <span class="op">=></span> <span class="bool-val">false</span>, });</pre>
|
|||
|
</div><h4 id='method.sort' class="method"><span id='sort.v' class='invisible'><code>fn <a href='#method.sort' class='fnname'>sort</a>(&mut self) <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Sorts the slice.</p>
|
|||
|
|
|||
|
<p>This sort is stable (i.e. does not reorder equal elements) and <code>O(n log n)</code> worst-case.</p>
|
|||
|
|
|||
|
<h1 id='current-implementation' class='section-header'><a href='#current-implementation'>Current implementation</a></h1>
|
|||
|
<p>The current algorithm is an adaptive, iterative merge sort inspired by
|
|||
|
<a href="https://en.wikipedia.org/wiki/Timsort">timsort</a>.
|
|||
|
It is designed to be very fast in cases where the slice is nearly sorted, or consists of
|
|||
|
two or more sorted sequences concatenated one after another.</p>
|
|||
|
|
|||
|
<p>Also, it allocates temporary storage half the size of <code>self</code>, but for short slices a
|
|||
|
non-allocating insertion sort is used instead.</p>
|
|||
|
|
|||
|
<h1 id='examples-32' class='section-header'><a href='#examples-32'>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="op">-</span><span class="number">5</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="ident">v</span>.<span class="ident">sort</span>();
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="op">-</span><span class="number">5</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>]);</pre>
|
|||
|
</div><h4 id='method.sort_by' class="method"><span id='sort_by.v' class='invisible'><code>fn <a href='#method.sort_by' class='fnname'>sort_by</a><F>(&mut self, compare: F) <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T, &T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/cmp/enum.Ordering.html" title="enum core::cmp::Ordering">Ordering</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Sorts the slice with a comparator function.</p>
|
|||
|
|
|||
|
<p>This sort is stable (i.e. does not reorder equal elements) and <code>O(n log n)</code> worst-case.</p>
|
|||
|
|
|||
|
<h1 id='current-implementation-1' class='section-header'><a href='#current-implementation-1'>Current implementation</a></h1>
|
|||
|
<p>The current algorithm is an adaptive, iterative merge sort inspired by
|
|||
|
<a href="https://en.wikipedia.org/wiki/Timsort">timsort</a>.
|
|||
|
It is designed to be very fast in cases where the slice is nearly sorted, or consists of
|
|||
|
two or more sorted sequences concatenated one after another.</p>
|
|||
|
|
|||
|
<p>Also, it allocates temporary storage half the size of <code>self</code>, but for short slices a
|
|||
|
non-allocating insertion sort is used instead.</p>
|
|||
|
|
|||
|
<h1 id='examples-33' class='section-header'><a href='#examples-33'>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="number">5</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>];
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_by</span>(<span class="op">|</span><span class="ident">a</span>, <span class="ident">b</span><span class="op">|</span> <span class="ident">a</span>.<span class="ident">cmp</span>(<span class="ident">b</span>));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);
|
|||
|
|
|||
|
<span class="comment">// reverse sorting</span>
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_by</span>(<span class="op">|</span><span class="ident">a</span>, <span class="ident">b</span><span class="op">|</span> <span class="ident">b</span>.<span class="ident">cmp</span>(<span class="ident">a</span>));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>]);</pre>
|
|||
|
</div><h4 id='method.sort_by_key' class="method"><span id='sort_by_key.v' class='invisible'><code>fn <a href='#method.sort_by_key' class='fnname'>sort_by_key</a><B, F>(&mut self, f: F) <span class="where fmt-newline">where<br> B: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> B, </span></code><div class='since' title='Stable since Rust version 1.7.0'>1.7.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Sorts the slice with a key extraction function.</p>
|
|||
|
|
|||
|
<p>This sort is stable (i.e. does not reorder equal elements) and <code>O(n log n)</code> worst-case.</p>
|
|||
|
|
|||
|
<h1 id='current-implementation-2' class='section-header'><a href='#current-implementation-2'>Current implementation</a></h1>
|
|||
|
<p>The current algorithm is an adaptive, iterative merge sort inspired by
|
|||
|
<a href="https://en.wikipedia.org/wiki/Timsort">timsort</a>.
|
|||
|
It is designed to be very fast in cases where the slice is nearly sorted, or consists of
|
|||
|
two or more sorted sequences concatenated one after another.</p>
|
|||
|
|
|||
|
<p>Also, it allocates temporary storage half the size of <code>self</code>, but for short slices a
|
|||
|
non-allocating insertion sort is used instead.</p>
|
|||
|
|
|||
|
<h1 id='examples-34' class='section-header'><a href='#examples-34'>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="op">-</span><span class="number">5i32</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_by_key</span>(<span class="op">|</span><span class="ident">k</span><span class="op">|</span> <span class="ident">k</span>.<span class="ident">abs</span>());
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">4</span>, <span class="op">-</span><span class="number">5</span>]);</pre>
|
|||
|
</div><h4 id='method.sort_unstable' class="method"><span id='sort_unstable.v' class='invisible'><code>fn <a href='#method.sort_unstable' class='fnname'>sort_unstable</a>(&mut self) <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a>, </span></code></span></h4>
|
|||
|
<div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>sort_unstable</code>)</div></div><div class='docblock'><p>Sorts the slice, but may not preserve the order of equal elements.</p>
|
|||
|
|
|||
|
<p>This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
|
|||
|
and <code>O(n log n)</code> worst-case.</p>
|
|||
|
|
|||
|
<h1 id='current-implementation-3' class='section-header'><a href='#current-implementation-3'>Current implementation</a></h1>
|
|||
|
<p>The current algorithm is based on Orson Peters' <a href="https://github.com/orlp/pdqsort">pattern-defeating quicksort</a>,
|
|||
|
which is a quicksort variant designed to be very fast on certain kinds of patterns,
|
|||
|
sometimes achieving linear time. It is randomized but deterministic, and falls back to
|
|||
|
heapsort on degenerate inputs.</p>
|
|||
|
|
|||
|
<p>It is generally faster than stable sorting, except in a few special cases, e.g. when the
|
|||
|
slice consists of several concatenated sorted sequences.</p>
|
|||
|
|
|||
|
<h1 id='examples-35' class='section-header'><a href='#examples-35'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#<span class="op">!</span>[<span class="ident">feature</span>(<span class="ident">sort_unstable</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> [<span class="op">-</span><span class="number">5</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_unstable</span>();
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="op">-</span><span class="number">5</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>]);</pre>
|
|||
|
</div><h4 id='method.sort_unstable_by' class="method"><span id='sort_unstable_by.v' class='invisible'><code>fn <a href='#method.sort_unstable_by' class='fnname'>sort_unstable_by</a><F>(&mut self, compare: F) <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T, &T) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/cmp/enum.Ordering.html" title="enum core::cmp::Ordering">Ordering</a>, </span></code></span></h4>
|
|||
|
<div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>sort_unstable</code>)</div></div><div class='docblock'><p>Sorts the slice with a comparator function, but may not preserve the order of equal
|
|||
|
elements.</p>
|
|||
|
|
|||
|
<p>This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
|
|||
|
and <code>O(n log n)</code> worst-case.</p>
|
|||
|
|
|||
|
<h1 id='current-implementation-4' class='section-header'><a href='#current-implementation-4'>Current implementation</a></h1>
|
|||
|
<p>The current algorithm is based on Orson Peters' <a href="https://github.com/orlp/pdqsort">pattern-defeating quicksort</a>,
|
|||
|
which is a quicksort variant designed to be very fast on certain kinds of patterns,
|
|||
|
sometimes achieving linear time. It is randomized but deterministic, and falls back to
|
|||
|
heapsort on degenerate inputs.</p>
|
|||
|
|
|||
|
<p>It is generally faster than stable sorting, except in a few special cases, e.g. when the
|
|||
|
slice consists of several concatenated sorted sequences.</p>
|
|||
|
|
|||
|
<h1 id='examples-36' class='section-header'><a href='#examples-36'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#<span class="op">!</span>[<span class="ident">feature</span>(<span class="ident">sort_unstable</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> [<span class="number">5</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>];
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_unstable_by</span>(<span class="op">|</span><span class="ident">a</span>, <span class="ident">b</span><span class="op">|</span> <span class="ident">a</span>.<span class="ident">cmp</span>(<span class="ident">b</span>));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);
|
|||
|
|
|||
|
<span class="comment">// reverse sorting</span>
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_unstable_by</span>(<span class="op">|</span><span class="ident">a</span>, <span class="ident">b</span><span class="op">|</span> <span class="ident">b</span>.<span class="ident">cmp</span>(<span class="ident">a</span>));
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>]);</pre>
|
|||
|
</div><h4 id='method.sort_unstable_by_key' class="method"><span id='sort_unstable_by_key.v' class='invisible'><code>fn <a href='#method.sort_unstable_by_key' class='fnname'>sort_unstable_by_key</a><B, F>(&mut self, f: F) <span class="where fmt-newline">where<br> B: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&T) -> B, </span></code></span></h4>
|
|||
|
<div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>sort_unstable</code>)</div></div><div class='docblock'><p>Sorts the slice with a key extraction function, but may not preserve the order of equal
|
|||
|
elements.</p>
|
|||
|
|
|||
|
<p>This sort is unstable (i.e. may reorder equal elements), in-place (i.e. does not allocate),
|
|||
|
and <code>O(n log n)</code> worst-case.</p>
|
|||
|
|
|||
|
<h1 id='current-implementation-5' class='section-header'><a href='#current-implementation-5'>Current implementation</a></h1>
|
|||
|
<p>The current algorithm is based on Orson Peters' <a href="https://github.com/orlp/pdqsort">pattern-defeating quicksort</a>,
|
|||
|
which is a quicksort variant designed to be very fast on certain kinds of patterns,
|
|||
|
sometimes achieving linear time. It is randomized but deterministic, and falls back to
|
|||
|
heapsort on degenerate inputs.</p>
|
|||
|
|
|||
|
<p>It is generally faster than stable sorting, except in a few special cases, e.g. when the
|
|||
|
slice consists of several concatenated sorted sequences.</p>
|
|||
|
|
|||
|
<h1 id='examples-37' class='section-header'><a href='#examples-37'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#<span class="op">!</span>[<span class="ident">feature</span>(<span class="ident">sort_unstable</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> [<span class="op">-</span><span class="number">5i32</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">2</span>];
|
|||
|
|
|||
|
<span class="ident">v</span>.<span class="ident">sort_unstable_by_key</span>(<span class="op">|</span><span class="ident">k</span><span class="op">|</span> <span class="ident">k</span>.<span class="ident">abs</span>());
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">v</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="op">-</span><span class="number">3</span>, <span class="number">4</span>, <span class="op">-</span><span class="number">5</span>]);</pre>
|
|||
|
</div><h4 id='method.rotate' class="method"><span id='rotate.v' class='invisible'><code>fn <a href='#method.rotate' class='fnname'>rotate</a>(&mut self, mid: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>)</code></span></h4>
|
|||
|
<div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API. (<code>slice_rotate</code>)</div></div><div class='docblock'><p>Permutes the slice in-place such that <code>self[mid..]</code> moves to the
|
|||
|
beginning of the slice while <code>self[..mid]</code> moves to the end of the
|
|||
|
slice. Equivalently, rotates the slice <code>mid</code> places to the left
|
|||
|
or <code>k = self.len() - mid</code> places to the right.</p>
|
|||
|
|
|||
|
<p>This is a "k-rotation", a permutation in which item <code>i</code> moves to
|
|||
|
position <code>i + k</code>, modulo the length of the slice. See <em>Elements
|
|||
|
of Programming</em> <a href="https://books.google.com/books?id=CO9ULZGINlsC&pg=PA178&q=k-rotation">§10.4</a>.</p>
|
|||
|
|
|||
|
<p>Rotation by <code>mid</code> and rotation by <code>k</code> are inverse operations.</p>
|
|||
|
|
|||
|
<h1 id='panics-6' class='section-header'><a href='#panics-6'>Panics</a></h1>
|
|||
|
<p>This function will panic if <code>mid</code> is greater than the length of the
|
|||
|
slice. (Note that <code>mid == self.len()</code> does <em>not</em> panic; it's a nop
|
|||
|
rotation with <code>k == 0</code>, the inverse of a rotation with <code>mid == 0</code>.)</p>
|
|||
|
|
|||
|
<h1 id='complexity' class='section-header'><a href='#complexity'>Complexity</a></h1>
|
|||
|
<p>Takes linear (in <code>self.len()</code>) time.</p>
|
|||
|
|
|||
|
<h1 id='examples-38' class='section-header'><a href='#examples-38'>Examples</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="attribute">#<span class="op">!</span>[<span class="ident">feature</span>(<span class="ident">slice_rotate</span>)]</span>
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">a</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>];
|
|||
|
<span class="kw">let</span> <span class="ident">mid</span> <span class="op">=</span> <span class="number">2</span>;
|
|||
|
<span class="ident">a</span>.<span class="ident">rotate</span>(<span class="ident">mid</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="ident">a</span>, <span class="kw-2">&</span>[<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">2</span>]);
|
|||
|
<span class="kw">let</span> <span class="ident">k</span> <span class="op">=</span> <span class="ident">a</span>.<span class="ident">len</span>() <span class="op">-</span> <span class="ident">mid</span>;
|
|||
|
<span class="ident">a</span>.<span class="ident">rotate</span>(<span class="ident">k</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="ident">a</span>, <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]);
|
|||
|
|
|||
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ops</span>::<span class="ident">Range</span>;
|
|||
|
<span class="kw">fn</span> <span class="ident">slide</span><span class="op"><</span><span class="ident">T</span><span class="op">></span>(<span class="ident">slice</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> [<span class="ident">T</span>], <span class="ident">range</span>: <span class="ident">Range</span><span class="op"><</span><span class="ident">usize</span><span class="op">></span>, <span class="ident">to</span>: <span class="ident">usize</span>) {
|
|||
|
<span class="kw">if</span> <span class="ident">to</span> <span class="op"><</span> <span class="ident">range</span>.<span class="ident">start</span> {
|
|||
|
<span class="ident">slice</span>[<span class="ident">to</span>..<span class="ident">range</span>.<span class="ident">end</span>].<span class="ident">rotate</span>(<span class="ident">range</span>.<span class="ident">start</span><span class="op">-</span><span class="ident">to</span>);
|
|||
|
} <span class="kw">else</span> <span class="kw">if</span> <span class="ident">to</span> <span class="op">></span> <span class="ident">range</span>.<span class="ident">end</span> {
|
|||
|
<span class="ident">slice</span>[<span class="ident">range</span>.<span class="ident">start</span>..<span class="ident">to</span>].<span class="ident">rotate</span>(<span class="ident">range</span>.<span class="ident">end</span><span class="op">-</span><span class="ident">range</span>.<span class="ident">start</span>);
|
|||
|
}
|
|||
|
}
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</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="number">0</span>..<span class="number">10</span>).<span class="ident">collect</span>();
|
|||
|
<span class="ident">slide</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">v</span>, <span class="number">1</span>..<span class="number">4</span>, <span class="number">7</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="ident">v</span>, <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]);
|
|||
|
<span class="ident">slide</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">v</span>, <span class="number">6</span>..<span class="number">8</span>, <span class="number">1</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&</span><span class="ident">v</span>, <span class="kw-2">&</span>[<span class="number">0</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">9</span>]);</pre>
|
|||
|
</div><h4 id='method.clone_from_slice' class="method"><span id='clone_from_slice.v' class='invisible'><code>fn <a href='#method.clone_from_slice' class='fnname'>clone_from_slice</a>(&mut self, src: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a>) <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>, </span></code><div class='since' title='Stable since Rust version 1.7.0'>1.7.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Copies the elements from <code>src</code> into <code>self</code>.</p>
|
|||
|
|
|||
|
<p>The length of <code>src</code> must be the same as <code>self</code>.</p>
|
|||
|
|
|||
|
<p>If <code>src</code> implements <code>Copy</code>, it can be more performant to use
|
|||
|
<a href="#method.copy_from_slice"><code>copy_from_slice</code></a>.</p>
|
|||
|
|
|||
|
<h1 id='panics-7' class='section-header'><a href='#panics-7'>Panics</a></h1>
|
|||
|
<p>This function will panic if the two slices have different lengths.</p>
|
|||
|
|
|||
|
<h1 id='example-7' class='section-header'><a href='#example-7'>Example</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">dst</span> <span class="op">=</span> [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>];
|
|||
|
<span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
|
|||
|
|
|||
|
<span class="ident">dst</span>.<span class="ident">clone_from_slice</span>(<span class="kw-2">&</span><span class="ident">src</span>);
|
|||
|
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">dst</span> <span class="op">==</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</pre>
|
|||
|
</div><h4 id='method.copy_from_slice' class="method"><span id='copy_from_slice.v' class='invisible'><code>fn <a href='#method.copy_from_slice' class='fnname'>copy_from_slice</a>(&mut self, src: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[T]</a>) <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Copy.html" title="trait core::marker::Copy">Copy</a>, </span></code><div class='since' title='Stable since Rust version 1.9.0'>1.9.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Copies all elements from <code>src</code> into <code>self</code>, using a memcpy.</p>
|
|||
|
|
|||
|
<p>The length of <code>src</code> must be the same as <code>self</code>.</p>
|
|||
|
|
|||
|
<p>If <code>src</code> does not implement <code>Copy</code>, use <a href="#method.clone_from_slice"><code>clone_from_slice</code></a>.</p>
|
|||
|
|
|||
|
<h1 id='panics-8' class='section-header'><a href='#panics-8'>Panics</a></h1>
|
|||
|
<p>This function will panic if the two slices have different lengths.</p>
|
|||
|
|
|||
|
<h1 id='example-8' class='section-header'><a href='#example-8'>Example</a></h1>
|
|||
|
<pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">dst</span> <span class="op">=</span> [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>];
|
|||
|
<span class="kw">let</span> <span class="ident">src</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
|
|||
|
|
|||
|
<span class="ident">dst</span>.<span class="ident">copy_from_slice</span>(<span class="kw-2">&</span><span class="ident">src</span>);
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">src</span>, <span class="ident">dst</span>);</pre>
|
|||
|
</div><h4 id='method.to_vec' class="method"><span id='to_vec.v' class='invisible'><code>fn <a href='#method.to_vec' class='fnname'>to_vec</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><T> <span class="where fmt-newline">where<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>, </span></code><div class='since' title='Stable since Rust version 1.0.0'>1.0.0</div></span></h4>
|
|||
|
<div class='docblock'><p>Copies <code>self</code> into a new <code>Vec</code>.</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">s</span> <span class="op">=</span> [<span class="number">10</span>, <span class="number">40</span>, <span class="number">30</span>];
|
|||
|
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">s</span>.<span class="ident">to_vec</span>();
|
|||
|
<span class="comment">// Here, `s` and `x` can be modified independently.</span></pre>
|
|||
|
</div></div><h2 id='implementations'>Trait Implementations</h2><h3 class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="trait core::ops::drop::Drop">Drop</a> for <a class="struct" href="../cairo/struct.ImageSurfaceData.html" title="struct cairo::ImageSurfaceData">ImageSurfaceData</a><'a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../src/cairo/image_surface.rs.html#159-165' title='goto source code'>[src]</a></span></h3>
|
|||
|
<div class='impl-items'><h4 id='method.drop' class="method"><span id='drop.v' class='invisible'><code>fn <a href='https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop' class='fnname'>drop</a>(&mut self)</code></span></h4>
|
|||
|
<div class='docblock'><p>A method called when the value goes out of scope. <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop">Read more</a></p>
|
|||
|
</div></div><h3 class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html" title="trait core::ops::deref::Deref">Deref</a> for <a class="struct" href="../cairo/struct.ImageSurfaceData.html" title="struct cairo::ImageSurfaceData">ImageSurfaceData</a><'a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../src/cairo/image_surface.rs.html#167-173' 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='https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html#associatedtype.Target' class="type">Target</a> = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</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='https://doc.rust-lang.org/nightly/core/ops/deref/trait.Deref.html#tymethod.deref' class='fnname'>deref</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&[</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a></code></span></h4>
|
|||
|
<div class='docblock'><p>The method called to dereference a value</p>
|
|||
|
</div></div><h3 class='impl'><span class='in-band'><code>impl<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/deref/trait.DerefMut.html" title="trait core::ops::deref::DerefMut">DerefMut</a> for <a class="struct" href="../cairo/struct.ImageSurfaceData.html" title="struct cairo::ImageSurfaceData">ImageSurfaceData</a><'a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../src/cairo/image_surface.rs.html#175-180' 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='https://doc.rust-lang.org/nightly/core/ops/deref/trait.DerefMut.html#tymethod.deref_mut' class='fnname'>deref_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a></code></span></h4>
|
|||
|
<div class='docblock'><p>The method called to mutably dereference a value</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>?</dt>
|
|||
|
<dd>Show this help dialog</dd>
|
|||
|
<dt>S</dt>
|
|||
|
<dd>Focus the search field</dd>
|
|||
|
<dt>⇤</dt>
|
|||
|
<dd>Move up in search results</dd>
|
|||
|
<dt>⇥</dt>
|
|||
|
<dd>Move down in search results</dd>
|
|||
|
<dt>⏎</dt>
|
|||
|
<dd>Go to active search result</dd>
|
|||
|
<dt>+</dt>
|
|||
|
<dd>Collapse/expand 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 = "cairo";
|
|||
|
</script>
|
|||
|
<script src="../main.js"></script>
|
|||
|
<script defer src="../search-index.js"></script>
|
|||
|
</body>
|
|||
|
</html>
|