This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
//ループラベル | |
'outer: for i in 0..10 { | |
'inner: for j in 1..11 { | |
if i % 2 != 0 { continue 'outer; } | |
if j % 2 == 0 { continue 'inner; } | |
println!("i: {}, j: {}", i, j); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
//break文が使える。 | |
let mut x = 7; | |
loop { | |
x -= 1; | |
println!("{}", x); | |
if x == 3 { break; } | |
} | |
//continue文が使える。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
//for(int i = 0; i < 10; i++)と同じ | |
for x in 0..10 { | |
println!("{}", x); | |
} | |
//何回目の繰り返しか知りたい場合は以下。 | |
for (index, value) in (5..10).enumerate() { | |
println!("index = {} and value = {}", index, value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
loop { | |
println!("infinite loop"); | |
} | |
//以下のようにも書けるが非推奨。 | |
//while true{ | |
// println!("infinite loop"); | |
//} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let x = 5; | |
//if(x == 5) { //←こうも書けるが、コンパイル時に警告が出る | |
if x == 5 { | |
println!("x is 5"); | |
} else { | |
println!("x isn't 5"); | |
} | |
//ifは式だからこんなこともできる。(三項演算子はない) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let function: fn(i32) -> i32 = times_number; //型として「fn(i32) -> i32」を宣言。 | |
println!("When x is doubled, {}.", function(7)); //上で宣言した変数を利用して関数呼び出し | |
} | |
fn times_number(x: i32) -> i32 { | |
x * 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
panic(); | |
//division_by_0(); | |
} | |
//実行中の現在のスレッドを与えられたメッセージとともにクラッシュさせるマクロを実行 | |
fn panic() -> ! { | |
panic!("This function never returns!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("When x is doubled, {}.", times_number(7)); | |
} | |
fn times_number(x: i32) -> i32 { | |
x * 2 //セミコロンがない=i32型で返す。(セミコロンがあると型なしになりコンパイルエラー) | |
// return x * 2; (もちろんこんなことはできる) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
print_2times_number(7); | |
print_multiply_number(7,8); | |
} | |
//第1引数を2倍した数を出力 | |
fn print_2times_number(x: i32) { | |
println!("When x is doubled, {}.", x * 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//注意!! 勉強メモ用にmainを並べているだけ。 | |
//実際に動かす場合はこの中から一つだけmainを選択すること。 | |
//(1)型推論を利用した宣言 | |
fn main() { | |
let x = 7; | |
println!("x is {}.", x); // x is 7. | |
} | |
//(2)型を定義 |