cpc
This commit is contained in:
@@ -1 +1,2 @@
|
||||
/target
|
||||
/.obsidian
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
mod variables;
|
||||
mod data_types;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
variables::run();
|
||||
println!("\n#####################\n");
|
||||
data_types::run();
|
||||
}
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user