CH 3. Common Programming Concepts Flashcards

1
Q

In Rust what does the :: syntax mean?

A

It indicates the use of an associated function of the given type. For example String::new(), new() is the function that is associated with the type String.

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

In Rust what does the & syntax mean?

A

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

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

What is an enum or enumeration?

A

It is a type that can be in one of multiple possible states. For example in enum Result<T, E>, contains the states, Ok(T) which represents a success and contains a value of Type T, Err(E) is the other state which represents an error and contains the error value.

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

What is the cargo.lock file and why is it needed?

A

Cargo has a mechanism that ensures you can rebuild the same artifact every time you or anyone else builds your code: Cargo will use only the versions of the dependencies you specified until you indicate otherwise. For example, say that next week version 0.8.4 of the rand crate comes out, and that version contains an important bug fix, but it also contains a regression that will break your code. To handle this, Rust creates the Cargo.lock file the first time you run cargo build.

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

What does cargo update do?

A

It updates the crate which will figure out all of the latest versions that fit your specifications in your cargo.toml. Then cargo will write those versions to the cargo.lock file.

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

What does the command cargo doc –open do?

A

It will build documentation provided by all of your dependencies locally and open it in your browser.

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

What is a match expression?

A

Its an expression with multiple arms. Each arm consists of a pattern to match against and the code that should be run if the value given to match fits the arms pattern.

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

What is shadowing?

A

Shadowing is when you reuse the same variable name rather than creating two unique variables.

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

What does the trim() method do?

A

The trim() method will will remove whitespace at the beginning and end of a String as well as remove newline(\n) and carriage return(\r).

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

What does the parse() method do?

A

Used on string will convert a string to another type. The parse() method could fail since it cant convert some characters to a number (ie. $,@!%) so the method returns a Result type.

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

What is the differences between constants and variables in rust?

A

Constants are always immutable. They are declared using the const keyword instead of let. The type of the value MUST be annotated. They can be declared in any scope. Last difference is that constants may be set only to a constant expression, not the result of a value that could only be computed at runtime.

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

Rust is a statically typed language, what does that mean?

A

It means that Rust must know the types of all variables at compile time.

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

What is a scalar type and what are the four primary scalar types of rust?

A

A scalar type represents a single value, such as integers floating-point numbers, booleans, and characters.

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