-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
108 lines (88 loc) · 3.53 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
fn main() {
println!("Hello World without Cargo!");
}
//NOTES
//rustc main.rs
//./main
// In windows .pdb file is generated which contains debugging info.
//Variables
//let,const,static (Use mut to make mutable)
//Rust is statically typed;So need to explicilty decalre type of varible except using 'let'.
//Naming Convention
//{Variable,Function,Files}=snake_case
//{Constants, Statics}=SCREAMING_SNAKE_CASE
//{Type,Traits,Enums}=PascalCase
//Functions
//fn main(){ //Here the return type is empty tuple -> ()
// let _unused_variable=my_func(10);
//}
//fn my_func(x:u8) -> i32 {
// x as i32
//}
//
//Strings and Slices
//let my_str: &str = "my_str is a ref to string literal, stack stored, its value can't change and its size is fixed."
//let my_string: String = String::from("my_string is an instance of the String struct, heap allocated, can have unknown size at comiple time");
//String literal == string slice as &str refers to the part of the string.
//let my_string = String::from("The quick brown fox");
//let my_str: &str = &my_string[4..9]; // "quick"
//let my_arr: [usize; 5] = [1, 2, 3, 4, 5];
//let my_arr_slice: &[usize] = &my_arr[0..3]; // [1, 2, 3]
//The [T; n] notation is used to create an array of n elements of type T.
//Chars
//char==Unicode Scalar Value (USV), E.g. U+221E for inf
//String==array of chars
//let my_str: &str = "Hello, world!";
//let collection_of_chars: &str = my_str.chars().as_str();
//Numbers
//Unsigned Int (positive whole nos.): u8, u16, u32, u64, u128
//Signed Int (+ve and -ve whole nos.): i8, i16, i32, i64, i128
//Floating Point Numbers (+ve adn -ve fractions): f32, f64
//Structs
//Struct=custom data type used to group related data
//struct String {
// vec: Vec<u8>,
//}
//vec=dynamically sized array
//Instance of struc is declared by giving values to the fields:
// struct MyStruct {
// field_1: u8,
// }
//let my_struct = MyStruct { field_1: 0, };
//Enums
//Act as type as well as values
// enum MyErrors {
// BrainTooTired,
// TimeOfDay(String)
// CoffeeCupEmpty,
// }
// fn work() -> Result<(), MyErrors> { // Result is also an enum
// if state == "missing semi-colon" {
// Err(MyErrors::BrainTooTired)
// } else if state == "06:00" {
// Err(MyErrors::TimeOfDay("It's too early to work".to_string()))
// } else if state == "22:00" {
// Err(MyErrors::TimeOfDay("It's too late to work".to_string()))
// } else if state == "empty" {
// Err(MyErrors::CoffeeCupEmpty)
// } else {
// Ok(())
// }
// }
//Macros
//Similar to functions
//Piece of code which writes other code
//Diff with functions: 1)calling with (!) 2)can take variable number of args
//println!("Hello {}!",mystr);
//panic!("This is an error.")
//Ownership
//Each value has a variable as its owner.
//Only one onwer at a time.
//When the owner goes out of scope, the value will be dropped.
//By above 3 rules Rust prevents need of garbage collector and not require memory management explicitly.
// fn main() { // first_string is not declared yet -> has no value
// let first_string = String::from("freeCodeCamp"); // first_string is now owner of the value "freeCodeCamp"
// let second_string = first_string; // second_string takes ownership of the value "freeCodeCamp"
// println!("Hello, {}!", first_string); // first_string is NOT valid, because the value was moved to second_string
// }
//To solve above compile time error: let second_string: &String = &first_string; // first_string is still the owner of the value "freeCodeCamp"