# 【Rust】入門してみました③

Rust の勉強をしてみたのでここにメモを残します。

### if

#### コード

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

fn main() {
divisible(1);
divisible(2);
divisible(3);
divisible(4);
divisible(5);
divisible(6);
divisible(7);
divisible(8);
divisible(9);
}
```

#### 実行

```\$ ./sample
number(1) is not divisible by 4 or 2
number(2) is divisible by 2
number(3) is not divisible by 4 or 2
number(4) is divisible by 4
number(5) is not divisible by 4 or 2
number(6) is divisible by 2
number(7) is not divisible by 4 or 2
number(8) is divisible by 4
number(9) is not divisible by 4 or 2```

### loop

#### コード

```fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};

println!("The result is {}", result);
}
```

#### 実行

```\$ ./sample
The result is 20```

### while

#### コード

```fn main() {
let mut number = 3;
while number != 0 {
println!("{}", number);
number -= 1;
};

println!("LIFTOFF!!!");
}
```

#### 実行

```\$ ./sample
3
2
1
LIFTOFF!!!```

### for

#### コード

```fn main() {
let a = [10, 20, 30, 40, 50];

for element in a.iter() {
println!("the value is: {}", element);
}
}
```

#### 実行

```\$ ./sample
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50```