0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rustを勉強する part3【制御文】

Posted at

記事について

次のTour of Rustを用いて学んだ内容のメモになります。

if / else if / else

C言語とは異なり、条件文を囲む括弧はない。
論理演算子として、==,!=,<,>,<=,>=,!,||,&&などが使用できる。

fn main() {
    let x = 42;
    if x < 42 {
        println!("42 より小さい");
    } else if x == 42 {
        println!("42 に等しい");
    } else {
        println!("42 より大きい");
    }
}

loop (無限ループ)

無限ループはloop文にて記述し、ループから抜けるにはbreak文を使う。

fn main() {
    let mut x = 0;
    loop {
        x += 1;
        if x == 42 {
            break;
        }
    }
    println!("{}", x);
}

値を返す loop

loop内のbreak文は、ループを抜けて値を返すことができる。

fn main() {
    let mut x = 0;
    let v = loop {
        x += 1;
        if x == 13 {
            break "13 を発見";
        }
    };
    println!("loop の戻り値: {}", v);
}

while

条件付きループで、条件がfalseとなると終了する。

fn main() {
    let mut x = 0;
    while x != 42 {
        x += 1;
    }
}

for

Rustではfor文が強力に改良されている。
イテレータとして評価される式を反復処理する。
(イテレータ:項目がなくなるまで、次の項目を返してくれるオブジェクト)
Rustでは整数のシーケンスを生成するイテレータを簡単に扱うことができる。

  • ..演算子:開始番号から終了番号の手前までの数値を生成するイテレータを作成する
  • ・・=演算子:開始番号から終了番号までの数値を生成するイテレータを作成する
fn main() {
    for x in 0..5 {
        println!("{}", x);
    }

    for x in 0..=5 {
        println!("{}", x);
    }
}

match

matchキーワードを用いることで、ある値について条件にマッチしたものの処理をすべて実行することができる。
記述したすべてのケースについて処理される。

fn main() {
    let x = 42;

    match x {
        0 => {
            println!("found zero");
        }
        // 複数の値にマッチ
        1 | 2 => {
            println!("found 1 or 2!");
        }
        // 範囲にマッチ
        3..=9 => {
            println!("found a number 3 to 9 inclusively");
        }
        // マッチした数字を変数に束縛
        matched_num @ 10..=100 => {
            println!("found {} number between 10 to 100!", matched_num);
        }
        // どのパターンにもマッチしない場合のデフォルトマッチが必須
        _ => {
            println!("found something else!");
        }
    }
}

ブロック文から値を返す

if,match,関数等のブロックは、同じ方法で値を返すことができる。
ブロックの最後がのない式であれば、それが戻り値として使用される。
if文で使用すると三項演算子のように使用できる。

fn example() -> i32 {
    let x = 42;
    // Rust の三項式
    let v = if x < 42 { -1 } else { 1 };
    println!("if より: {}", v);

    let food = "ハンバーガー";
    let result = match food {
        "ホットドッグ" => "ホットドッグです",
        // 単一の式で値を返す場合、中括弧は省略可能
        _ => "ホットドッグではありません",
    };
    println!("食品の識別: {}", result);

    let v = {
        // ブロックのスコープは関数のスコープから分離されている
        let a = 1;
        let b = 2;
        a + b
    };
    println!("ブロックより: {}", v);

    // Rust で関数の最後から値を返す慣用的な方法
    v + 4
}

fn main() {
    println!("関数より: {}", example());
}

まとめ

基本的な制御分においても、Rustの特徴が垣間見える。
イテレータを使用することで、柔軟かつ簡単に反復処理を記述できる。
また、各ブロック文が戻り値を返すことができるという点で、より簡潔にコーディングが可能なようになっている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?