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 print(n: Option<i32>) { | |
// conditionally destructure | |
if let Some(inner) = n { | |
println!("Val: {}", inner); | |
} | |
} | |
fn main() { | |
print(None); | |
print(Some(0)); |
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
/// Enum the great! | |
#[derive(Debug)] | |
enum DeviceKind { | |
USB { vid: u16, pid: u16 }, | |
Serial { port: String }, | |
} | |
fn main() { | |
let devices = [ |
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
/// Methods | |
struct Point { x: i32, y: i32 } | |
impl Point { | |
fn from(x: i32, y: i32) -> Point { // Associated function = no self | |
Point { x, y } | |
} | |
fn tostr(&self) -> String { // Method takes self as param |
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
/// various ways to init a struct | |
#[derive(Debug)] | |
struct Point { | |
x: i32, | |
y: i32, | |
} | |
fn main() { | |
let pt1 = Point { x: 11, y: 12 }; |
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
| | |
13 | let x, y = 21, 22; | |
| -^-- help: try adding parentheses: `(x, y)` | |
---- | |
| | |
24 | println!("val {:?} {:?} {:?}", pt, pt2, pt3); | |
| ^^ did you mean `pt1`? | |
---- | |
error[E0499]: cannot borrow `vec` as mutable more than once at a time | |
--> src/main.rs:5:9 |
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 cat = '😻'; // U+1F63B // UTF8: F0 9F 98 BB | |
println!("val {} {:x?}", cat, cat as i32); | |
let cat_str = String::from("😻"); | |
let cat_bytes = cat_str.as_bytes(); | |
println!("val {} {:x?}", cat_str, cat_bytes); | |
} |
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 n = 0; | |
let val = loop { | |
if n == 0 { | |
break (1, "done"); | |
} | |
}; | |
println!("val: {:?}", val) |
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
// type inference | |
fn main() { | |
let val1: u32 = 42; | |
// ^^^ type specified explicitly | |
let val2 = 42; | |
// ^ type inferred | |
let val3: u32 = "42".parse().unwrap(); |
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
// tuple | |
fn main() { | |
let (x, y, z) = (1, 2.0, "abc"); | |
let xyz = (1, 2.0, (10, 20)); | |
println!("{} {} {}", x, y, z); | |
println!("{} {} {:?}", xyz.0, xyz.1, xyz.2); | |
} |