Skip to content

Commit ec73fdf

Browse files
committed
Initial commit (hello, guessing_game, vars_types)
1 parent d8abc38 commit ec73fdf

File tree

7 files changed

+215
-10
lines changed

7 files changed

+215
-10
lines changed

.gitignore

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# Generated by Cargo
2-
# will have compiled files and executables
3-
/target/
4-
5-
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6-
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
7-
Cargo.lock
8-
9-
# These are backup files generated by rustfmt
10-
**/*.rs.bk
1+
# Ignore backup files generated by rustfmt
2+
**/**/*.rs.bk
3+
4+
# Ignore all project targets
5+
**/target/
6+
7+
# Ignore Cargo.lock
8+
**/Cargo.lock
9+
10+
# Ignore all my Clion stuff
11+
/.idea/

1_hello_cargo/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "hello_cargo"
3+
version = "0.1.0"
4+
authors = ["matty"]
5+
6+
[dependencies]

1_hello_cargo/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

2_guessing_game/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "guessing_game"
3+
version = "0.1.0"
4+
authors = ["mattyp"]
5+
6+
[dependencies]
7+
8+
rand = "0.4.0"

2_guessing_game/src/main.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
extern crate rand;
2+
3+
use std::io;
4+
use std::cmp::Ordering;
5+
use rand::Rng;
6+
7+
fn main() {
8+
println!("Guess the number!");
9+
10+
let secret_number = rand::thread_rng().gen_range(1, 101); // 1-100
11+
12+
println!("The secret number is {}", secret_number);
13+
14+
loop {
15+
println!("Please input your guess.");
16+
17+
let mut guess = String::new();
18+
19+
io::stdin().read_line(&mut guess)
20+
.expect("Failed to read line");
21+
22+
let guess: u32 = match guess.trim().parse() {
23+
Ok(num) => num, // return num given by Ok value
24+
Err(_) => { // Catch all with '_'
25+
println!("That's not a number! Try again!");
26+
continue; // Continue loop
27+
}
28+
};
29+
30+
println!("You guessed: {}", guess);
31+
32+
match guess.cmp(&secret_number) {
33+
Ordering::Less => println!("Too low!"),
34+
Ordering::Greater => println!("Too high!"),
35+
Ordering::Equal => {
36+
println!("You win!");
37+
break; // Exit loop
38+
},
39+
}
40+
}
41+
}

3_variables_and_types/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "variables"
3+
version = "0.1.0"
4+
authors = ["matty"]
5+
6+
[dependencies]

