gh-pages
Dhghomon 3 years ago
parent 9cf051e1d0
commit 173feb9493

@ -164,7 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#display-and-debug" id="display-and-debug">Display and debug</a></h2>
<p><a href="https://youtu.be/jd3pC248c0o">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/jd3pC248c0o">See this chapter on YouTube</a></strong></p>
<p>Simple variables in Rust can be printed with <code>{}</code> inside <code>println!</code>. But some variables can't, and you need to <strong>debug print</strong>. Debug print is printing for the programmer, because it usually shows more information. Debug sometimes doesn't look pretty, because it has extra information to help you.</p>
<p>How do you know if you need <code>{:?}</code> and not <code>{}</code>? The compiler will tell you. For example:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {

@ -164,7 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#mutability-changing" id="mutability-changing">Mutability (changing)</a></h2>
<p><a href="https://youtu.be/Nyyd6qn7dZY">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/Nyyd6qn7dZY">See this chapter on YouTube</a></strong></p>
<p>When you declare a variable with <code>let</code>, it is immutable (cannot be changed).</p>
<p>This will not work:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
@ -188,7 +188,7 @@
</code></pre></pre>
<p>You will see the same &quot;expected&quot; message from the compiler: <code>expected integer, found &amp;str</code>. <code>&amp;str</code> is a string type that we will learn soon.</p>
<h3><a class="header" href="#shadowing" id="shadowing">Shadowing</a></h3>
<p><a href="https://youtu.be/InULHyRGw7g">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/InULHyRGw7g">See this chapter on YouTube</a></strong></p>
<p>Shadowing means using <code>let</code> to declare a new variable with the same name as another variable. It looks like mutability, but it is completely different. Shadowing looks like this:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
let my_number = 8; // This is an i32

@ -164,6 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#strings" id="strings">Strings</a></h2>
<p><strong><a href="https://youtu.be/pSyaGzGg26o">See this chapter on YouTube</a></strong></p>
<p>Rust has two main types of strings: <code>String</code> and <code>&amp;str</code>. What is the difference?</p>
<ul>
<li><code>&amp;str</code> is a simple string. When you write <code>let my_variable = &quot;Hello, world!&quot;</code>, you create a <code>&amp;str</code>. A <code>&amp;str</code> is very fast.</li>

@ -164,6 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#const-and-static" id="const-and-static">const and static</a></h2>
<p><strong><a href="https://youtu.be/Ky3HqkWUcI0">See this chapter on YouTube</a></strong></p>
<p>There are two types that don't use <code>let</code> to declare: <code>const</code> and <code>static</code>. Also, Rust won't use type inference: you need to write the type for them. These are for variables that don't change (<code>const</code> means constant). The difference is that:</p>
<ul>
<li><code>const</code> is a value that does not change,</li>

@ -164,6 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#more-on-references" id="more-on-references">More on references</a></h2>
<p><strong><a href="https://youtu.be/R13sQ8SNoEQ">See this chapter on YouTube</a></strong></p>
<p>References are very important in Rust. Rust uses references to make sure that all memory access is safe. We know that we use <code>&amp;</code> to create a reference:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
let country = String::from(&quot;Austria&quot;);

@ -164,7 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#rust-playground" id="rust-playground">Rust Playground</a></h2>
<p><a href="https://youtu.be/-lYeJeQ11OI">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/-lYeJeQ11OI">See this chapter on YouTube</a></strong></p>
<p>Maybe you don't want to install Rust yet, and that's okay. You can go to <a href="https://play.rust-lang.org/">https://play.rust-lang.org/</a> and start writing Rust without leaving your browser. You can write your code there and click Run to see the results. You can run most of the samples in this book inside the Playground in your browser. Only near the end you will see samples that go beyond what you can do in the Playground (like opening files).</p>
<p>Here are some tips when using the Rust Playground:</p>
<ul>

@ -164,7 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#comments" id="comments">Comments</a></h2>
<p><a href="https://youtu.be/fJ7jBZG_Rpo">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/fJ7jBZG_Rpo">See this chapter on YouTube</a></strong></p>
<p>Comments are made for programmers to read, not the computer. It's good to write comments to help other people understand your code. It's also good to help you understand your code later. (Many people write good code but then forget why they wrote it.) To write comments in Rust you usually use <code>//</code>:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
// Rust programs start with fn main()

@ -166,7 +166,7 @@
<h2><a class="header" href="#types" id="types">Types</a></h2>
<p>Rust has many types that let you work with numbers, characters, and so on. Some are simple, others are more complicated, and you can even create your own.</p>
<h3><a class="header" href="#primitive-types" id="primitive-types">Primitive types</a></h3>
<p><a href="https://youtu.be/OxTPU5UGMhs">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/OxTPU5UGMhs">See this chapter on YouTube</a></strong></p>
<p>Rust has simple types that are called <strong>primitive types</strong> (primitive = very basic). We will start with integers and <code>char</code> (characters). Integers are whole numbers with no decimal point. There are two types of integers:</p>
<ul>
<li>Signed integers,</li>

@ -164,7 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#type-inference" id="type-inference">Type inference</a></h2>
<p><a href="https://youtu.be/q1D2vpy3kEI">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/q1D2vpy3kEI">See this chapter on YouTube</a></strong></p>
<p>Type inference means that if you don't tell the compiler the type, but it can decide by itself, it will decide. The compiler always needs to know the type of the variables, but you dont always need to tell it. Actually, usually you don't need to tell it. For example, for <code>let my_number = 8</code>, <code>my_number</code> will be an <code>i32</code>. That is because the compiler chooses i32 for integers if you don't tell it. But if you say <code>let my_number: u8 = 8</code>, it will make <code>my_number</code> a <code>u8</code>, because you told it <code>u8</code>.</p>
<p>So usually the compiler can guess. But sometimes you need to tell it, for two reasons:</p>
<ol>

@ -164,7 +164,7 @@
<div id="content" class="content">
<main>
<h2><a class="header" href="#printing-hello-world" id="printing-hello-world">Printing 'hello, world!'</a></h2>
<p>See this chapter on YouTube: <a href="https://youtu.be/yYlPHRl2geQ">Video 1</a>, <a href="https://youtu.be/DTCSfBJJZb8">Video 2</a></p>
<p><strong>See this chapter on YouTube: <a href="https://youtu.be/yYlPHRl2geQ">Video 1</a>, <a href="https://youtu.be/DTCSfBJJZb8">Video 2</a></strong></p>
<p>When you start a new Rust program, it always has this code:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
println!(&quot;Hello, world!&quot;);

@ -365,7 +365,7 @@
<h1><a class="header" href="#part-1---rust-in-your-browser" id="part-1---rust-in-your-browser">Part 1 - Rust in your browser</a></h1>
<p>This book has two parts. In Part 1, you will learn as much Rust as you can just in your browser. You can actually learn almost everything you need to know without installing Rust, so Part 1 is very long. Then at the end is Part 2. It is much shorter, and is about Rust on your computer. That's where you will learn everything else you need to know that you can only do outside of a browser. Some examples are: working with files, taking user input, graphics, and personal settings. Hopefully, by the end of Part 1 you will like Rust enough that you will install it. And if you don't, no problem - Part 1 teaches you so much that you won't mind.</p>
<h2><a class="header" href="#rust-playground" id="rust-playground">Rust Playground</a></h2>
<p><a href="https://youtu.be/-lYeJeQ11OI">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/-lYeJeQ11OI">See this chapter on YouTube</a></strong></p>
<p>Maybe you don't want to install Rust yet, and that's okay. You can go to <a href="https://play.rust-lang.org/">https://play.rust-lang.org/</a> and start writing Rust without leaving your browser. You can write your code there and click Run to see the results. You can run most of the samples in this book inside the Playground in your browser. Only near the end you will see samples that go beyond what you can do in the Playground (like opening files).</p>
<p>Here are some tips when using the Rust Playground:</p>
<ul>
@ -380,7 +380,7 @@
<h2><a class="header" href="#-and-" id="-and-">🚧 and ⚠️</a></h2>
<p>Sometimes the code examples in the book don't work. If an example doesn't work, it will have a 🚧 or a ⚠️ in it. 🚧 is like &quot;under construction&quot;: it means that the code is not complete. Rust needs a <code>fn main()</code> (a main function) to run, but sometimes we just want to look at small pieces of code so it won't have a <code>fn main()</code>. Those examples are correct, but need a <code>fn main()</code> for you to run them. And some code examples show you a problem that we will fix. Those ones might have a <code>fn main()</code> but generate an error, and so they will have a ⚠️.</p>
<h2><a class="header" href="#comments" id="comments">Comments</a></h2>
<p><a href="https://youtu.be/fJ7jBZG_Rpo">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/fJ7jBZG_Rpo">See this chapter on YouTube</a></strong></p>
<p>Comments are made for programmers to read, not the computer. It's good to write comments to help other people understand your code. It's also good to help you understand your code later. (Many people write good code but then forget why they wrote it.) To write comments in Rust you usually use <code>//</code>:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
// Rust programs start with fn main()
@ -411,7 +411,7 @@
<h2><a class="header" href="#types" id="types">Types</a></h2>
<p>Rust has many types that let you work with numbers, characters, and so on. Some are simple, others are more complicated, and you can even create your own.</p>
<h3><a class="header" href="#primitive-types" id="primitive-types">Primitive types</a></h3>
<p><a href="https://youtu.be/OxTPU5UGMhs">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/OxTPU5UGMhs">See this chapter on YouTube</a></strong></p>
<p>Rust has simple types that are called <strong>primitive types</strong> (primitive = very basic). We will start with integers and <code>char</code> (characters). Integers are whole numbers with no decimal point. There are two types of integers:</p>
<ul>
<li>Signed integers,</li>
@ -512,7 +512,7 @@ Slice2 is 7 bytes.
Slice2 is 7 bytes but only 3 characters.
</code></pre>
<h2><a class="header" href="#type-inference" id="type-inference">Type inference</a></h2>
<p><a href="https://youtu.be/q1D2vpy3kEI">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/q1D2vpy3kEI">See this chapter on YouTube</a></strong></p>
<p>Type inference means that if you don't tell the compiler the type, but it can decide by itself, it will decide. The compiler always needs to know the type of the variables, but you dont always need to tell it. Actually, usually you don't need to tell it. For example, for <code>let my_number = 8</code>, <code>my_number</code> will be an <code>i32</code>. That is because the compiler chooses i32 for integers if you don't tell it. But if you say <code>let my_number: u8 = 8</code>, it will make <code>my_number</code> a <code>u8</code>, because you told it <code>u8</code>.</p>
<p>So usually the compiler can guess. But sometimes you need to tell it, for two reasons:</p>
<ol>
@ -599,7 +599,7 @@ Slice2 is 7 bytes but only 3 characters.
}
</code></pre></pre>
<h2><a class="header" href="#printing-hello-world" id="printing-hello-world">Printing 'hello, world!'</a></h2>
<p>See this chapter on YouTube: <a href="https://youtu.be/yYlPHRl2geQ">Video 1</a>, <a href="https://youtu.be/DTCSfBJJZb8">Video 2</a></p>
<p><strong>See this chapter on YouTube: <a href="https://youtu.be/yYlPHRl2geQ">Video 1</a>, <a href="https://youtu.be/DTCSfBJJZb8">Video 2</a></strong></p>
<p>When you start a new Rust program, it always has this code:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
println!(&quot;Hello, world!&quot;);
@ -723,7 +723,7 @@ fn main() {
</code></pre></pre>
<p>So why did we write <code>{:?}</code> and not <code>{}</code>? We will talk about that now.</p>
<h2><a class="header" href="#display-and-debug" id="display-and-debug">Display and debug</a></h2>
<p><a href="https://youtu.be/jd3pC248c0o">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/jd3pC248c0o">See this chapter on YouTube</a></strong></p>
<p>Simple variables in Rust can be printed with <code>{}</code> inside <code>println!</code>. But some variables can't, and you need to <strong>debug print</strong>. Debug print is printing for the programmer, because it usually shows more information. Debug sometimes doesn't look pretty, because it has extra information to help you.</p>
<p>How do you know if you need <code>{:?}</code> and not <code>{}</code>? The compiler will tell you. For example:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
@ -781,7 +781,7 @@ The smallest i128 is -170141183460469231731687303715884105728 and the biggest i1
The smallest u128 is 0 and the biggest u128 is 340282366920938463463374607431768211455.
</code></pre>
<h2><a class="header" href="#mutability-changing" id="mutability-changing">Mutability (changing)</a></h2>
<p><a href="https://youtu.be/Nyyd6qn7dZY">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/Nyyd6qn7dZY">See this chapter on YouTube</a></strong></p>
<p>When you declare a variable with <code>let</code>, it is immutable (cannot be changed).</p>
<p>This will not work:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
@ -805,7 +805,7 @@ The smallest u128 is 0 and the biggest u128 is 340282366920938463463374607431768
</code></pre></pre>
<p>You will see the same &quot;expected&quot; message from the compiler: <code>expected integer, found &amp;str</code>. <code>&amp;str</code> is a string type that we will learn soon.</p>
<h3><a class="header" href="#shadowing" id="shadowing">Shadowing</a></h3>
<p><a href="https://youtu.be/InULHyRGw7g">See this chapter on YouTube</a></p>
<p><strong><a href="https://youtu.be/InULHyRGw7g">See this chapter on YouTube</a></strong></p>
<p>Shadowing means using <code>let</code> to declare a new variable with the same name as another variable. It looks like mutability, but it is completely different. Shadowing looks like this:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
let my_number = 8; // This is an i32
@ -1106,6 +1106,7 @@ but Tokyo is not in Korea.
SEOUL--------------------TOKYO
</code></pre>
<h2><a class="header" href="#strings" id="strings">Strings</a></h2>
<p><strong><a href="https://youtu.be/pSyaGzGg26o">See this chapter on YouTube</a></strong></p>
<p>Rust has two main types of strings: <code>String</code> and <code>&amp;str</code>. What is the difference?</p>
<ul>
<li><code>&amp;str</code> is a simple string. When you write <code>let my_variable = &quot;Hello, world!&quot;</code>, you create a <code>&amp;str</code>. A <code>&amp;str</code> is very fast.</li>
@ -1184,6 +1185,7 @@ And 'Adrian Fahrenheit Țepeș' is 25 bytes. It is not Sized.
</code></pre></pre>
<p>And now you get a String.</p>
<h2><a class="header" href="#const-and-static" id="const-and-static">const and static</a></h2>
<p><strong><a href="https://youtu.be/Ky3HqkWUcI0">See this chapter on YouTube</a></strong></p>
<p>There are two types that don't use <code>let</code> to declare: <code>const</code> and <code>static</code>. Also, Rust won't use type inference: you need to write the type for them. These are for variables that don't change (<code>const</code> means constant). The difference is that:</p>
<ul>
<li><code>const</code> is a value that does not change,</li>
@ -1193,6 +1195,7 @@ And 'Adrian Fahrenheit Țepeș' is 25 bytes. It is not Sized.
<p>You write them with ALL CAPITAL LETTERS, and usually outside of <code>main</code> so that they can live for the whole program.</p>
<p>Two examples are: <code>const NUMBER_OF_MONTHS: u32 = 12;</code> and <code>static SEASONS: [&amp;str; 4] = [&quot;Spring&quot;, &quot;Summer&quot;, &quot;Fall&quot;, &quot;Winter&quot;];</code></p>
<h2><a class="header" href="#more-on-references" id="more-on-references">More on references</a></h2>
<p><strong><a href="https://youtu.be/R13sQ8SNoEQ">See this chapter on YouTube</a></strong></p>
<p>References are very important in Rust. Rust uses references to make sure that all memory access is safe. We know that we use <code>&amp;</code> to create a reference:</p>
<pre><pre class="playground"><code class="language-rust">fn main() {
let country = String::from(&quot;Austria&quot;);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save