theory stuff
This commit is contained in:
parent
d98abc357c
commit
0db66fee98
6 changed files with 94 additions and 0 deletions
BIN
examples/branches
Executable file
BIN
examples/branches
Executable file
Binary file not shown.
9
examples/branches.rs
Normal file
9
examples/branches.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
fn main() {
|
||||||
|
let number = 10;
|
||||||
|
|
||||||
|
if number < 5 {
|
||||||
|
println!("condition was true");
|
||||||
|
} else {
|
||||||
|
println!("condition was false");
|
||||||
|
}
|
||||||
|
}
|
37
examples/datatypes.txt
Normal file
37
examples/datatypes.txt
Normal file
|
@ -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
|
||||||
|
|
16
examples/datatypes_operations.rs
Normal file
16
examples/datatypes_operations.rs
Normal file
|
@ -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;
|
||||||
|
}
|
19
examples/functions.txt
Normal file
19
examples/functions.txt
Normal file
|
@ -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
|
13
examples/loop_return_value.rs
Normal file
13
examples/loop_return_value.rs
Normal file
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in a new issue