From da35fc365a96d781cb964551d69fb2d9e25c9705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartal=20L=C3=A6arsson?= Date: Tue, 29 Sep 2026 19:14:57 +0100 Subject: [PATCH] cpc --- .gitignore | 1 + common-programming-concepts/README.md | 7 ++++ common-programming-concepts/src/data_types.rs | 33 +++++++++++++++ common-programming-concepts/src/main.rs | 7 +++- common-programming-concepts/src/variables.rs | 40 +++++++++++++++++++ guessing-game/README.md | 4 ++ 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 common-programming-concepts/README.md create mode 100644 common-programming-concepts/src/data_types.rs create mode 100644 common-programming-concepts/src/variables.rs create mode 100644 guessing-game/README.md diff --git a/.gitignore b/.gitignore index ea8c4bf..d97515a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/.obsidian diff --git a/common-programming-concepts/README.md b/common-programming-concepts/README.md new file mode 100644 index 0000000..c816ce4 --- /dev/null +++ b/common-programming-concepts/README.md @@ -0,0 +1,7 @@ +# Common Programming Concepts + +> This chapter covers concepts that appear in almost every programming language and how they work in Rust. Many programming languages have much in common at their core. None of the concepts presented in this chapter are unique to Rust, but we’ll discuss them in the context of Rust and explain the conventions around using them. +> +> Specifically, you’ll learn about variables, basic types, functions, comments, and control flow. These foundations will be in every Rust program, and learning them early will give you a strong core to start from. + + diff --git a/common-programming-concepts/src/data_types.rs b/common-programming-concepts/src/data_types.rs new file mode 100644 index 0000000..efb6996 --- /dev/null +++ b/common-programming-concepts/src/data_types.rs @@ -0,0 +1,33 @@ +pub fn run() { + println!("=== 2. data types ==="); + println!("\n=== 2.1 floating points ===\n"); + + let x = 2.0; // f64 + let y: f32 = 3.0; // f32 + + println!("delcared floats: \nvar x: {x} \nvar y: {y}"); + + + println!("\n=== 2.2 numeric ops ===\n"); + // addition + let sum = 5 + 10; + + // subtraction + let difference = 95.5 - 4.3; + + // multiplication + let product = 4 * 30; + + // division + let quotient = 56.7 / 32.2; + let truncated = -5 / 3; // Results in -1 + + // remainder + let remainder = 43 % 5; + println!("5 + 10: {sum}\n95.5 - 4.3: {difference}\n4 * 30: {product}\n56.7 divided by 32.2: {quotient}\ntruncated -5 / 3: {truncated}\n43 % 5: {remainder}\n"); + + + + println!("\n=== 2.2 compound types ===\n"); + // Compound type +} diff --git a/common-programming-concepts/src/main.rs b/common-programming-concepts/src/main.rs index e7a11a9..48cc5dd 100644 --- a/common-programming-concepts/src/main.rs +++ b/common-programming-concepts/src/main.rs @@ -1,3 +1,8 @@ +mod variables; +mod data_types; + fn main() { - println!("Hello, world!"); + variables::run(); + println!("\n#####################\n"); + data_types::run(); } diff --git a/common-programming-concepts/src/variables.rs b/common-programming-concepts/src/variables.rs new file mode 100644 index 0000000..ee486f5 --- /dev/null +++ b/common-programming-concepts/src/variables.rs @@ -0,0 +1,40 @@ +pub fn run() { + println!("=== 1. variables ==="); + // NON Working code, variables are immutable by default + // let x = 5; + // println!("var x is assigned {x}"); + // + // x = 6; + // println!("var x is assigned {x}"); + + // This works, because we declared the variable as mutable + println!("\n=== 1.1 mutable variables===\n"); + let mut x = 5; + println!("The value of x is: {x}"); + x = 6; + println!("The value of x is: {x}"); + + // Declaring constants: constants are declared as const. They can NEVER be mutated. They also + // must have a type declaration. Convention is to use all uppercase, and word separators as + // underscore. + println!("\n=== 1.2 constants ===\n"); + const THREE_HOURS_IN_MINUTES: u32 = 60 * 60 * 3; + println!("Three hours in muntes: {THREE_HOURS_IN_MINUTES}"); + + // Variable shadowing + // variable shadowing is handy for altering the variables somewhat, like when getting user + // input, then doing to length instead of having it as a string. If you do this with mut + // variable, then you'll get compile time error. The variable shadowing is great for not having + // to constantly creating new variables which only have one final purpose. Like space, + // space_count and such. + println!("\n=== 1.3 variable shadowing ===\n"); + let y = 5; + let y = y + 1; + + { + let y = y * 3; + println!("The value of y in the inner scope is {y}"); + } + + println!("The value of y is {y}"); +} diff --git a/guessing-game/README.md b/guessing-game/README.md new file mode 100644 index 0000000..dc97982 --- /dev/null +++ b/guessing-game/README.md @@ -0,0 +1,4 @@ +> Let’s jump into Rust by working through a hands-on project together! This chapter introduces you to a few common Rust concepts by showing you how to use them in a real program. You’ll learn about `let`, `match`, methods, associated functions, external crates, and more! In the following chapters, we’ll explore these ideas in more detail. In this chapter, you’ll just practice the fundamentals. +> +> We’ll implement a classic beginner programming problem: a guessing game. Here’s how it works: The program will generate a random integer between 1 and 100. It will then prompt the player to enter a guess. After a guess is entered, the program will indicate whether the guess is too low or too high. If the guess is correct, the game will print a congratulatory message and exit. +