Skip to content

Instantly share code, notes, and snippets.

View dumindu's full-sized avatar
🦄
One step at a time

Dumindu Madunuwan dumindu

🦄
One step at a time
View GitHub Profile
struct Color (u8, u8, u8);
struct Kilometers(i32);
fn main() {
// creating an instance
let black = Color (0, 0, 0);
// destructure the instance using a `let` binding, this will not destruct black instance
let Color (r, g, b) = black;
println!("Black = rgb({}, {}, {})", r, g, b); //black = rgb(0, 0, 0);
@dumindu
dumindu / System Design.md
Created April 18, 2016 14:42 — forked from vasanthk/System Design.md
System Design Cheatsheet

#System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

##Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
struct Electron;
fn main() {
let x = Electron;
}
enum Day {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
enum FlashMessage {
Success, //a unit variant
Warning{ category: i32, message: String }, //a struct variant
Error(String) //a tuple variant
}
fn main() {
let mut form_status = FlashMessage::Success;
print_flash_message(form_status);
// generalizing functions
//-----------------------
fn takes_anything<T>(x: T) { // x has type T, T is a generic type
}
fn takes_two_of_the_same_things<T>(x: T, y: T) { // both x and y has same type
}
fn takes_two_things<T, U>(x: T, y: U) { // multiple types
}
@dumindu
dumindu / colors
Last active October 13, 2016 08:45
Green
`````
#02C386
#5fba7d
Blue
`````
#669AFB
#4099ff
#242646 (better align with #343857)
// 01 - - - - - - - - - - - - - - - - - - - - - -
fn getIdByUsername(username: &str) -> Option<usize> {
//if username can be found in the system, set userId
return Some(userId);
//else
None
}
//💭 So on above function, instead of setting return type as usize
// set return type as Option<usize>
// ------- code ----------
use std::io;
fn main() {
println!("Type someting!");
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => println!("You input {}({} bytes)", input, n),
Err(error) => println!("error: {}", error)
@dumindu
dumindu / string to double
Last active July 10, 2016 17:15
application breaks on line16
use std::io;
fn main() {
let i_default: u64 = 4;
let d_default: f64 = 4.0;
let s_default = "HackerRank ".to_string();
let mut input = String::new();
println!("Type second integer!");