Common Programming Concepts. Flashcards

1
Q

By default Rust variables are ___ ?

A

immutable

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

How do you make a variable mutable in Rust?

A

Adding “mut” in front of the variable name.

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

What are some differences between variables and constants?

A
  1. aren’t allowed to use “mut” with constants.
  2. Constants are declared with the “const” keyword, not “let”.
  3. The value of the constant type MUST be annotated.
  4. Constants can be declared in any scope.
  5. Constants may only be set to a constant expression.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is shadowing?

A

Declaring a new variable with the same name as a previous variable. The new variable shadows the previous variable. The first variable is shadowed by the second.

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

If one variable shadows another, what value appears when the value is used?

A

The second one.

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

How is shadowing different from marking a variable with mut?

A

A compile-time error will occur if we accidentally try to reassign to this variable without using the let keyword.

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

What does using let in shadowing allow for?

A

Perform transformations on a value but have the variable be immutable after those transformations have been completed.

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

What are two data type subsets found in Rust?

A

scalar and compound.

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

Is Rust statically typed?

A

yes.

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

What does a scalar data type represent?

A

A single value.

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

What are the four primary scalar types found in Rust?

A

Integers, floating-point numbers, Booleans, and characters.

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

What is an integer type?

A

A number without a fractional component.

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

What does the i and u stand for with integer types?

A

Signed integer types start with i and unsigned start with u.

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

List the various lengths for signed/unsigned integer types in Rust.

A
8-bit
16-bit
32-bit
64-bit
128-bit
arch
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does signed and unsigned indicate for integer types?

A

Whether it’s possible for the number to be negative or positive. Signed can be negative and positive, unsigned is only for positive.

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

How are signed integer stored?

A

Two’s compliment representation.

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

What range of numbers can signed ints store?

A

-2^(n - 1) to 2^(n - 1) - 1 inclusive.

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

What range of numbers can unsigned ints store?

A

0 to 2^(n - 1)

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

What is Rust’s integer type default?

A

i32.

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

What is a floating point type?

A

Numbers with decimal points.

21
Q

What are the two floating point types in Rust?

A

f32 and f64.

22
Q

What is the default floating point type in Rust

A

f64 - because on modern CPUs it’s roughly the same speed as f32 but is capable of more precision.

23
Q

How do you specify a boolean type?

A

bool.

24
Q

How many bytes is a boolean type?

A

one.

25
Q

How big is the char data type?

A

Four bytes in size and represents a Unicode Scalar Value.

26
Q

What is a compound type?

A

A type that can group multiple values into one type.

27
Q

What are two primitive compound types in Rust?

A

Tuples and arrays.

28
Q

What is a tuple?

A

A general way of grouping together some number of other values with a variety of types into one compound type.

29
Q

Do tuples have a fixed length?

A

Yes, once they are declared they cannot grow or shrink.

30
Q

How do you create a tuple?

A

Write a comma-separated list of values inside parentheses.

31
Q

How can we access tuple elements?

A

By destructuring (breaking down tuple into individual variables) or a . followed by an index.

32
Q

What is an array?

A

A compound data type that groups a collection of multiple values of the same type. Arrays have a fixed length.

33
Q

What are two reasons why arrays are useful?

A

When you want your data allocated on the stack rather than the heap or when you want to ensure you always have a fixed number of elements.

34
Q

What does [i32; 5] in “let a: [i32; 5] = [1, 2, 3, 4, 5];” do.

A

Specifies an array of 5 i32 integers.

35
Q

What does “let a = [3; 5];” do?

A

Initializes an array a of 5 elements all of the value 3.

36
Q

What is the keyword that allows you to to declare new functions?

A

fn

37
Q

In function signatures what must you declare?

A

The type of each parameter.

38
Q

What is a statement in Rust?

A

An instruction that performs some action and does not return a value.

39
Q

What is an expression in Rust?

A

An instruction that performs some action and does return some value.

40
Q

What does adding a ; to an expression do?

A

Turns it into a statement. It will not return anything.

41
Q

Where does a return type get declared in a function signature?

A

after the ->

42
Q

What must comments start with in Rust?

A

//

43
Q

Is if an expression or a statement in Rust?

A

An expression.

44
Q

Since if is an expression, what does it allow for?

A

Using it on the right side of a let statement.

45
Q

What are the three types of loops Rust has?

A

loop, while, for.

46
Q

What does the loop keyword do?

A

Tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.

47
Q

What is a way to pass the result of a loop operation to the rest of your code?

A

Add the value you want returned after the break expression in the loop.

48
Q

What does a while loop do in Rust?

A

While a condition holds true, the code runs; otherwise, it exits the loop.

49
Q

What does a for loop do in Rust?

A

Executes some code for each item in a collection