Programming A Guessing Game Flashcards

1
Q

What is the command to create a new Rust project?

A

cargo new project_name

cd project_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the io library?

A

input/output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Where does the io library come from?

A

The standard library known as std

Example: use std::io;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the prelude?

A

The prelude is the list of things that Rust automatically imports into every Rust program. It’s kept as small as possible, and is focused on things, particularly traits, which are used in almost every single Rust program.

More info in the standard library documentation:

https://doc.rust-lang.org/std/prelude/index.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you use a type that isn’t in the prelude?

A

You must bring the type into scope explicitly with a use statement.

Using the std::io library provides you with a number useful features, including the ability to accept user input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

fn main() {

The main function is the entry point into the program.

What do each of these mean?

fn
main()
{

A

The fn syntax declares a new function.

The parentheses, (), indicate there are no parameters.

The curly bracket, {, starts the body of the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you make a variable mutable?

A

Add mut before the variable’s name.

let apples = 5; // immutable
let mut bananas = 5; // mutable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

let mut guess will introduce a mutable variable named what?

A

The mutable variable will be named guess.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the = sign mean?

A

It tells Rust we want to bind something to the variable now.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

let mut guess = String::new();

String::new is bound to guess. What is String:new?

A

It is a function that returns a new instance of a String.

String is a string type provided by the standard library that is a growable, UTF-8 encoded bit of text.

https://doc.rust-lang.org/std/string/struct.String.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

let mut guess = String::new();

What does the :: syntax in the ::new line indicate?

A

The :: syntax in the ::new line indicates that new is an associated function of the String type.

An associated function is a function that’s implemented on a type, in this case String. This new function creates a new, empty string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an associated function?

A

An associated function is a function that’s implemented on a type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

let mut guess = String::new();

What does this code create?

A

In full, the let mut guess = String::new(); line has created a mutable variable that is currently bound to a new, empty instance of a String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

We’ll call the stdin function from the io module.

io::stdin()
.read_line(&mut guess)

What does this allow us to do?

A

It will allow us to handle user input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

io::stdin()
.read_line(&mut guess)

If we hadn’t imported the io library with use std::io at the beginning of the program, how else could we still use the function?

A

We could write the function call as std::io:stdin.

The stdin function returns an instance of std::io::Stdin, which is a type that represents a handle to the standard input for your terminal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

.read_line(&mut guess)

What method does this line call?

A

The line .read_line(&mut guess) calls the read_line method on the standard input handle to get input from the user.

17
Q

.read_line(&mut guess)

What is the purpose of &mut guess in this line?

A

We pass &mut guess as the argument to read_line to tell it what string to store the user input in.

18
Q

io::stdin()
.read_line(&mut guess)

What is the full job of read_line?

A

The full job of read_line is to take whatever the user types into standard input and append that into a string (without overwriting its contents), so we therefore pass that string as an argument. The string argument needs to be mutable so the method can change the string’s content.

19
Q

What does & indicate?

A

The & indicates that this argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times.

References, like variables, are immutable by default. You need to write &mut guess rather than &guess to make it mutable.

20
Q

How do you make a reference mutable?

A

Write mut after the &.

&mut guess

21
Q

Result is from the standard library. It has two variants. What are they?

A

Ok and Err

22
Q

What do the Ok and Err variants of Result indicate?

A

The Ok variant indicates the operation was successful, and inside Ok is the successfully generated value. The Err variant means the operation failed, and Err contains information about how or why the operation failed.