Skip to content

Commit 847ae0c

Browse files
rust basic [2]
1 parent f13c9b6 commit 847ae0c

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

readme.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# RUST BASIC Powered By AI
2+
3+
Welcome to the **Rust Basics** repository, a collection of foundational Rust programming topics. This project is designed to help learners get started with Rust by exploring key concepts such as variables, data types, control flow, and more.
4+
5+
## Overview
6+
7+
Rust is a systems programming language that emphasizes performance, safety, and concurrency. This repository will guide you through some of the most important and basic features of the language.
8+
9+
## Topics Covered
10+
11+
Here is an organized list of topics with a brief description for each:
12+
13+
| Topic | Description |
14+
| ------------------------ | ------------------------------------------------------------------------------------------ |
15+
| **Hello World** | A simple "Hello, World!" program to get started with Rust. |
16+
| **Cargo** | Learn how to use Cargo, the Rust package manager and build system. |
17+
| **Unit Test** | Writing and running unit tests to ensure code correctness. |
18+
| **Variable** | Understanding variable declarations, mutability, and scoping. |
19+
| **Comment** | Using comments in Rust code for clarity and documentation. |
20+
| **Data Type** | Exploring the basic data types available in Rust (e.g., integers, floats, booleans, etc.). |
21+
| **Number** | Working with numeric types, including integer and floating-point numbers. |
22+
| **Numeric Operations** | Performing basic arithmetic operations like addition, subtraction, and multiplication. |
23+
| **Boolean** | Understanding boolean values and their role in logic and conditions. |
24+
| **Comparison Operators** | Using comparison operators (e.g., `==`, `!=`, `<`, `>`) to compare values. |
25+
| **Boolean Operators** | Using logical boolean operators (AND, OR, NOT) to combine boolean values. |
26+
| **Char** | Working with characters and understanding the char data type. |
27+
| **Array** | Introduction to arrays in Rust and how to work with them. |
28+
| **Constant** | Defining and using constants in Rust to store fixed values. |
29+
| **Variable Scope** | Understanding the scope of variables and how they affect code behavior. |
30+
31+
## How to Run the Code
32+
33+
To run the examples in this repository:
34+
35+
1. Clone the repository to your local machine:
36+
37+
```bash
38+
git clone https://github.com/yogithesymbian/rust_basic.git
39+
cd rust_basic
40+
```
41+
42+
2. Make sure you have Rust installed on your machine. If not, you can install it from [Rust's official site](https://www.rust-lang.org/learn/get-started).
43+
44+
3. Each topic is separated into a `src` file (e.g., `src/hello_world.rs`, `src/cargo.rs`). You can compile and run the code in any of these files with Cargo:
45+
46+
```bash
47+
cargo run --example hello_world
48+
```
49+
50+
Replace `hello_world` with the name of any example you want to run.
51+
52+
## Contributing
53+
54+
Feel free to fork the repository, make changes, and create pull requests to contribute new examples, improve documentation, or fix bugs.
55+
56+
## License
57+
58+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
59+
60+
---
61+
62+
Thank you for exploring Rust with this basic guide! 🎉

src/array.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub fn main() {
2+
// Fixed-size array with 5 elements of type i32
3+
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
4+
5+
println!("First element: {}", numbers[0]);
6+
println!("Second element: {}", numbers[1]);
7+
8+
// Iterating over the array
9+
for number in numbers.iter() {
10+
println!("Array element: {}", number);
11+
}
12+
}

src/char.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub fn main() {
2+
let char1: char = 'R';
3+
let char2: char = 'u';
4+
let char3: char = 's';
5+
let char4: char = 't';
6+
7+
println!("Character 1: {}", char1);
8+
println!("Character 2: {}", char2);
9+
println!("Character 3: {}", char3);
10+
println!("Character 4: {}", char4);
11+
12+
// Combining characters into a string
13+
let word = format!("{}{}{}{}", char1, char2, char3, char4);
14+
println!("Word formed: {}", word);
15+
}

src/constant.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Define a constant
2+
const MAX_POINTS: u32 = 100_000;
3+
4+
pub fn main() {
5+
println!("The maximum points are: {}", MAX_POINTS);
6+
}

src/main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
mod array;
2+
mod char;
3+
mod constant;
14
mod data_types;
25
mod numbers;
36
mod operator_boolean;
47
mod operators_comparison;
8+
mod variable_scope;
59
mod variables;
610

711
fn main() {
@@ -17,4 +21,12 @@ fn main() {
1721
operators_comparison::main();
1822
println!("\n=========boolean_operator=========");
1923
operator_boolean::main();
24+
println!("\n=========char=========");
25+
char::main();
26+
println!("\n=========array=========");
27+
array::main();
28+
println!("\n=========constant=========");
29+
constant::main();
30+
println!("\n=========variable scope=========");
31+
variable_scope::main();
2032
}

src/variable_scope.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn main() {
2+
let x: i32 = 5; // x is valid within the main function's scope
3+
println!("x inside main: {}", x);
4+
5+
{
6+
let y: i32 = 10; // y is valid inside this inner scope
7+
println!("y inside inner scope: {}", y);
8+
}
9+
10+
// println!("y outside inner scope: {}", y); // This will cause a compile-time error
11+
12+
// x is still accessible here
13+
println!("x outside inner scope: {}", x);
14+
}

0 commit comments

Comments
 (0)