Rust is a modern, statically-typed systems programming language that emphasizes safety, concurrency, and performance. This repository serves as a comprehensive guide to the fundamentals of Rust, covering everything from primitive data types to advanced concepts like ownership, borrowing, and control flow.
- Primitive Data Types
- Compound Data Types
- Functions
- Ownership
- Borrowing & References
- Variables & Mutability
- Constants
- Shadowing
- Comments
- If-Else Statements - Control Flow
- Loops Control - Flow
- Structs
- Enums
- Error Handling Techniques
- Collection Types
- Projects
To get started with Rust, you'll need to install the Rust toolchain on your system. Once installed, you can compile and run Rust programs using the rustc
compiler and the cargo
build tool.
Here's the updated guide with the additional information on running Rust code directly with rustc
:
-
Create a New Rust Project:
-
Navigate to the directory where you want to create your project.
-
Use the
cargo
command to create a new project. For example, to create a project namedhello_world
, run:cargo new hello_world
-
This command creates a new directory named
hello_world
with a basic project structure.
-
-
Build the Project:
-
In the project directory, run the following command to build your project:
cargo build
-
This compiles your project and generates an executable in the
target/debug
directory.
-
-
Run the Project:
-
To run the project, use the following command:
cargo run
-
This will compile and run your project in one step, displaying the output.
-
If you prefer not to use cargo
, you can compile and run Rust code directly with rustc
:
- Ensure Your Path is Set Up Correctly:
-
Since you just installed Rust, your shell might not have the updated PATH yet. To fix this, run:
source $HOME/.cargo/env
-
To make this change permanent for future sessions, add the line to your shell's startup file (
.bashrc
,.zshrc
, etc.):echo 'source $HOME/.cargo/env' >> ~/.bashrc
-
Then, reload your
.bashrc
file:source ~/.bashrc
-
This guide is based on the excellent Bek Brace YouTube channel, which provides comprehensive video tutorials on Rust fundamentals. Additionally, you can refer to the official Rust Book and the Rust by Example for in-depth documentation and examples.
By following these steps, you should be able to set up, write, build, and run Rust code on your local machine. Rust's tooling, especially with cargo
, makes managing projects and dependencies straightforward and efficient.