This example project will teach you the following concepts:
- Loops in Rust
You will learn loops practically using easy to understand examples.
Step 1: Create Project
The first step is to create a Rust project .
Creating a Project Directory
Open a terminal and enter the following commands to make a projects directory and an inner directory for this example within that projects directory.
For Linux, macOS, and PowerShell on Windows, enter this:
$ mkdir ~/projects
$ cd ~/projects
$ mkdir your_example_name
$ cd your_example_name
For Windows CMD, enter this:
> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> mkdir your_example_name
> cd your_example_name
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Start by creating a file named main.rs
. In it create a struct with two variables:
struct Pair {
x: i32,
y: i32
}
Now create the main function:
fn main() {
Here is an example of a for loop over the contents of a vector:
let v: &[u32] = &[4, 2, 1];
for i in v.iter() {
println!("{} is an integer!", *i);
}
A standard library function that applies a closure to every number between 0 and 10.
for i in 0..10 {
println!("{} is an integer!", i);
}
is inlined by the compiler as:
let mut j = 0usize;
while j < 10 {
println!("{} is an integer!", j);
j += 1;
}
And ssing loop:
let mut k = 0usize;
loop {
k += 1;
if k == 10 {
break;
}
println!("{} is an integer!", k);
}
More generally, a for loop works with anything implementing the Iterator
trait. Data structures can provide one or more methods that return iterators over their contents. For example, strings support iteration over their contents in various ways:
let s = "Hello";
for c in s.chars() {
println!("{}", c);
}
Destructuring a struct in for loops:
let pairs = [Pair {x: 10, y: 20}, Pair {x: 30, y: 0}];
for &Pair {x, y} in pairs.iter() {
assert_eq!(x + y, 30);
}
}
Here is the full code:
main.rs
struct Pair {
x: i32,
y: i32
}
fn main() {
let v: &[u32] = &[4, 2, 1];
for i in v.iter() {
println!("{} is an integer!", *i);
}
for i in 0..10 {
println!("{} is an integer!", i);
}
let mut j = 0usize;
while j < 10 {
println!("{} is an integer!", j);
j += 1;
}
let mut k = 0usize;
loop {
k += 1;
if k == 10 {
break;
}
println!("{} is an integer!", k);
}
let s = "Hello";
for c in s.chars() {
println!("{}", c);
}
let pairs = [Pair {x: 10, y: 20}, Pair {x: 30, y: 0}];
for &Pair {x, y} in pairs.iter() {
assert_eq!(x + y, 30);
}
}
If your run the code you will get the following:
4 is an integer!
2 is an integer!
1 is an integer!
0 is an integer!
1 is an integer!
2 is an integer!
3 is an integer!
4 is an integer!
5 is an integer!
6 is an integer!
7 is an integer!
8 is an integer!
9 is an integer!
0 is an integer!
1 is an integer!
2 is an integer!
3 is an integer!
4 is an integer!
5 is an integer!
6 is an integer!
7 is an integer!
8 is an integer!
9 is an integer!
1 is an integer!
2 is an integer!
3 is an integer!
4 is an integer!
5 is an integer!
6 is an integer!
7 is an integer!
8 is an integer!
9 is an integer!
H
e
l
l
o
Run
Run rustc hello.rs
to produce an executable called hello
(or hello.exe
on Windows).
Save the code and run your code as follows:
Assuming our file name is main.rs
, first compile it
$ rustc main.rs
then run it:
$ ./main
On Windows, enter the command .\main.exe
instead of ./main
:
compile:
> rustc main.rs
then run it:
> .\main.exe