Home
Rust Programming Language
Rust is a modern, fast, and memory-safe programming language. It is mainly used to build system-level software, high-performance applications, and secure programs. Rust is beginner-friendly in concepts and also powerful enough for professional developers.
What is Rust Programming Language?
Rust is a general-purpose programming language designed to provide high performance and memory safety without using a garbage collector. It helps developers write reliable and efficient code and prevents common bugs like null pointer and data race issues.
History of Rust
Rust was first started as a personal project by Graydon Hoare in 2006. Later, Mozilla officially sponsored Rust, and the first stable version of Rust was released in 2015.
Who Developed Rust?
Rust was originally developed by Graydon Hoare and later supported by Mozilla Foundation. Today, Rust is maintained by the Rust open-source community.
Applications of Rust
- System programming
- Operating systems
- WebAssembly applications
- Game engines
- Network services
- Embedded systems
Features of Rust
- Memory safety without garbage collection
- High performance
- Strong type system
- Concurrency without data races
- Open-source language
- Modern compiler and tooling
Advantages of Using Rust
- Prevents memory-related bugs
- Very fast execution
- Safe and reliable code
- Excellent community support
- Ideal for system-level programming
Basics of Rust Programming
Rust programs use a clean and expressive syntax. Variables are immutable by default, which helps write safer code. Rust supports variables, data types, operators, functions, and control statements.
Hello World Program in Rust
fn main() {
println!("Hello, World!");
}
Hello, World!
Rust Comments
Comments are used to explain code and improve readability.
// This is a single-line comment
/*
This is a
multi-line comment
*/
Variables in Rust
Variables in Rust are immutable by default. To make a variable mutable, use the mut keyword.
let name = "Sudhir";
let mut age = 25;
println!("{}", name);
println!("{}", age);
Sudhir
25
Conditional Statements in Rust
Conditional statements are used to make decisions in a program. Rust mainly supports if, if-else, and match statements.
if-else Example
let marks = 75;
if marks >= 40 {
println!("Pass");
} else {
println!("Fail");
}
Pass
Loops in Rust
Loops are used to repeat a block of code. Rust supports loop, while, and for loops.
for Loop Example
for i in 1..=5 {
println!("{}", i);
}
1
2
3
4
5
while Loop Example
let mut i = 1;
while i <= 3 {
println!("{}", i);
i += 1;
}
1
2
3
Functions in Rust
Functions are used to organize code and reuse it whenever required.
fn add(a: i32, b: i32) {
println!("{}", a + b);
}
fn main() {
add(10, 20);
}
30
Arrays in Rust
Arrays are used to store multiple values of the same type.
let numbers = [1, 2, 3, 4, 5];
println!("{:?}", numbers);
[1, 2, 3, 4, 5]
Tuples in Rust
Tuples store multiple values of different types.
let student = ("Rahul", 21);
println!("{}", student.0);
Rahul
Rust Ownership Concept
Ownership is Rust’s most unique feature. It helps manage memory safely without a garbage collector.
let s = String::from("Rust");
println!("{}", s);
Rust
Rust Structs
Structs are used to create custom data types.
struct Person {
name: String,
}
fn main() {
let p = Person {
name: String::from("Amit"),
};
println!("{}", p.name);
}
Amit
Enums in Rust
Enums are used to define multiple possible values.
enum Direction {
North,
South,
East,
West,
}
fn main() {
let move_dir = Direction::North;
println!("Direction selected");
}
Direction selected
Error Handling in Rust
Rust handles errors using Result and Option types instead of exceptions.
fn divide(a: i32, b: i32) -> Result {
if b == 0 {
Err("Cannot divide by zero".to_string())
} else {
Ok(a / b)
}
}
Compilation successful
Rust Programming Language Summary
Rust is a fast, safe, and modern programming language. It is widely used for system programming and performance-critical applications. Rust helps developers write reliable code without common memory errors.
Why Learn Rust?
Rust is highly demanded in the industry due to its safety and performance. It is used by companies like Mozilla, Microsoft, Amazon, and Google, making it a great career choice.
Rust Programs (Examples)
We have written many Rust programming language example programs to help you understand Rust concepts easily. These examples are explained in a simple way for better learning. To practice Rust programs topic-wise, you can check our Rust programs section, where examples are organized category-wise for beginners and learners.
Rust Programs
Advertisement
Advertisement