448 lines
24 KiB
HTML
448 lines
24 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Control Flow</title>
|
||
</head>
|
||
<body>
|
||
<h2 id="control-flow"><a class="header" href="#control-flow">Control Flow</a></h2>
|
||
<p>The ability to run some code depending on whether a condition is <code>true</code> and the
|
||
ability to run some code repeatedly while a condition is <code>true</code> are basic
|
||
building blocks in most programming languages. The most common constructs that
|
||
let you control the flow of execution of Rust code are <code>if</code> expressions and
|
||
loops.</p>
|
||
<h3 id="if-expressions"><a class="header" href="#if-expressions"><code>if</code> Expressions</a></h3>
|
||
<p>An <code>if</code> expression allows you to branch your code depending on conditions. You
|
||
provide a condition and then state, “If this condition is met, run this block
|
||
of code. If the condition is not met, do not run this block of code.”</p>
|
||
<p>Create a new project called <em>branches</em> in your <em>projects</em> directory to explore
|
||
the <code>if</code> expression. In the <em>src/main.rs</em> file, input the following:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let number = 3;
|
||
|
||
if number < 5 {
|
||
println!("condition was true");
|
||
} else {
|
||
println!("condition was false");
|
||
}
|
||
}</code></pre>
|
||
<p>All <code>if</code> expressions start with the keyword <code>if</code>, followed by a condition. In
|
||
this case, the condition checks whether or not the variable <code>number</code> has a
|
||
value less than 5. We place the block of code to execute if the condition is
|
||
<code>true</code> immediately after the condition inside curly brackets. Blocks of code
|
||
associated with the conditions in <code>if</code> expressions are sometimes called <em>arms</em>,
|
||
just like the arms in <code>match</code> expressions that we discussed in the <a href="../ch02/ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number">“Comparing
|
||
the Guess to the Secret Number”</a><!--
|
||
ignore --> section of Chapter 2.</p>
|
||
<p>Optionally, we can also include an <code>else</code> expression, which we chose to do
|
||
here, to give the program an alternative block of code to execute should the
|
||
condition evaluate to <code>false</code>. If you don’t provide an <code>else</code> expression and
|
||
the condition is <code>false</code>, the program will just skip the <code>if</code> block and move on
|
||
to the next bit of code.</p>
|
||
<p>Try running this code; you should see the following output:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling branches v0.1.0 (file:///projects/branches)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
|
||
Running `target/debug/branches`
|
||
condition was true
|
||
</code></pre>
|
||
<p>Let’s try changing the value of <code>number</code> to a value that makes the condition
|
||
<code>false</code> to see what happens:</p>
|
||
<pre><code class="language-rust ignore"><span class="boring">fn main() {
|
||
</span> let number = 7;
|
||
<span class="boring">
|
||
</span><span class="boring"> if number < 5 {
|
||
</span><span class="boring"> println!("condition was true");
|
||
</span><span class="boring"> } else {
|
||
</span><span class="boring"> println!("condition was false");
|
||
</span><span class="boring"> }
|
||
</span><span class="boring">}</span></code></pre>
|
||
<p>Run the program again, and look at the output:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling branches v0.1.0 (file:///projects/branches)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
|
||
Running `target/debug/branches`
|
||
condition was false
|
||
</code></pre>
|
||
<p>It’s also worth noting that the condition in this code <em>must</em> be a <code>bool</code>. If
|
||
the condition isn’t a <code>bool</code>, we’ll get an error. For example, try running the
|
||
following code:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre><code class="language-rust ignore does_not_compile">fn main() {
|
||
let number = 3;
|
||
|
||
if number {
|
||
println!("number was three");
|
||
}
|
||
}</code></pre>
|
||
<p>The <code>if</code> condition evaluates to a value of <code>3</code> this time, and Rust throws an
|
||
error:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling branches v0.1.0 (file:///projects/branches)
|
||
error[E0308]: mismatched types
|
||
--> src/main.rs:4:8
|
||
|
|
||
4 | if number {
|
||
| ^^^^^^ expected `bool`, found integer
|
||
|
||
For more information about this error, try `rustc --explain E0308`.
|
||
error: could not compile `branches` (bin "branches") due to 1 previous error
|
||
</code></pre>
|
||
<p>The error indicates that Rust expected a <code>bool</code> but got an integer. Unlike
|
||
languages such as Ruby and JavaScript, Rust will not automatically try to
|
||
convert non-Boolean types to a Boolean. You must be explicit and always provide
|
||
<code>if</code> with a Boolean as its condition. If we want the <code>if</code> code block to run
|
||
only when a number is not equal to <code>0</code>, for example, we can change the <code>if</code>
|
||
expression to the following:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let number = 3;
|
||
|
||
if number != 0 {
|
||
println!("number was something other than zero");
|
||
}
|
||
}</code></pre>
|
||
<p>Running this code will print <code>number was something other than zero</code>.</p>
|
||
<h4 id="handling-multiple-conditions-with-else-if"><a class="header" href="#handling-multiple-conditions-with-else-if">Handling Multiple Conditions with <code>else if</code></a></h4>
|
||
<p>You can use multiple conditions by combining <code>if</code> and <code>else</code> in an <code>else if</code>
|
||
expression. For example:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let number = 6;
|
||
|
||
if number % 4 == 0 {
|
||
println!("number is divisible by 4");
|
||
} else if number % 3 == 0 {
|
||
println!("number is divisible by 3");
|
||
} else if number % 2 == 0 {
|
||
println!("number is divisible by 2");
|
||
} else {
|
||
println!("number is not divisible by 4, 3, or 2");
|
||
}
|
||
}</code></pre>
|
||
<p>This program has four possible paths it can take. After running it, you should
|
||
see the following output:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling branches v0.1.0 (file:///projects/branches)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
|
||
Running `target/debug/branches`
|
||
number is divisible by 3
|
||
</code></pre>
|
||
<p>When this program executes, it checks each <code>if</code> expression in turn and executes
|
||
the first body for which the condition evaluates to <code>true</code>. Note that even
|
||
though 6 is divisible by 2, we don’t see the output <code>number is divisible by 2</code>,
|
||
nor do we see the <code>number is not divisible by 4, 3, or 2</code> text from the <code>else</code>
|
||
block. That’s because Rust only executes the block for the first <code>true</code>
|
||
condition, and once it finds one, it doesn’t even check the rest.</p>
|
||
<p>Using too many <code>else if</code> expressions can clutter your code, so if you have more
|
||
than one, you might want to refactor your code. Chapter 6 describes a powerful
|
||
Rust branching construct called <code>match</code> for these cases.</p>
|
||
<h4 id="using-if-in-a-let-statement"><a class="header" href="#using-if-in-a-let-statement">Using <code>if</code> in a <code>let</code> Statement</a></h4>
|
||
<p>Because <code>if</code> is an expression, we can use it on the right side of a <code>let</code>
|
||
statement to assign the outcome to a variable, as in Listing 3-2.</p>
|
||
<figure class="listing" id="listing-3-2">
|
||
<span class="file-name">Filename: src/main.rs</span>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let condition = true;
|
||
let number = if condition { 5 } else { 6 };
|
||
|
||
println!("The value of number is: {number}");
|
||
}</code></pre>
|
||
<figcaption><a href="#listing-3-2">Listing 3-2</a>: Assigning the result of an <code>if</code> expression to a variable</figcaption>
|
||
</figure>
|
||
<p>The <code>number</code> variable will be bound to a value based on the outcome of the <code>if</code>
|
||
expression. Run this code to see what happens:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling branches v0.1.0 (file:///projects/branches)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
|
||
Running `target/debug/branches`
|
||
The value of number is: 5
|
||
</code></pre>
|
||
<p>Remember that blocks of code evaluate to the last expression in them, and
|
||
numbers by themselves are also expressions. In this case, the value of the
|
||
whole <code>if</code> expression depends on which block of code executes. This means the
|
||
values that have the potential to be results from each arm of the <code>if</code> must be
|
||
the same type; in Listing 3-2, the results of both the <code>if</code> arm and the <code>else</code>
|
||
arm were <code>i32</code> integers. If the types are mismatched, as in the following
|
||
example, we’ll get an error:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre><code class="language-rust ignore does_not_compile">fn main() {
|
||
let condition = true;
|
||
|
||
let number = if condition { 5 } else { "six" };
|
||
|
||
println!("The value of number is: {number}");
|
||
}</code></pre>
|
||
<p>When we try to compile this code, we’ll get an error. The <code>if</code> and <code>else</code> arms
|
||
have value types that are incompatible, and Rust indicates exactly where to
|
||
find the problem in the program:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling branches v0.1.0 (file:///projects/branches)
|
||
error[E0308]: `if` and `else` have incompatible types
|
||
--> src/main.rs:4:44
|
||
|
|
||
4 | let number = if condition { 5 } else { "six" };
|
||
| - ^^^^^ expected integer, found `&str`
|
||
| |
|
||
| expected because of this
|
||
|
||
For more information about this error, try `rustc --explain E0308`.
|
||
error: could not compile `branches` (bin "branches") due to 1 previous error
|
||
</code></pre>
|
||
<p>The expression in the <code>if</code> block evaluates to an integer, and the expression in
|
||
the <code>else</code> block evaluates to a string. This won’t work, because variables must
|
||
have a single type, and Rust needs to know definitively at compile time what
|
||
type the <code>number</code> variable is. Knowing the type of <code>number</code> lets the compiler
|
||
verify the type is valid everywhere we use <code>number</code>. Rust wouldn’t be able to
|
||
do that if the type of <code>number</code> was only determined at runtime; the compiler
|
||
would be more complex and would make fewer guarantees about the code if it had
|
||
to keep track of multiple hypothetical types for any variable.</p>
|
||
<h3 id="repetition-with-loops"><a class="header" href="#repetition-with-loops">Repetition with Loops</a></h3>
|
||
<p>It’s often useful to execute a block of code more than once. For this task,
|
||
Rust provides several <em>loops</em>, which will run through the code inside the loop
|
||
body to the end and then start immediately back at the beginning. To experiment
|
||
with loops, let’s make a new project called <em>loops</em>.</p>
|
||
<p>Rust has three kinds of loops: <code>loop</code>, <code>while</code>, and <code>for</code>. Let’s try each one.</p>
|
||
<h4 id="repeating-code-with-loop"><a class="header" href="#repeating-code-with-loop">Repeating Code with <code>loop</code></a></h4>
|
||
<p>The <code>loop</code> keyword tells Rust to execute a block of code over and over again
|
||
either forever or until you explicitly tell it to stop.</p>
|
||
<p>As an example, change the <em>src/main.rs</em> file in your <em>loops</em> directory to look
|
||
like this:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre><code class="language-rust ignore">fn main() {
|
||
loop {
|
||
println!("again!");
|
||
}
|
||
}</code></pre>
|
||
<p>When we run this program, we’ll see <code>again!</code> printed over and over continuously
|
||
until we stop the program manually. Most terminals support the keyboard shortcut
|
||
<kbd>ctrl</kbd>-<kbd>C</kbd> to interrupt a program that is stuck in a continual
|
||
loop. Give it a try:</p>
|
||
<!-- manual-regeneration
|
||
cd listings/ch03-common-programming-concepts/no-listing-32-loop
|
||
cargo run
|
||
CTRL-C
|
||
-->
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling loops v0.1.0 (file:///projects/loops)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s
|
||
Running `target/debug/loops`
|
||
again!
|
||
again!
|
||
again!
|
||
again!
|
||
^Cagain!
|
||
</code></pre>
|
||
<p>The symbol <code>^C</code> represents where you pressed <kbd>ctrl</kbd>-<kbd>C</kbd>.</p>
|
||
<p>You may or may not see the word <code>again!</code> printed after the <code>^C</code>, depending on
|
||
where the code was in the loop when it received the interrupt signal.</p>
|
||
<p>Fortunately, Rust also provides a way to break out of a loop using code. You
|
||
can place the <code>break</code> keyword within the loop to tell the program when to stop
|
||
executing the loop. Recall that we did this in the guessing game in the
|
||
<a href="../ch02/ch02-00-guessing-game-tutorial.html#quitting-after-a-correct-guess">“Quitting After a Correct Guess”</a><!-- ignore
|
||
--> section of Chapter 2 to exit the program when the user won the game by
|
||
guessing the correct number.</p>
|
||
<p>We also used <code>continue</code> in the guessing game, which in a loop tells the program
|
||
to skip over any remaining code in this iteration of the loop and go to the
|
||
next iteration.</p>
|
||
<h4 id="returning-values-from-loops"><a class="header" href="#returning-values-from-loops">Returning Values from Loops</a></h4>
|
||
<p>One of the uses of a <code>loop</code> is to retry an operation you know might fail, such
|
||
as checking whether a thread has completed its job. You might also need to pass
|
||
the result of that operation out of the loop to the rest of your code. To do
|
||
this, you can add the value you want returned after the <code>break</code> expression you
|
||
use to stop the loop; that value will be returned out of the loop so that you
|
||
can use it, as shown here:</p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let mut counter = 0;
|
||
|
||
let result = loop {
|
||
counter += 1;
|
||
|
||
if counter == 10 {
|
||
break counter * 2;
|
||
}
|
||
};
|
||
|
||
println!("The result is {result}");
|
||
}</code></pre>
|
||
<p>Before the loop, we declare a variable named <code>counter</code> and initialize it to
|
||
<code>0</code>. Then, we declare a variable named <code>result</code> to hold the value returned from
|
||
the loop. On every iteration of the loop, we add <code>1</code> to the <code>counter</code> variable,
|
||
and then check whether the <code>counter</code> is equal to <code>10</code>. When it is, we use the
|
||
<code>break</code> keyword with the value <code>counter * 2</code>. After the loop, we use a
|
||
semicolon to end the statement that assigns the value to <code>result</code>. Finally, we
|
||
print the value in <code>result</code>, which in this case is <code>20</code>.</p>
|
||
<p>You can also <code>return</code> from inside a loop. While <code>break</code> only exits the current
|
||
loop, <code>return</code> always exits the current function.</p>
|
||
<!-- Old headings. Do not remove or links may break. -->
|
||
<p><a id="loop-labels-to-disambiguate-between-multiple-loops"></a></p>
|
||
<h4 id="disambiguating-with-loop-labels"><a class="header" href="#disambiguating-with-loop-labels">Disambiguating with Loop Labels</a></h4>
|
||
<p>If you have loops within loops, <code>break</code> and <code>continue</code> apply to the innermost
|
||
loop at that point. You can optionally specify a <em>loop label</em> on a loop that
|
||
you can then use with <code>break</code> or <code>continue</code> to specify that those keywords
|
||
apply to the labeled loop instead of the innermost loop. Loop labels must begin
|
||
with a single quote. Here’s an example with two nested loops:</p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let mut count = 0;
|
||
'counting_up: loop {
|
||
println!("count = {count}");
|
||
let mut remaining = 10;
|
||
|
||
loop {
|
||
println!("remaining = {remaining}");
|
||
if remaining == 9 {
|
||
break;
|
||
}
|
||
if count == 2 {
|
||
break 'counting_up;
|
||
}
|
||
remaining -= 1;
|
||
}
|
||
|
||
count += 1;
|
||
}
|
||
println!("End count = {count}");
|
||
}</code></pre>
|
||
<p>The outer loop has the label <code>'counting_up</code>, and it will count up from 0 to 2.
|
||
The inner loop without a label counts down from 10 to 9. The first <code>break</code> that
|
||
doesn’t specify a label will exit the inner loop only. The <code>break 'counting_up;</code> statement will exit the outer loop. This code prints:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling loops v0.1.0 (file:///projects/loops)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.58s
|
||
Running `target/debug/loops`
|
||
count = 0
|
||
remaining = 10
|
||
remaining = 9
|
||
count = 1
|
||
remaining = 10
|
||
remaining = 9
|
||
count = 2
|
||
remaining = 10
|
||
End count = 2
|
||
</code></pre>
|
||
<!-- Old headings. Do not remove or links may break. -->
|
||
<p><a id="conditional-loops-with-while"></a></p>
|
||
<h4 id="streamlining-conditional-loops-with-while"><a class="header" href="#streamlining-conditional-loops-with-while">Streamlining Conditional Loops with while</a></h4>
|
||
<p>A program will often need to evaluate a condition within a loop. While the
|
||
condition is <code>true</code>, the loop runs. When the condition ceases to be <code>true</code>, the
|
||
program calls <code>break</code>, stopping the loop. It’s possible to implement behavior
|
||
like this using a combination of <code>loop</code>, <code>if</code>, <code>else</code>, and <code>break</code>; you could
|
||
try that now in a program, if you’d like. However, this pattern is so common
|
||
that Rust has a built-in language construct for it, called a <code>while</code> loop. In
|
||
Listing 3-3, we use <code>while</code> to loop the program three times, counting down each
|
||
time, and then, after the loop, to print a message and exit.</p>
|
||
<figure class="listing" id="listing-3-3">
|
||
<span class="file-name">Filename: src/main.rs</span>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let mut number = 3;
|
||
|
||
while number != 0 {
|
||
println!("{number}!");
|
||
|
||
number -= 1;
|
||
}
|
||
|
||
println!("LIFTOFF!!!");
|
||
}</code></pre>
|
||
<figcaption><a href="#listing-3-3">Listing 3-3</a>: Using a <code>while</code> loop to run code while a condition evaluates to <code>true</code></figcaption>
|
||
</figure>
|
||
<p>This construct eliminates a lot of nesting that would be necessary if you used
|
||
<code>loop</code>, <code>if</code>, <code>else</code>, and <code>break</code>, and it’s clearer. While a condition
|
||
evaluates to <code>true</code>, the code runs; otherwise, it exits the loop.</p>
|
||
<h4 id="looping-through-a-collection-with-for"><a class="header" href="#looping-through-a-collection-with-for">Looping Through a Collection with <code>for</code></a></h4>
|
||
<p>You can choose to use the <code>while</code> construct to loop over the elements of a
|
||
collection, such as an array. For example, the loop in Listing 3-4 prints each
|
||
element in the array <code>a</code>.</p>
|
||
<figure class="listing" id="listing-3-4">
|
||
<span class="file-name">Filename: src/main.rs</span>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let a = [10, 20, 30, 40, 50];
|
||
let mut index = 0;
|
||
|
||
while index < 5 {
|
||
println!("the value is: {}", a[index]);
|
||
|
||
index += 1;
|
||
}
|
||
}</code></pre>
|
||
<figcaption><a href="#listing-3-4">Listing 3-4</a>: Looping through each element of a collection using a <code>while</code> loop</figcaption>
|
||
</figure>
|
||
<p>Here, the code counts up through the elements in the array. It starts at index
|
||
<code>0</code> and then loops until it reaches the final index in the array (that is,
|
||
when <code>index < 5</code> is no longer <code>true</code>). Running this code will print every
|
||
element in the array:</p>
|
||
<pre><code class="language-console">$ cargo run
|
||
Compiling loops v0.1.0 (file:///projects/loops)
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.32s
|
||
Running `target/debug/loops`
|
||
the value is: 10
|
||
the value is: 20
|
||
the value is: 30
|
||
the value is: 40
|
||
the value is: 50
|
||
</code></pre>
|
||
<p>All five array values appear in the terminal, as expected. Even though <code>index</code>
|
||
will reach a value of <code>5</code> at some point, the loop stops executing before trying
|
||
to fetch a sixth value from the array.</p>
|
||
<p>However, this approach is error-prone; we could cause the program to panic if
|
||
the index value or test condition is incorrect. For example, if you changed the
|
||
definition of the <code>a</code> array to have four elements but forgot to update the
|
||
condition to <code>while index < 4</code>, the code would panic. It’s also slow, because
|
||
the compiler adds runtime code to perform the conditional check of whether the
|
||
index is within the bounds of the array on every iteration through the loop.</p>
|
||
<p>As a more concise alternative, you can use a <code>for</code> loop and execute some code
|
||
for each item in a collection. A <code>for</code> loop looks like the code in Listing 3-5.</p>
|
||
<figure class="listing" id="listing-3-5">
|
||
<span class="file-name">Filename: src/main.rs</span>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
let a = [10, 20, 30, 40, 50];
|
||
|
||
for element in a {
|
||
println!("the value is: {element}");
|
||
}
|
||
}</code></pre>
|
||
<figcaption><a href="#listing-3-5">Listing 3-5</a>: Looping through each element of a collection using a <code>for</code> loop</figcaption>
|
||
</figure>
|
||
<p>When we run this code, we’ll see the same output as in Listing 3-4. More
|
||
importantly, we’ve now increased the safety of the code and eliminated the
|
||
chance of bugs that might result from going beyond the end of the array or not
|
||
going far enough and missing some items. Machine code generated from <code>for</code>
|
||
loops can be more efficient as well because the index doesn’t need to be
|
||
compared to the length of the array at every iteration.</p>
|
||
<p>Using the <code>for</code> loop, you wouldn’t need to remember to change any other code if
|
||
you changed the number of values in the array, as you would with the method
|
||
used in Listing 3-4.</p>
|
||
<p>The safety and conciseness of <code>for</code> loops make them the most commonly used loop
|
||
construct in Rust. Even in situations in which you want to run some code a
|
||
certain number of times, as in the countdown example that used a <code>while</code> loop
|
||
in Listing 3-3, most Rustaceans would use a <code>for</code> loop. The way to do that
|
||
would be to use a <code>Range</code>, provided by the standard library, which generates
|
||
all numbers in sequence starting from one number and ending before another
|
||
number.</p>
|
||
<p>Here’s what the countdown would look like using a <code>for</code> loop and another method
|
||
we’ve not yet talked about, <code>rev</code>, to reverse the range:</p>
|
||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||
for number in (1..4).rev() {
|
||
println!("{number}!");
|
||
}
|
||
println!("LIFTOFF!!!");
|
||
}</code></pre>
|
||
<p>This code is a bit nicer, isn’t it?</p>
|
||
<h2 id="summary"><a class="header" href="#summary">Summary</a></h2>
|
||
<p>You made it! This was a sizable chapter: You learned about variables, scalar
|
||
and compound data types, functions, comments, <code>if</code> expressions, and loops! To
|
||
practice with the concepts discussed in this chapter, try building programs to
|
||
do the following:</p>
|
||
<ul>
|
||
<li>Convert temperatures between Fahrenheit and Celsius.</li>
|
||
<li>Generate the <em>n</em>th Fibonacci number.</li>
|
||
<li>Print the lyrics to the Christmas carol “The Twelve Days of Christmas,”
|
||
taking advantage of the repetition in the song.</li>
|
||
</ul>
|
||
<p>When you’re ready to move on, we’ll talk about a concept in Rust that <em>doesn’t</em>
|
||
commonly exist in other programming languages: ownership.</p>
|
||
</body>
|
||
</html>
|