diff --git a/examples/branches b/examples/branches new file mode 100755 index 0000000..7c121db Binary files /dev/null and b/examples/branches differ diff --git a/examples/branches.rs b/examples/branches.rs new file mode 100644 index 0000000..899e04b --- /dev/null +++ b/examples/branches.rs @@ -0,0 +1,9 @@ +fn main() { + let number = 10; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} \ No newline at end of file diff --git a/examples/datatypes.txt b/examples/datatypes.txt new file mode 100644 index 0000000..7ee8423 --- /dev/null +++ b/examples/datatypes.txt @@ -0,0 +1,37 @@ +Scalar types: +- A scalar type represents a single value. +- Integers: + - unsigned: (u8, u16, u32, u64, u128, usize) + - signed: (i8, i16, i32, i64, i128, isize) + - defaults to u32 + - usize/isize change automatically depending on CPU architecture +- Floating point: + - f32, f64 + - defaults to f64 +- Booleans: + - true/false +- Char: + - Defined with '' as opposed to "" (which are used for strings) + - Can store unicode characters + +Compound types: +- Compound types can group multiple values into one type. + Rust has two primitive compound types: tuples and arrays. +- Tuple: + - A tuple is a general way of grouping together a number of values with a variety of types into + one compound type. Tuples have a fixed length: once declared, they cannot grow or shrink in size. + - We create a tuple by writing a comma-separated list of values inside parentheses. + Each position in the tuple has a type, and the types of the different values in the tuple don’t + have to be the same. + + okay i cant be bothered typing more: https://doc.rust-lang.org/stable/book/ch03-02-data-types.html + +- Arrays: + - defined with square brackets [] + - all elements must be of the same type + - fixed length + - Arrays are defined like so: + - let a: [i32; 5] = [1, 2, 3, 4, 5]; + - the first square brackets contain the type and the length + - Array elements are fetched by using the index of the object a[0] = 1, a[1] = 2, etc + diff --git a/examples/datatypes_operations.rs b/examples/datatypes_operations.rs new file mode 100644 index 0000000..0a784bb --- /dev/null +++ b/examples/datatypes_operations.rs @@ -0,0 +1,16 @@ +fn main() { + // addition + let sum = 5 + 10; + + // subtraction + let difference = 95.5 - 4.3; + + // multiplication + let product = 4 * 30; + + // division + let quotient = 56.7 / 32.2; + + // remainder + let remainder = 43 % 5; +} diff --git a/examples/functions.txt b/examples/functions.txt new file mode 100644 index 0000000..1fbec31 --- /dev/null +++ b/examples/functions.txt @@ -0,0 +1,19 @@ +https://doc.rust-lang.org/stable/book/ch03-03-how-functions-work.html + +- Rust uses snake case as the standard style for variable and funtion names (like_this) +- Arguments in rust functions require that you specify the type + - Example: + - fn main() { + another_function(5); + } + + fn another_function(x: i32) { + println!("The value of x is: {}", x); + } +- Expressions dont end with semicolons +- Functions automatically return the last line in them as the return value +- The value of the return value is specified like so: + - fn five() -> i32 { + 5 + } +- You can use a return statement to return early diff --git a/examples/loop_return_value.rs b/examples/loop_return_value.rs new file mode 100644 index 0000000..6ffdab5 --- /dev/null +++ b/examples/loop_return_value.rs @@ -0,0 +1,13 @@ +fn main() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + println!("The result is {}", result); +}