vault backup: 2025-05-14 14:32:55
This commit is contained in:
parent
1733bccf94
commit
3ee80994dc
@ -91,7 +91,78 @@ escapes), hex values, octal values, binary values, or even as a byte(b'A').
|
|||||||
> but instead will wrap around to the first value in the possible range.
|
> but instead will wrap around to the first value in the possible range.
|
||||||
|
|
||||||
# 3.3 Functions
|
# 3.3 Functions
|
||||||
|
Rust functions have a couple of interesting properties:
|
||||||
|
|
||||||
|
1. Rust functions don't care about order. They can be in any place in the code,
|
||||||
|
as long as the scope of things is maintained relative to where calls happen.
|
||||||
|
|
||||||
|
2. Functions start with `fn`, followed by a name in snake case, then inputs,
|
||||||
|
then the function scope using {}.
|
||||||
|
|
||||||
|
*functions* is an example of some basic functions
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
Parameters can be used to pass values into functions. When this is done, two
|
||||||
|
things must happen: First, the value must be input into the function (duh!).
|
||||||
|
Second, the function must *match the type defined in the function*.
|
||||||
|
|
||||||
|
Arguments are a similar thing to parameters, but have a technical difference
|
||||||
|
that arguments are concrete values. Like add(5), vs add(x).
|
||||||
|
|
||||||
|
## Statements and Expressions
|
||||||
|
A *statement* is a line of code that does some computation or other calculation,
|
||||||
|
and does NOT return a value.
|
||||||
|
|
||||||
|
A *expression* evaluates to a resultant value.
|
||||||
|
|
||||||
|
Rust does something interesting. When defining a variable, the output is a
|
||||||
|
*statement*, not an expression. Using `let` to define the variable does NOT
|
||||||
|
return the variable itself. Defining functions are also statements.
|
||||||
|
|
||||||
|
Calling functions, macros, or using a scope block created with curly brackets
|
||||||
|
is an expression however. Here's a mind bending example:
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
fn main() {
|
||||||
|
let y = {
|
||||||
|
let x = 3;
|
||||||
|
x + 1
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("The value of y is: {y}");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This is an statement (defining main), with a statement inside (defining y) which
|
||||||
|
has an expression inside (the statement defining x, and then the expression adding x).
|
||||||
|
|
||||||
|
>[!Important] Statements, Expressions, and Semicolons
|
||||||
|
> Notice `x+1` does **not** end in a semicolon. If it did, it would be a statement.
|
||||||
|
> Expressions do not end in semicolons.
|
||||||
|
|
||||||
|
## Functions with Return Values
|
||||||
|
Functions can have outputs, but they do not need to be defined with `return` keywords.
|
||||||
|
Instead, the function will by default return the last expressions result.
|
||||||
|
|
||||||
|
Functions must declare the type of the output:
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
fn five() -> i32{
|
||||||
|
5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
# 3.4 Comments
|
# 3.4 Comments
|
||||||
|
|
||||||
|
This one is pretty simple.
|
||||||
|
```Rust
|
||||||
|
// This is a single line comment.
|
||||||
|
let x = 22; // They can be after code
|
||||||
|
|
||||||
|
/* Or, if I've really got something to say
|
||||||
|
I can use this multiple line comment */
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
# 3.5 Control Flow
|
# 3.5 Control Flow
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user