Skip to content

Commit 69ac60e

Browse files
updated repo structure
1 parent 1db47f3 commit 69ac60e

6 files changed

Lines changed: 73 additions & 77 deletions

File tree

.github/workflows/ci.yml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,30 @@ name: Rust CI
22

33
on:
44
push:
5-
branches: [ main ]
65
pull_request:
7-
branches: [ main ]
8-
9-
env:
10-
CARGO_TERM_COLOR: always
116

127
jobs:
138
build:
149
runs-on: ubuntu-latest
1510

1611
steps:
17-
- name: Checkout repository
12+
- name: Checkout code
1813
uses: actions/checkout@v4
1914

2015
- name: Install Rust
2116
uses: dtolnay/rust-toolchain@stable
2217

23-
- name: Build
24-
run: cargo build --verbose
25-
26-
- name: Run tests
27-
run: cargo test --verbose
28-
2918
- name: Check formatting
30-
run: cargo fmt --all -- --check
19+
run: cargo fmt -- --check
3120

3221
- name: Run Clippy
33-
run: cargo clippy --all-targets
22+
run: cargo clippy --all-targets --all-features -- -D warnings
23+
24+
- name: Check project
25+
run: cargo check --workspace
26+
27+
- name: Check examples
28+
run: cargo check --examples
29+
30+
- name: Run tests
31+
run: cargo test --workspace

examples/error_handling.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

examples/functions.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// use std::io;
22
use std::io::{self, Write};
33

