You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
auto merge of #15229 : steveklabnik/rust/if, r=cmr
Whew. So much here! Feedback very welcome.
This is the first part where we actually start learning things. I'd like to think I struck a good balance of explaining enough details, without getting too bogged down, and without being confusing... but of course I'd think that. 😉
As I mention in the commit comment, We probably want to move the guessing game to the rust-lang org, rather than just having it on my GitHub. Or, I could put the code inline. I think it'd be neat to have it as a project, so people can pull it down with Cargo. Until we make that decision, I'll just leave this here.
src/guessing_game.rs:4:39: 4:40 error: use of possibly uninitialized variable: `x`
578
+
src/guessing_game.rs:4 println!("The value of x is: {}", x);
579
+
^
580
+
note: in expansion of format_args!
581
+
<std macros>:2:23: 2:77 note: expansion site
582
+
<std macros>:1:1: 3:2 note: in expansion of println!
583
+
src/guessing_game.rs:4:5: 4:42 note: expansion site
584
+
error: aborting due to previous error
585
+
Could not execute process `rustc src/guessing_game.rs --crate-type bin --out-dir /home/you/projects/guessing_game/target -L /home/you/projects/guessing_game/target -L /home/you/projects/guessing_game/target/deps` (status=101)
586
+
```
587
+
588
+
Rust will not let us use a value that has not been initialized. So why let us
589
+
declare a binding without initializing it? You'd think our first example would
590
+
have errored. Well, Rust is smarter than that. Before we get to that, let's talk
591
+
about this stuff we've added to `println!`.
592
+
593
+
If you include two curly braces (`{}`, some call them moustaches...) in your
594
+
string to print, Rust will interpret this as a request to interpolate some sort
595
+
of value. **String interpolation** is a computer science term that means "stick
596
+
in the middle of a string." We add a comma, and then `x`, to indicate that we
597
+
want `x` to be the value we're interpolating. The comma is used to separate
598
+
arguments we pass to functions and macros, if you're passing more than one.
599
+
600
+
When you just use the double curly braces, Rust will attempt to display the
601
+
value in a meaningful way by checking out its type. If you want to specify the
602
+
format in a more detailed manner, there are a [wide number of options
603
+
available](/std/fmt/index.html). Fow now, we'll just stick to the default:
604
+
integers aren't very complicated to print.
605
+
606
+
So, we've cleared up all of the confusion around bindings, with one exception:
607
+
why does Rust let us declare a variable binding without an initial value if we
608
+
must initialize the binding before we use it? And how does it know that we have
609
+
or have not initialized the binding? For that, we need to learn our next
610
+
concept: `if`.
611
+
612
+
## If
613
+
614
+
## Functions
615
+
616
+
return
617
+
618
+
comments
433
619
434
620
## Compound Data Types
435
621
@@ -451,10 +637,35 @@ loop
451
637
452
638
break/continue
453
639
454
-
iterators
640
+
## Guessing Game: complete
641
+
642
+
At this point, you have successfully built the Guessing Game! Congratulations!
0 commit comments