Skip to content

Commit

Permalink
Merge pull request #1 from sohilladhani/dev
Browse files Browse the repository at this point in the history
Update master branch
  • Loading branch information
sohilladhani authored Sep 23, 2021
2 parents dc9b01b + bd8e011 commit b2929a8
Show file tree
Hide file tree
Showing 48 changed files with 1,646 additions and 203 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#https://stackoverflow.com/q/43667176

# Generated files
/target/

# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
#Cargo.lock
2 changes: 1 addition & 1 deletion chapter_02/guessing_game/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
# Cargo.lock
Cargo.lock
202 changes: 0 additions & 202 deletions chapter_02/guessing_game/Cargo.lock

This file was deleted.

8 changes: 8 additions & 0 deletions chapter_03/control_flow/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#https://stackoverflow.com/q/43667176

# Generated files
/target/

# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
Cargo.lock
7 changes: 7 additions & 0 deletions chapter_03/control_flow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "control_flow"
version = "0.1.0"
authors = ["Sohil Ladhani <[email protected]>"]
edition = "2018"

[dependencies]
40 changes: 40 additions & 0 deletions chapter_03/control_flow/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
fn main() {
let num = 3;

/* good ol' if-else */
if num < 5 {
println!("num is less than 5");
} else {
println!("num is not less than 5");
}

/* The condition must always be 'bool' otherwise rust will throw error */
/*if num {
println!("num is 3");
}*/
/* shadowing of 'num' variable */
let num = 7;

if num % 4 == 0 {
println!("num is divisible by 4");
} else if num % 3 == 0 {
println!("num is divisible by 3");
} else if num % 2 == 0 {
println!("num is divisible by 2");
} else {
println!("num is not divisible by 4,3,2");
}

/* We can use if on the right side of the if statement
* Meaning a variable is assigned a value if the condition holds*/

let condition = true;
let number = if condition {
5
} else {
6
};

println!("The value of number is: {}", number);

}
8 changes: 8 additions & 0 deletions chapter_03/data_types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#https://stackoverflow.com/q/43667176

# Generated files
/target/

# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
#Cargo.lock
4 changes: 4 additions & 0 deletions chapter_03/data_types/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions chapter_03/data_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "data_types"
version = "0.1.0"
authors = ["Sohil Ladhani <[email protected]>"]
edition = "2018"

[dependencies]
78 changes: 78 additions & 0 deletions chapter_03/data_types/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
fn main() {

/* There are 2 kinds of data types */
/* 1. Scalar Types 2. Compound Types */

/* 1. Scalar Types */
/* There are 4 types of scalar types */

/* i. Integer type */
/* Variants:
* i8, u8
* i16, u16
* i32 (default), u32
* i64, u64
* isize, usize */
let integer: u32 = 250;
println!("The integer value is {}", integer);

/* ii. Floating type */
/* Variants:
* f32, f64 (default) */
let float: f64 = 3.5;
println!("The float value is {}", float);

/* iii. Boolean type */
/* true or false */
let true_value = true;
let false_value: bool = false; // with explicit type annotation
println!("Does the Earth rotate around the Sun?: {}", true_value);
println!("Is the Earth flat?: {}", false_value);

/* iv. Character */
let c = 'c';
let z = '\u{2602}';
println!("Characters: {} {}", c, z);

/* 2. Compound Types */

/* i. Tuples */
/* A tuple is a general way of grouping together some number of other
* values with a variety of types (heterogeneous elements)
* into one compound type */
let tup: (i32, f64, u8) = (1992, 6.0, 2);

/* To get the individual values out of a tuple, we can use pattern
* matching to "destructure" a tuple value, like this: */
let (y, m, d) = tup;
println!("DOB is: {}-{}-{}", y, m, d);

/* Another way to access elements of a tuple */
let year = tup.0;
let month = tup.1;
let date = tup.2;
println!("DOB is: {}-{}-{}", year, month, date);

/* ii. Arrays */
/* Arrays are a way to group homogeneous elements. i.e. Each element
* in an array is of same type.
* Arrays in Rust have fixed length. Once declared, they cannot grow
* or shrink in size. */
let the_array = [1, 2, 3, 4, 5];

/* Accessing the array elements */
let zeroth = the_array[0];
let first = the_array[1];
println!("Array elements are {} {}", zeroth, first);

/* Accessing an array element outside of the bounds result in a 'panic',
* which is a runtime error (and not compile time) */

/* Below code creates a panic like this:
* thread 'main' panicked at 'index out of bounds: the len is 5 but the
* index is 10', src/main.rs:71:46 note: Run with `RUST_BACKTRACE=1`
* for a backtrace.*/
//let index = 10;
//println!("Array element at index is {}", the_array[index]);
}

8 changes: 8 additions & 0 deletions chapter_03/exercises/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#https://stackoverflow.com/q/43667176

# Generated files
*/target/*

# The library shouldn't decide about the exact versions of
# its dependencies, but let the downstream crate decide.
*/Cargo.lock
3 changes: 3 additions & 0 deletions chapter_03/exercises/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Exercises

These are the exercises mentioned at the end of chapter 3
7 changes: 7 additions & 0 deletions chapter_03/exercises/fibonacci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "fibonacci"
version = "0.1.0"
authors = ["Sohil Ladhani <[email protected]>"]
edition = "2018"

[dependencies]
Loading

0 comments on commit b2929a8

Please sign in to comment.