Last active
October 16, 2024 19:54
-
-
Save dacr/096963be64589ee2599dce488ef5aa01 to your computer and use it in GitHub Desktop.
hello rust expressions / published by https://github.com/dacr/code-examples-manager #9c87bdfa-32f5-4ebe-8bbe-a4ff74057715/ec5c13983bc38791bbefce5a8862f24d24d79136
This file contains hidden or 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
#!/usr/bin/env rust-script | |
// summary : hello rust expressions | |
// keywords : rust, expressions, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 9c87bdfa-32f5-4ebe-8bbe-a4ff74057715 | |
// created-on : 2024-10-12T21:26:15+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : ./$file | |
fn main() { | |
// instruction (separated using ;) | |
let v1 = 5; | |
// expression (no ;) ⚠️ | |
let v2 = { | |
let mut x = 1; | |
x += 5; | |
x | |
}; | |
assert_eq!(v2, 6); | |
let text = if v2 < 0 { | |
"Value is negative" // no ; ⚠️ because expression | |
} else if v2 > 0 { | |
"Value is positive" // no ; ⚠️ because expression | |
} else { | |
"Value is zero" // no ; ⚠️ because expression | |
}; | |
println!("{}", text); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment