0% found this document useful (0 votes)
24 views

Functions in Rust

- Functions in Rust are defined using `fn` and curly braces. The order of function definition doesn't matter. Parameters require type specification. - Rust is an expression-based language where statements don't return values unlike expressions. Semicolons turn expressions into statements. - Return types must be specified, like `fn five() -> i32`. - Comments use `//` for single line and multiline. - Control flow includes `if` expressions where the condition must be a bool, and `else if` blocks. Values in `if` conditions must be the same type. - Loops include infinite `loop` with `break`, condition-checked `while`, and `

Uploaded by

Marina Atoyants
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Functions in Rust

- Functions in Rust are defined using `fn` and curly braces. The order of function definition doesn't matter. Parameters require type specification. - Rust is an expression-based language where statements don't return values unlike expressions. Semicolons turn expressions into statements. - Return types must be specified, like `fn five() -> i32`. - Comments use `//` for single line and multiline. - Control flow includes `if` expressions where the condition must be a bool, and `else if` blocks. Values in `if` conditions must be the same type. - Loops include infinite `loop` with `break`, condition-checked `while`, and `

Uploaded by

Marina Atoyants
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

## Functions

- define using `fn` and curly braces


- Rust doesn't care if function is defined before or after its caller
- Parameters e.g. `fn print_labeled_measurement(value: i32, unit_label: char)`
- type must be specified
- Statements and Expressions
- Rust is expression-based language
- In Rust statements e.g. `let a = 6` doesn't return a value (unlike C and
Ruby)
- Expressions evaluate to a value, e.g `5+6` expression evaluates to `11`
- Expression can be part of a statement
- Expressions don't have semicolon `;`. Once semicolon is used it becomes a
statement and doesn't have a return value.
E.g. here `x + 1` doesn't have `;`, so the block returns 4.
```rust
{
let x = 3;
x + 1
}
```
- Return Values
- declare type of the return value e.g. below.
5 is the return value (note it doesn't have a semicolon at the end)
```rust
fn five() -> i32 {
5
}
```

## Comments

- one line and multi line `//`


- documentation comments will be discussed in “Publishing a Crate to Crates.io”
section of Chapter 14.

## Control Flow

### if Expressions

```rust
fn main() {
let number = 3;

if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}
```

The condition must be alway bool. Otherwise - error. (so `if number {}` won't
work)

```rust
fn main() {
let number = 6;

if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}
```

```rust
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };

println!("The value of number is: {}", number);


}
```
Both values in the if condition should be of the same type as rust need to know the
type of number at compile time.

### Repetition with Loops

- `loop`
- infinite loop until `break` used explicitly
- can specify a loop label `'label_name: loop {}`
- can break loop by label `break 'label_name`
```rust
fn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {}", count);
let mut remaining = 10;

loop {
println!("remaining = {}", remaining);
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}

count += 1;
}
println!("End count = {}", count);
}
```
- can return value by `break value;`
- `while`
- slower than for due to the condition check
- `for`
- `for element in arr {}`
- use Range from standard lib like `(1..4)` - 4 not incl
- use `(1..4).rev()` to reverse the range

`continue` is used to jump to the next iteration of the loop.

You might also like