4-
fn add(x: i32, y: i32) -> i32{
5-
x+y // NOTE : In rust no ; means return
4+
fn add(x: i32, y: i32) -> i32 {
5+
x + y // NOTE : In rust no ; means return
66
}
77

8-
fn prod(x: i32, y: i32) -> i32{
9-
return x*y; // can work as well
8+
fn prod(x: i32, y: i32) -> i32 {
9+
return x * y; // can work as well
1010
}
1111

12-
fn grade (score : u32) -> &'static str{ // refers to a reference where the borrowed data is valid for the entire duration of the running program
12+
fn grade(score: u32) -> &'static str {
13+
// refers to a reference where the borrowed data is valid for the entire duration of the running program
1314
match score {
1415
90..=100 => "A",
1516
80..=89 => "B",
@@ -19,60 +20,59 @@ fn grade (score : u32) -> &'static str{ // refers to a reference where the borro
1920
}
2021
}
2122

22-
fn main() -> io::Result<()>{ // this is for safety so that program do not panic
23+
fn main() -> io::Result<()> {
24+
// this is for safety so that program do not panic
2325
println!("add(5,3) = {}", add(5, 3));
2426
println!("prod(5,3) = {}", prod(5, 3));
2527
println!("grade(95) = {}", grade(95));
26-
28+
2729
// Conditional expression
2830
let number = 67;
29-
30-
if number > 5{
31+
32+
if number > 5 {
3133
println!("{} is greater than 5", number);
32-
}else{
34+
} else {
3335
println!("{} is less than 5 ", number);
3436
}
35-
37+
3638
// In Rust you can assign if/else value to a variable
37-
let message = if number > 5{"big"} else {"small"};
39+
let message = if number > 5 { "big" } else { "small" };
3840
println!("Number is {}", message);
39-
41+
4042
// Loops
4143
let mut count = 0;
4244
let result = loop {
4345
count += 1;
44-
if count == 5{
45-
break count*2;
46+
if count == 5 {
47+
break count * 2;
4648
}
4749
};
48-
50+
4951
println!("Loop result = {}", result);
50-
52+
5153
count = 0;
52-
while count <5{
54+
while count < 5 {
5355
count += 1;
54-
5556
}
5657
println!("While loop result = {}", count);
57-
58+
5859
count = 0;
5960
for i in 0..=5 {
6061
count += i;
6162
}
6263
println!("For loop result = {}", count);
63-
64+
6465
// MATCH
65-
66+
6667
let cmd = "echo";
6768
match cmd {
6869
"quit" => println!("Exiting"),
6970
"help" => println!("help"),
7071
"echo" => println!("Hello world"),
7172
_ => println!("Unknown cmd"),
72-
7373
}
74-
75-
// MATCH can be used inside a variable
74+
75+
// MATCH can be used inside a variable
7676
let code = 404;
7777
let meaning = match code {
7878
200 => "OK",
@@ -81,30 +81,30 @@ fn main() -> io::Result<()>{ // this is for safety so that program do
8181
_ => "Unknown",
8282
};
8383
println!("HTTP {} : {}", code, meaning);
84-
84+
8585
println!("Score 95 = grade {}", grade(95));
8686
println!("Score 83 = grade {}", grade(83));
8787
println!("Score 60 = grade {}", grade(60));
88-
89-
88+
9089
// PART 5 : Reading user input
9190
println!("Type a command (echo / help / exit");
92-
91+
9392
let mut input = String::new();
94-
io::stdout().flush()?; // ensures the prompt apprears before the program waits for input
93+
io::stdout().flush()?; // ensures the prompt apprears before the program waits for input
9594
io::stdin().read_line(&mut input)?;
96-
97-
input= input.trim().to_string();
98-
99-
if input.is_empty(){
95+
96+
input = input.trim().to_string();
97+
98+
if input.is_empty() {
10099
println!("Please enter a command");
101100
} else {
102-
match input.as_str() { // when matching against 'input' use as_str() to ensure the type is correct
101+
match input.as_str() {
102+
// when matching against 'input' use as_str() to ensure the type is correct
103103
"echo" => println!("Hello world "),
104104
"help" => println!("Available commands : echo, help, exit"),
105105
"quit" => println!("Exiting ..."),
106106
other => println!("Unknown Command: {}", other),
107107
}
108108
}
109109
Ok(())
110-
}
110+
}

examples/struct_enums.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

examples/variables.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
fn main() {
2-
32
// PART 1 : Immutable vs Mutable Variables
43
let x = 5;
54
let mut y = 10;
6-
y = 20 ;
7-
5+
y = 20;
6+
87
println!("x = {} ", x);
98
println!("y = {} ", y);
10-
9+
1110
// PART 2 : Data Types
12-
11+
1312
let age: u32 = 25; // unsigned 32-bit integer -- can't be negative
1413
let count: i32 = 100; // signed 32=bit integer -- can be negative or positive
1514
let big: i64 = 1_000_000; // underscores make numbers readable
1615
let pi: f64 = 3.14159; // float
1716
let flag: bool = true; // boolean
1817
let letter: char = 'A'; // character
1918
let name: &str = "Hitesh Bhatnagar"; // fixed text known at compile time
20-
19+
2120
let greeting: String = String::from("Hello, Universe"); // owned text - flexible
22-
23-
// PART 3: Shadowing
24-
21+
22+
// PART 3: Shadowing
23+
2524
// shadowing allows redeclare a variable with the same name
2625
// Each let x creates a BRAND NEW VARIABLE
2726
let x = 5;
28-
let x = x*2; // new x created, old x is deleted
29-
let x = x+3; // another new x
30-
27+
let x = x * 2; // new x created, old x is deleted
28+
let x = x + 3; // another new x
29+
3130
println!("Shadowed x = {}", x); // 13
32-
31+
3332
let spaces = " "; // &str type
34-
let spaces = spaces.len(); // now usize ( a number ) -- totally fine
33+
let spaces = spaces.len(); // now usize ( a number ) -- totally fine
3534
println!("spaces = {}", spaces); // 3
36-
37-
// PART 4: mut V/S Shadowing
38-
35+
36+
// PART 4: mut V/S Shadowing
37+
3938
let mut score = 0;
4039
score = 20;
4140
println!(" score = {}", score);
42-
41+
4342
// Shadowing => Creates new variable, can change VALUE & TYPE
4443
let status = "inactive";
4544
println!("status = {}", status);
4645
let status = true; // changes type from &str to bool
4746
println!("status = {}", status);
48-
47+
4948
// PART5 : Constants
50-
49+
5150
const EULER_NUMBER: f32 = 2.71828;
52-
println!("Max euler_num= {}", EULER_NUMBER );
53-
54-
55-
56-
}
51+
println!("Max euler_num= {}", EULER_NUMBER);
52+
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
fn main(){
1+
fn main() {
22
/*
33
* Run examples for each topic to learn and see the results.
44
*/
55
println!("Happy Learning :)")
6-
}
6+
}

0 commit comments

Comments
 (0)