3_variables_and_types/src/main.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
const MAX_POINTS: u32 = 100_000; // Can add '_' to num literals for readability
2+
3+
4+
fn main() {
5+
println!("\n=== Using the Constant ===");
6+
println!("Max points = {}", MAX_POINTS);
7+
8+
println!("\n=== Mutability ===");
9+
mutability();
10+
11+
println!("\n=== Shadowing ===");
12+
shadowing();
13+
14+
println!("\n=== Mutating Types (illegal) ===");
15+
mutate_type();
16+
17+
println!("\n=== Data Types (see source code) ===");
18+
data_types();
19+
compound_data_types();
20+
numeric_operations();
21+
}
22+
23+
fn mutability() {
24+
let mut x = 5; // mut needed to make it mutable
25+
println!("The value of x is: {}", x);
26+
x = 6;
27+
println!("The value of x is now: {}", x);
28+
}
29+
30+
31+
fn shadowing() {
32+
let x = 5;
33+
34+
let x = x + 1;
35+
36+
let x = x * 2;
37+
38+
println!("The value of x is: {}", x);
39+
}
40+
41+
fn mutate_type() {
42+
let spaces = " ";
43+
println!("spaces = \"{}\"", spaces);
44+
let spaces = spaces.len();
45+
println!("spaces = \"{}\" \
46+
(shadowed - can't actually change type of mutated variable)",
47+
spaces);
48+
49+
// This is fine because we are just shadowing the first with the second
50+
// However the following is illegal:
51+
/*
52+
let mut spaces = " ";
53+
spaces = spaces.len();
54+
*/
55+
}
56+
57+
#[allow(unused_variables)] // ignore compiler warnings for unused_variables
58+
fn data_types() {
59+
// Integer Types
60+
let signed_8_bit: i8 = 0;
61+
let unsigned_8_bit: u8 = 0;
62+
let signed_16_bit: i16 = 0;
63+
let unsigned_16_bit: u16 = 0;
64+
let signed_32_bit: i32 = 0; // default
65+
let unsigned_32_bit: u32 = 0;
66+
let signed_64_bit: i64 = 0;
67+
let unsigned_64_bit: u64 = 0;
68+
let signed_128_bit: i128 = 0;
69+
let unsigned_128_bit: u128 = 0;
70+
let signed_32or64_bit: isize = 0; // architecture-dependent
71+
let unsigned_32or64_bit: usize = 0; // architecture-dependent
72+
73+
// Integer Literals
74+
let decimal_type = 98_222; // defaults to i32
75+
let hex_type = 0xff; // defaults to i32
76+
let octal_type = 0o77; // defaults to i32
77+
let binary_type = 0b1111_0000; // defaults to i32
78+
let byte_type = b'A'; // defaults to u8 (only)
79+
let non_byte_type_with_suffix = 57u8; // suffix forces type
80+
81+
// Floating Point Types
82+
let fp_64 = 2.0; // f64 (default)
83+
let fp_32: f32 = 3.0; // f32
84+
85+
// Boolean Types
86+
let t = true;
87+
let f: bool = false; // with explicit type annotation (not needed)
88+
89+
// Character types
90+
let char_lower = 'z';
91+
let char_upper = 'Z';
92+
let emoji = '😻'; // Note unicode scalar, so more than just ASCII!
93+
// Range: U+0000 to U+D7FF and U+E000 to U+10FFFF inclusive
94+
}
95+
96+
#[allow(unused_variables)] // ignore compiler warnings for unused_variables
97+
fn compound_data_types() {
98+
// Tuple Type
99+
let tup_explicit: (i32, f64, u8) = (500, 6.4, 1);
100+
let tup_implicit = (500, 6.4, 1);
101+
102+
// now to get an individual value (destructuring):
103+
let (x, y, z) = tup_implicit; // now x = 500, y = 6.4, z = 1
104+
105+
// Alternatively, use a period (.)
106+
let five_hundred = tup_implicit.0;
107+
let six_point_four = tup_implicit.1;
108+
let one = tup_implicit.2;
109+
110+
// Array Type
111+
let arr_explicit: [u8;5] = [1, 2, 3, 4, 5]; // Limited to 5 elements [u8;5]
112+
let arr_implicit = [1, 2, 3, 4, 5]; // Limited to 5 elements [i32;5]
113+
let months = ["January", "February", "March", "April", "May",
114+
"June", "July", "August", "September", "October", "November",
115+
"December"]; // [&str; 12]
116+
117+
// Array Access
118+
let first_num = arr_implicit[0]; // i32
119+
let second_month = months[1]; // &str
120+
// let thirteenth_month = months[12]: runtime error! (not compile)
121+
}
122+
123+
#[allow(unused_variables)] // ignore compiler warnings for unused_variables
124+
fn numeric_operations() {
125+
// addition
126+
let sum = 5 + 10;
127+
128+
// subtraction
129+
let difference = 95.5 - 4.3;
130+
131+
// multiplication
132+
let product = 4 * 30;
133+
134+
// division
135+
let float_quotient = 56.7 / 32.2; // = 1.7608695652173911
136+
let int_quotient2 = 5/2; // = 2
137+
138+
// remainder
139+
let remainder = 43 % 5;
140+
}

0 commit comments

Comments
 (0)