Created
November 6, 2017 17:17
-
-
Save abhijith/a386de583bfcfcc6bb7ba5300ef0393c to your computer and use it in GitHub Desktop.
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
fn main() { | |
println!("hello world!"); | |
let foo = format!("hello rustacean!"); | |
println!("{}", foo); | |
let x = 5 + 10; | |
println!("x is {}", x); | |
#[allow(unused_variables)] | |
let y: i32 = 42; | |
// ignore unused_variable by starting var with _ | |
let _truth: bool = false; | |
// supress warning with rustc -A unsued_variables flag | |
// RUSTFLAGS="$RUSTFLAGS -A dead_code" cargo build | |
let mut khar: char = 'c'; | |
println!("khar = {}", khar); | |
khar = 'd'; | |
println!("khar = {}", khar); | |
let (a, b) = (1, "a"); | |
println!("a = {} b = {}", a, b); | |
println!("{0}, {1} {0}", "Bond", "James"); | |
println!( | |
"{second_name}, {first_name} {second_name}", | |
first_name = "James", | |
second_name = "Bond" | |
); | |
#[allow(dead_code)] | |
struct Structure(i32); | |
// println!("this won't print {:?}", Structure(2)); | |
#[allow(dead_code)] | |
struct UnPrintable(i32); | |
#[derive(Debug)] | |
struct Printable(i32); | |
println!("this will print printable {:?}", Printable(2)); | |
for x in 1..4 { | |
println!("{}", x); | |
} | |
let mut i = 0; | |
while i < 3 { | |
println!("while {}", i); | |
i += 1 | |
} | |
println!("{}", "abc" > "abcd"); | |
let mut n = 1; | |
println!("n = {}", n); | |
n = 2; | |
println!("n = {}", n); | |
let n = 3.14; // redeclaration into different type allowed | |
println!("n = {}", n); | |
if x > 0 && x < 10 { | |
println!("oh"); | |
} else if x == 15 { | |
println!("ah"); | |
} else { | |
println!("whatever"); | |
} | |
let mut z = 0; | |
// compiler will suggest you use loop to denote infinite loops if the attribute is not added | |
#[allow(while_true)] | |
while true { | |
if z == 5 { | |
println!("done counting to 4"); | |
break; | |
} else { | |
z += 1 | |
} | |
} | |
// write this instead of while true is what the compiler is suggesting | |
loop { | |
if z == 5 { | |
println!("done counting to 4"); | |
break; | |
} else { | |
z += 1 | |
} | |
} | |
let sentence = ["this", "is", "cool"]; | |
println!("{:?}", sentence); | |
println!("{}", sentence.len()); | |
#[allow(unused_mut, unused_variables)] | |
let mut coll_of_str = ["a", "b", "c"]; // type [&str); 3] | |
/* mismatched types | |
(expected an array with a fixed size of 3 elements, found one with 2 elements) [E0308] | |
*/ | |
// coll_of_str = ["a", "b"]; // type [&str); 2] -> overall type does not match | |
// coll_of_str = [1, 2, 3]; // expected &str but got integer | |
coll_of_str[2] = "Y"; | |
println!("{:?}", coll_of_str); | |
let fixed_coll = ["x"; 10]; // array of fixed size 10 initialized with "x" | |
println!("{:?}", fixed_coll); | |
// multi-dimensional array | |
let arr = [[0; 5]; 5]; | |
println!("{:?}", arr); | |
// vectors are dynamic arrays | |
let mut v: Vec<&str> = vec!["this", "is"]; | |
v.push("cool!"); | |
println!("{:?}", v); | |
v.pop(); | |
println!("{:?}", v); | |
v.insert(2, "rad"); | |
println!("{:?}", v); | |
v.remove(0); | |
println!("{:?}", v); | |
let hexadecimal = 0x10; | |
let decimal = 10; | |
let octal = 0o10; | |
let binary = 0b10; | |
println!("{} {} {} {}", hexadecimal, decimal, octal, binary); | |
let a: () = (); | |
let b = { | |
12; | |
87; | |
283 | |
}; | |
let c = { | |
12; | |
87; | |
283; | |
}; | |
let d = {}; | |
let e = if false {}; | |
let f = while false {}; | |
print!("{:?} {:?} {:?} {:?} {:?} {:?}", a, b, c, d, e, f); | |
#[allow(dead_code)] | |
enum Day { | |
Monday, | |
Tuesday, | |
Wednesday, | |
Thursday, | |
Friday, | |
Saturday, | |
Sunday, | |
} | |
let day = Day::Friday; | |
match day { | |
Day::Monday => println!("monday"), | |
Day::Tuesday => println!("tuesday"), | |
Day::Wednesday => println!("wednesday"), | |
Day::Thursday => println!("thursday"), | |
Day::Friday => println!("friday"), | |
Day::Saturday => println!("saturday"), | |
Day::Sunday => println!("sunday"), | |
} | |
#[allow(dead_code)] | |
enum Editor { | |
Emacs, | |
Vim, | |
Ed, | |
Nano, | |
}; | |
// let editor = Editor::Emacs; | |
// compiler error -> non-exhaustive pattern match | |
// match editor { | |
// Editor::Ed => "3", | |
// Editor::Emacs => "1", | |
// Editor::Vim => "2", | |
// } | |
#[allow(dead_code)] | |
enum Result { | |
Success(f64), | |
Failure(u16, char), | |
Uncertainty, | |
}; | |
// let outcome = Result::Success(23.67); | |
let outcome = Result::Failure(1200, 'X'); | |
match outcome { | |
Result::Success(value) => println!("Result: {}", value), | |
Result::Failure(error_code, module) => { | |
println!("Error n. {} in module {}", error_code, module) | |
} | |
Result::Uncertainty => {} | |
}; | |
match outcome { | |
Result::Success(value) if value != 10. => println!("Result: {}", value), // Guards | |
Result::Success(value) => println!("Result: {}", value), | |
Result::Failure(error_code, module) => { | |
println!("Error n. {} in module {}", error_code, module) | |
} | |
Result::Uncertainty => {} | |
}; | |
let tup = ("tuple", 1); | |
println!("{:?}", tup); | |
#[allow(dead_code)] | |
struct PureStructPoint { | |
x: u8, | |
y: u8, | |
}; | |
#[allow(dead_code)] | |
struct TupleStructPoint(u8, u8); | |
#[allow(dead_code)] | |
const MAX_LIMIT: u16 = 10; | |
// return type is () | |
// equivalent to fn cool() -> () { ... } | |
fn cool() { | |
println!("cool!") | |
} | |
cool(); | |
let res = if true { 1 } else { 2 }; | |
// let res = if true { 1 } else { 2.0 }; | |
/* | |
compiler error: | |
expected type `{integer}` found type `{float}` [E0308] | |
if and else have incompatible types (expected integral | |
variable, found floating-point variable) [E0308] | |
*/ | |
println!("{:?}", res); | |
fn sum(a: i32, b: i32) -> i32 { | |
a + b | |
} | |
println!("{}", sum(1, 2)); | |
println!("{}", sum(2, 2)); | |
fn inc(mut n: i32) -> i32 { | |
n += 1; | |
return n; | |
} | |
println!("{}", inc(1)); | |
fn add1(a: &mut [i32; 4]) { | |
for i in 0..4 { | |
a[i] += 1; | |
} | |
} | |
let mut arr = [1, 2, 3, 4]; | |
add1(&mut arr); | |
println!("{:?}", arr); | |
let num = 10; | |
let ref_num = # | |
println!("{} {} {}", num, *ref_num, ref_num); | |
// Generic fn | |
fn fun<T>(ch: char, num1: T, num2: T) -> T { | |
if ch == 'a' { num1 } else { num2 } | |
} | |
let a: i16 = fun::<i16>('a', 37, 41); | |
let b: f64 = fun::<f64>('b', 37.2, 41.1); | |
println!("{} {}", a, b); | |
let a: i16 = fun('a', 37, 41); | |
let b: f64 = fun('b', 37.2, 41.1); | |
println!("{} {}", a, b); | |
let a = fun('a', 37, 41); | |
let b = fun('b', 37.2, 41.1); | |
println!("{} {}", a, b); | |
fn simple<S>(simp: S) -> S { | |
simp | |
} | |
let s1 = simple(1); | |
let s2 = simple(1.0); | |
let s3 = simple('a'); | |
let s4 = simple("asdf"); | |
println!("{} {} {} {}", s1, s2, s3, s4); | |
struct Table<Pk> { | |
id: i64, | |
key: Pk, | |
} | |
let t = Table { key: "a", id: 1 }; | |
println!("{} -> {}", t.id, t.key); | |
let t = Table { key: 1, id: 1 }; | |
println!("{} -> {}", t.id, t.key); | |
let t = Table { key: "asdf", id: 1 }; | |
println!("{} -> {}", t.id, t.key); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment