Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
Control Flow
Control Flowif
Expressions
fn main() {
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}
let number = 3;
if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}
Blocks of code associated with the conditions in
if
expressions are sometimes called arms, just like the arms in match
expressionError code:
expected `bool`, found integer
fn main() {
let number = 3;
if number {
println!("number was three");
}
}
let number = 3;
if number {
println!("number was three");
}
}
Using
if
in a
let
Statement
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number); //The value of number is: 5
}
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number); //The value of number is: 5
}
Error code:
fn main() {
let condition = true;
let number = if condition { 5 } else { "six" };
println!("The value of number is: {}", number);
}
let condition = true;
let number = if condition { 5 } else { "six" };
println!("The value of number is: {}", number);
}
$ cargo run
Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: if and else have incompatible types
--> src/main.rs:4:44
|
4 | let number = if condition { 5 } else { "six" };
| - ^^^^^ expected integer, found `&str`
| |
| expected because of this
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `branches`.
To learn more, run the command again with --verbose.
Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: if and else have incompatible types
--> src/main.rs:4:44
|
4 | let number = if condition { 5 } else { "six" };
| - ^^^^^ expected integer, found `&str`
| |
| expected because of this
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `branches`.
To learn more, run the command again with --verbose.
Repeating Code with
loop
fn main() {
loop {
println!("again!");
}
}
loop {
println!("again!");
}
}
Returning Values from Loops
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {}", result); // The result is 20
}
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {}", result); // The result is 20
}
Conditional Loops with while
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
}
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
}
Looping Through a Collection with for
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
}
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
}
Here’s what the countdown would look like using a
for
loop and another method we’ve not yet talked about, rev
, to reverse the range:fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}