Functions Flashcards

(11 cards)

1
Q

What is the convention for naming functions?

A

Rust convetion is to use snake case for function and variable names meaning all the letters are lowercase and underscores separate words.

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

Does Rust care where you define a function?

A

No, Rust doesn’t care where you defined a function only that they’re defined somewhere in a scope that can be seen by the caller.

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

Do you need to declare the type in function signatures?

A

Yes, in Rust you must declare the tyupe of each parameter. Each parameter declaration is seperated with commas.

fn test_function(x_coord:i32, y_coord:i32, z_coord:i32){
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are statements?

A

Statements are instructions that perform some action and do not return a value

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

What are expressions?

A

Expressions evaluate to a resultant value

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

Does Rust automatically try to convert non-Boolean types to a Boolean in an if expression?

A

No, unlike Ruby and Javascript Rust will not automatically try to convert non-Boolean types to a Boolean

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

Why can’t you have different types when using an if block in variable assignment?

A

Variables must have a single type, and Rust needs to know at compile time what type the variable is.

let condition = true;
let number = if condition {5} else {"six"}; // returns an error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the three kinds of loops that rust has?

A

loop, while, and for

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

What is the loop keyword in Rust?

A

The loop keyword tells Rust to execute a block of code over and over again forever until it’s explicitly told to stop. One of the uses of a loop is to try an operation that is known to probably fail such as checking whether a thread has completed its job.

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

How might you provide the value you got out of the loop to a variable?

A
let mut counter = 0;

let result = loop {
    counter += 1;
    if counter == 10 {
        break counter * 2;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Difference between break and return?

A

break only exists the current loop, return always exits the current function

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