Skip to content

AshishTechExplorer/RustApis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Rust is a systems programming language that emphasizes performance, safety, and concurrency. It is designed to be a modern alternative to languages like C and C++, offering powerful features while maintaining a focus on preventing common programming errors. Here's an overview of key aspects of Rust programming:

Key Features of Rust

  1. Memory Safety Without Garbage Collection

    • Rust uses ownership, borrowing, and lifetimes to ensure memory safety without needing a garbage collector. This prevents issues like dangling pointers and data races.
  2. Zero-Cost Abstractions

    • Rust provides high-level abstractions with minimal overhead. The language ensures that abstractions are as efficient as writing in lower-level code.
  3. Concurrency

    • Rust’s ownership system makes it easier to write concurrent code without data races. The language has built-in support for concurrency and parallelism.
  4. Immutable by Default

    • Variables are immutable by default, which encourages functional programming practices and reduces unintended side effects.
  5. Pattern Matching

    • Rust has a powerful pattern matching system that simplifies handling complex data structures.
  6. Rich Type System

    • Rust includes features like algebraic data types (enums), traits for defining shared behavior, and powerful generics.
  7. Tooling

    • Rust comes with a robust set of tools, including cargo (the package manager and build system), rustc (the compiler), and clippy (a linter).
  8. Cross-Platform

    • Rust supports cross-compilation, allowing you to build applications for various platforms from a single codebase.

Basic Rust Syntax and Concepts

Here’s a quick overview of some basic Rust concepts and syntax:

1. Hello World

The classic "Hello, World!" program in Rust:

fn main() {
    println!("Hello, world!");
}

2. Variables and Data Types

Variables in Rust are immutable by default. To make a variable mutable, use the mut keyword.

fn main() {
    let x = 5; // immutable variable
    let mut y = 10; // mutable variable
    y += 5;
    println!("x: {}, y: {}", x, y);
}

Rust has a strong, static type system. Some common data types include:

  • i32, u32 for integers
  • f32, f64 for floating-point numbers
  • char for single Unicode characters
  • bool for boolean values

3. Control Flow

Rust supports standard control flow constructs like if, else, and match.

fn main() {
    let number = 6;

    if number % 4 == 0 {
        println!("The number is divisible by 4");
    } else {
        println!("The number is not divisible by 4");
    }

    // Match statement
    match number {
        1 => println!("One"),
        2 | 3 => println!("Two or Three"),
        4..=6 => println!("Four to Six"),
        _ => println!("Something else"),
    }
}

4. Ownership and Borrowing

Rust’s ownership system enforces strict rules about how memory is managed. Here’s a basic example:

fn main() {
    let s1 = String::from("hello"); // s1 owns the string
    let s2 = s1; // s1 is moved to s2

    // println!("{}", s1); // This would cause a compile-time error
    println!("{}", s2); // This works
}

Borrowing allows you to have references to data without taking ownership:

fn main() {
    let s1 = String::from("hello");
    let len = calculate_length(&s1); // Borrowing s1

    println!("The length of '{}' is {}.", s1, len);
}

fn calculate_length(s: &String) -> usize {
    s.len()
}

5. Structs and Enums

Rust allows you to define custom data types using struct and enum.

  • Structs:

    struct Person {
        name: String,
        age: u8,
    }
    
    fn main() {
        let person = Person {
            name: String::from("Alice"),
            age: 30,
        };
    
        println!("Name: {}, Age: {}", person.name, person.age);
    }
  • Enums:

    enum Message {
        Quit,
        ChangeColor(i32, i32, i32),
        Write(String),
    }
    
    fn main() {
        let msg = Message::ChangeColor(255, 0, 0);
    
        match msg {
            Message::Quit => println!("Quit message"),
            Message::ChangeColor(r, g, b) => println!("Change color to ({}, {}, {})", r, g, b),
            Message::Write(text) => println!("Write message: {}", text),
        }
    }

6. Error Handling

Rust provides two main ways to handle errors: Result and Option.

  • Result: Used for functions that can return an error.

    fn divide(numerator: f64, denominator: f64) -> Result<f64, String> {
        if denominator == 0.0 {
            Err(String::from("Cannot divide by zero"))
        } else {
            Ok(numerator / denominator)
        }
    }
    
    fn main() {
        match divide(4.0, 2.0) {
            Ok(result) => println!("Result: {}", result),
            Err(e) => println!("Error: {}", e),
        }
    }
  • Option: Used for cases where a value might be absent.

    fn find_index(arr: &[i32], value: i32) -> Option<usize> {
        for (index, &item) in arr.iter().enumerate() {
            if item == value {
                return Some(index);
            }
        }
        None
    }
    
    fn main() {
        let arr = [10, 20, 30];
        match find_index(&arr, 20) {
            Some(index) => println!("Found at index: {}", index),
            None => println!("Not found"),
        }
    }

Resources for Learning Rust

Rust’s unique features and strong emphasis on safety and performance make it a powerful tool for systems programming and beyond. The community is active and supportive, making it a great choice for modern software development.

About

Contain APIs in rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors