Learn coding with amol

PART= 16 Rust

hello my name is amol sharma or vineet and this is my 16th part of learn coding with amol and in this part we are going RUST basics to advance.



 

Rust Programming Language – Complete Guide

Rust Programming Language – Complete Guide

Definition: Rust is a modern, fast, and memory-safe programming language used for system programming, game engines, operating systems, and high-performance applications.

1. Hello World Program

Definition: This is the basic Rust program used to print text on the screen.
fn main() {
    println!("Hello, World!");
}

2. Variables in Rust

Definition: Variables in Rust store data. By default, variables are immutable (cannot be changed).
fn main() {
    let x = 10;
    println!("{}", x);
}

3. Mutable Variables

Definition: Mutable variables allow changing the value using the mut keyword.
fn main() {
    let mut y = 5;
    y = 10;
    println!("{}", y);
}

4. Data Types

Definition: Rust supports integer, float, boolean, and character data types.
fn main() {
    let a: i32 = 10;
    let b: f64 = 3.14;
    let c: bool = true;
    let d: char = 'R';
    println!("{} {} {} {}", a, b, c, d);
}

5. Functions

Definition: Functions are reusable blocks of code defined using the fn keyword.
fn greet() {
    println!("Welcome to Rust");
}

fn main() {
    greet();
}

6. If-Else Condition

Definition: Conditional statements control program flow based on conditions.
fn main() {
    let num = 10;
    if num > 5 {
        println!("Number is greater than 5");
    } else {
        println!("Number is 5 or less");
    }
}

7. Loops

Definition: Loops execute a block of code repeatedly.
fn main() {
    for i in 1..6 {
        println!("{}", i);
    }
}

8. Arrays

Definition: Arrays store multiple values of the same type.
fn main() {
    let numbers = [1, 2, 3, 4, 5];
    println!("{}", numbers[0]);
}

9. Ownership Concept

Definition: Ownership is Rust’s system for managing memory safely without a garbage collector.
fn main() {
    let s1 = String::from("Rust");
    let s2 = s1;
    println!("{}", s2);
}

10. Comments

Definition: Comments are used to explain code and are ignored by the compiler.
// This is a single-line comment

/* This is
a multi-line
comment */

Conclusion

Rust is a powerful language focused on performance and safety. It is widely used for system-level development, games, and modern applications.

Report a Bug

🐞 Report a Bug

OR

Comments

Popular posts from this blog

Learn coding with amol

Learn coding with amol

Learn coding with amol