mirror of
https://github.com/Dhghomon/easy_rust
synced 2024-11-11 07:11:02 +00:00
deploy: 2689fe0060
This commit is contained in:
parent
337755e49b
commit
80fe56a2f4
@ -137,9 +137,8 @@
|
|||||||
<h2 id="a-tour-of-the-standard-library"><a class="header" href="#a-tour-of-the-standard-library">A tour of the standard library</a></h2>
|
<h2 id="a-tour-of-the-standard-library"><a class="header" href="#a-tour-of-the-standard-library">A tour of the standard library</a></h2>
|
||||||
<p>Now that you know a lot of Rust, you will be able to understand most things inside the standard library. The code inside it isn't so scary anymore. Let's take a look at some of the parts in it that we haven't learned yet. This tour will go over most parts of the standard library that you don't need to install Rust for. We will revisit a lot of items we already know so we can learn them with greater understanding.</p>
|
<p>Now that you know a lot of Rust, you will be able to understand most things inside the standard library. The code inside it isn't so scary anymore. Let's take a look at some of the parts in it that we haven't learned yet. This tour will go over most parts of the standard library that you don't need to install Rust for. We will revisit a lot of items we already know so we can learn them with greater understanding.</p>
|
||||||
<h3 id="arrays"><a class="header" href="#arrays">Arrays</a></h3>
|
<h3 id="arrays"><a class="header" href="#arrays">Arrays</a></h3>
|
||||||
<p>One thing about arrays to note is that they don't implement <code>Iterator.</code>. That means that if you have an array, you can't use <code>for</code>. But you can use methods like <code>.iter()</code> on them. Or you can use <code>&</code> to get a slice. Actually, the compiler will tell you exactly that if you try to use <code>for</code>:</p>
|
<p>In the past (before Rust 1.53), arrays didn't implement <code>Iterator</code> and you needed to use methods like <code>.iter()</code> on them in for <code>loops</code>. (People also used <code>&</code> to get a slice in <code>for</code> loops). So this didn't work in the past:</p>
|
||||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||||
// ⚠️
|
|
||||||
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
||||||
|
|
||||||
for city in my_cities {
|
for city in my_cities {
|
||||||
@ -147,16 +146,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre></pre>
|
</code></pre></pre>
|
||||||
<p>The message is:</p>
|
<p>The compiler used to give this message:</p>
|
||||||
<pre><code class="language-text">error[E0277]: `[&str; 3]` is not an iterator
|
<pre><code class="language-text">error[E0277]: `[&str; 3]` is not an iterator
|
||||||
--> src\main.rs:5:17
|
--> src\main.rs:5:17
|
||||||
|
|
|
|
||||||
| ^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
| ^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>So let's try both. They give the same result.</p>
|
<p>Luckily, that isn't a problem anymore! So all three of these work:</p>
|
||||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||||
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
||||||
|
|
||||||
|
for city in my_cities {
|
||||||
|
println!("{}", city);
|
||||||
|
}
|
||||||
for city in &my_cities {
|
for city in &my_cities {
|
||||||
println!("{}", city);
|
println!("{}", city);
|
||||||
}
|
}
|
||||||
@ -172,6 +174,9 @@ Nicosia
|
|||||||
Beirut
|
Beirut
|
||||||
Tel Aviv
|
Tel Aviv
|
||||||
Nicosia
|
Nicosia
|
||||||
|
Beirut
|
||||||
|
Tel Aviv
|
||||||
|
Nicosia
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>If you want to get variables from an array, you can put their names inside <code>[]</code> to destructure it. This is the same as using a tuple in <code>match</code> statements or to get variables from a struct.</p>
|
<p>If you want to get variables from an array, you can put their names inside <code>[]</code> to destructure it. This is the same as using a tuple in <code>match</code> statements or to get variables from a struct.</p>
|
||||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||||
|
@ -12,6 +12,7 @@ html {
|
|||||||
color: var(--fg);
|
color: var(--fg);
|
||||||
background-color: var(--bg);
|
background-color: var(--bg);
|
||||||
text-size-adjust: none;
|
text-size-adjust: none;
|
||||||
|
-webkit-text-size-adjust: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
|
13
print.html
13
print.html
@ -9762,9 +9762,8 @@ struct Point {
|
|||||||
<div style="break-before: page; page-break-before: always;"></div><h2 id="a-tour-of-the-standard-library"><a class="header" href="#a-tour-of-the-standard-library">A tour of the standard library</a></h2>
|
<div style="break-before: page; page-break-before: always;"></div><h2 id="a-tour-of-the-standard-library"><a class="header" href="#a-tour-of-the-standard-library">A tour of the standard library</a></h2>
|
||||||
<p>Now that you know a lot of Rust, you will be able to understand most things inside the standard library. The code inside it isn't so scary anymore. Let's take a look at some of the parts in it that we haven't learned yet. This tour will go over most parts of the standard library that you don't need to install Rust for. We will revisit a lot of items we already know so we can learn them with greater understanding.</p>
|
<p>Now that you know a lot of Rust, you will be able to understand most things inside the standard library. The code inside it isn't so scary anymore. Let's take a look at some of the parts in it that we haven't learned yet. This tour will go over most parts of the standard library that you don't need to install Rust for. We will revisit a lot of items we already know so we can learn them with greater understanding.</p>
|
||||||
<h3 id="arrays-1"><a class="header" href="#arrays-1">Arrays</a></h3>
|
<h3 id="arrays-1"><a class="header" href="#arrays-1">Arrays</a></h3>
|
||||||
<p>One thing about arrays to note is that they don't implement <code>Iterator.</code>. That means that if you have an array, you can't use <code>for</code>. But you can use methods like <code>.iter()</code> on them. Or you can use <code>&</code> to get a slice. Actually, the compiler will tell you exactly that if you try to use <code>for</code>:</p>
|
<p>In the past (before Rust 1.53), arrays didn't implement <code>Iterator</code> and you needed to use methods like <code>.iter()</code> on them in for <code>loops</code>. (People also used <code>&</code> to get a slice in <code>for</code> loops). So this didn't work in the past:</p>
|
||||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||||
// ⚠️
|
|
||||||
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
||||||
|
|
||||||
for city in my_cities {
|
for city in my_cities {
|
||||||
@ -9772,16 +9771,19 @@ struct Point {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre></pre>
|
</code></pre></pre>
|
||||||
<p>The message is:</p>
|
<p>The compiler used to give this message:</p>
|
||||||
<pre><code class="language-text">error[E0277]: `[&str; 3]` is not an iterator
|
<pre><code class="language-text">error[E0277]: `[&str; 3]` is not an iterator
|
||||||
--> src\main.rs:5:17
|
--> src\main.rs:5:17
|
||||||
|
|
|
|
||||||
| ^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
| ^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>So let's try both. They give the same result.</p>
|
<p>Luckily, that isn't a problem anymore! So all three of these work:</p>
|
||||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||||
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
let my_cities = ["Beirut", "Tel Aviv", "Nicosia"];
|
||||||
|
|
||||||
|
for city in my_cities {
|
||||||
|
println!("{}", city);
|
||||||
|
}
|
||||||
for city in &my_cities {
|
for city in &my_cities {
|
||||||
println!("{}", city);
|
println!("{}", city);
|
||||||
}
|
}
|
||||||
@ -9797,6 +9799,9 @@ Nicosia
|
|||||||
Beirut
|
Beirut
|
||||||
Tel Aviv
|
Tel Aviv
|
||||||
Nicosia
|
Nicosia
|
||||||
|
Beirut
|
||||||
|
Tel Aviv
|
||||||
|
Nicosia
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>If you want to get variables from an array, you can put their names inside <code>[]</code> to destructure it. This is the same as using a tuple in <code>match</code> statements or to get variables from a struct.</p>
|
<p>If you want to get variables from an array, you can put their names inside <code>[]</code> to destructure it. This is the same as using a tuple in <code>match</code> statements or to get variables from a struct.</p>
|
||||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user