Skip to content

Instantly share code, notes, and snippets.

@toshimasa-nanaki
Last active March 26, 2023 06:55
Show Gist options
  • Save toshimasa-nanaki/2f051ff1e8f24fedfbf60c56918b0f1f to your computer and use it in GitHub Desktop.
Save toshimasa-nanaki/2f051ff1e8f24fedfbf60c56918b0f1f to your computer and use it in GitHub Desktop.
Rust if文
fn main() {
let x = 5;
//if(x == 5) { //←こうも書けるが、コンパイル時に警告が出る
if x == 5 {
println!("x is 5");
} else {
println!("x isn't 5");
}
//ifは式だからこんなこともできる。(三項演算子はない)
let y = if x == 5 {
10
} else {
15
}; //y == 10
//let y = if x == 5 { 10 } else { 15 }; //←こんな感じで書くのが一般的
println!("y is {}", y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment