Rust Book Day 1 Flashcards

Learning Rust

1
Q

How to quickly verify if compiling is good?

A

cargo check

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

how to compile it with optimizations?

A

cargo build –release

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

what is part of the cargo.toml

A

[package]
name = “hello_project”
version = “0.1.0”
edition = “2020”

Comment
[dependencies]
list your crates here

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

Print hello world

A

fn main() {
println!(“hello world”);
}

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

how to compile and run straight away?

A

cargo run

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

how to build and then execute separately?

A

cargo build
then execute
./target/debug/project_name

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

How to ask for command line input (like a guess)

A

io::stdin()
.read_line(&mut guess)
.expect(“Failed to read line”);

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

How to import standard input library for terminal inputs?

A

use stdin::io;

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

How to create a variable whcih is a mutable string

A

let mut my_variable = String::new();

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

when using read_line(&mut guess) we are passing the variable guess. why must it be mutable?

A

Because we are appending stuff to the string as we are writing it seems, growing the string

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

what does the & in &mut guess indicate?

A

that this argument is a reference, no need to copy the data into memory multiple times

references are also immutable by default, so you need to write &mut here

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

read_line() returns a Result which is an …

A

enum

it is a type that can be in one of multipel possible states (each possibile state is called a variant)

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

what is Rng in use rand::Rng;

A

a trait

it defines methods, so the trait must be in scope for us to use those methods

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

turn string into integer

A

let guess: u32 = guess.trim().parse().expect(“should be a number”);

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