Skip to content

Instantly share code, notes, and snippets.

View vijairaj's full-sized avatar

vijairaj vijairaj

  • Chennai
View GitHub Profile
@vijairaj
vijairaj / playground.rs
Created December 13, 2018 15:12 — forked from rust-play/playground.rs
Code shared from the Rust Playground
fn print(n: Option<i32>) {
// conditionally destructure
if let Some(inner) = n {
println!("Val: {}", inner);
}
}
fn main() {
print(None);
print(Some(0));
@vijairaj
vijairaj / playground.rs
Created December 13, 2018 14:40 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/// Enum the great!
#[derive(Debug)]
enum DeviceKind {
USB { vid: u16, pid: u16 },
Serial { port: String },
}
fn main() {
let devices = [
@vijairaj
vijairaj / playground.rs
Created December 12, 2018 16:12 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/// 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
@vijairaj
vijairaj / playground.rs
Created December 12, 2018 15:05 — forked from rust-play/playground.rs
Code shared from the Rust Playground
/// various ways to init a struct
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let pt1 = Point { x: 11, y: 12 };
@vijairaj
vijairaj / rustc
Last active December 13, 2018 17:09
Rust compiler errors
|
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
@vijairaj
vijairaj / playground.rs
Created December 11, 2018 18:29 — forked from rust-play/playground.rs
Code shared from the Rust Playground
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);
}
@vijairaj
vijairaj / playground.rs
Created December 11, 2018 18:03 — forked from rust-play/playground.rs
Code shared from the Rust Playground
fn main() {
let n = 0;
let val = loop {
if n == 0 {
break (1, "done");
}
};
println!("val: {:?}", val)
@vijairaj
vijairaj / playground.rs
Created December 11, 2018 17:55 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// type inference
fn main() {
let val1: u32 = 42;
// ^^^ type specified explicitly
let val2 = 42;
// ^ type inferred
let val3: u32 = "42".parse().unwrap();
@vijairaj
vijairaj / playground.rs
Created December 11, 2018 17:54 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// 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);
}