🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

शुरुआती लोगों के लिए रस्ट प्रोग्रामिंग गाइड 2026: स्वामित्व, संरचनाएं और कार्गो

⏱️5 min read  ·  984 words

रस्ट 2026 में सबसे तेजी से बढ़ने वाली सिस्टम प्रोग्रामिंग भाषा है। लिनक्स कर्नेल, एंड्रॉइड, विंडोज, एडब्ल्यूएस और मेटा में प्रयुक्त, रस्ट संकलन समय पर मेमोरी सुरक्षा की गारंटी के साथ सी-स्तरीय प्रदर्शन प्रदान करता है। यह शुरुआती मार्गदर्शिका शब्दजाल की अधिकता के बिना रस्ट की मूल अवधारणाओं को शामिल करती है।

2026 में जंग क्यों?

  • स्मृति सुरक्षा– कोई शून्य संकेतक नहीं, कोई बफर ओवरफ्लो नहीं, कोई उपयोग-बाद-मुक्त नहीं
  • प्रदर्शन– शून्य रनटाइम ओवरहेड के साथ C/C++ से मेल खाता है
  • निर्भय समवर्ती– संकलन समय पर डेटा दौड़ पकड़ी गई
  • आधुनिक टूलींग– कार्गो पैकेज मैनेजर, रस्टफएमटी, क्लिपी, रस्ट-एनालाइजर
  • वेबअसेंबली– ब्राउज़र में रस्ट को लगभग मूल गति से चलाएँ
  • 9 साल तक दौड़ना अच्छा लगा– स्टैक ओवरफ़्लो सर्वेक्षण में सर्वाधिक पसंद की जाने वाली भाषा

इंस्टालेशन

# Install rustup (manages Rust versions)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env

# Verify
rustc --version    # rustc 1.79.0
cargo --version    # cargo 1.79.0

# Create new project
cargo new hello_rust
cd hello_rust

# Run
cargo run

# Build release (optimized)
cargo build --release

चर, प्रकार और कार्य

// Variables are immutable by default
fn main() {
    let x = 5;          // immutable
    let mut y = 10;     // mutable
    y += 1;             // OK
    // x += 1;          // ERROR: cannot assign to immutable variable

    // Type annotation (usually inferred)
    let name: String = String::from("Alice");
    let age: u32 = 30;
    let pi: f64 = 3.14159;
    let active: bool = true;

    // Constants
    const MAX_SIZE: usize = 1024;

    // Shadowing — create new variable with same name
    let spaces = "   ";
    let spaces = spaces.len(); // now a number, not a string

    println!("Name: {name}, Age: {age}");
}

// Functions
fn add(a: i32, b: i32) -> i32 {
    a + b  // no semicolon = expression, this is the return value
}

fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

// Multiple return values via tuple
fn min_max(numbers: &[i32]) -> (i32, i32) {
    let min = *numbers.iter().min().unwrap();
    let max = *numbers.iter().max().unwrap();
    (min, max)
}

let (lo, hi) = min_max(&[3, 1, 4, 1, 5, 9]);
println!("min={lo}, max={hi}");

स्वामित्व – रस्ट की मूल अवधारणा

रस्ट की स्वामित्व प्रणाली वह है जो कचरा संग्रहण के बिना स्मृति सुरक्षा को संभव बनाती है:

// Rule 1: Each value has exactly one owner
// Rule 2: When owner goes out of scope, value is dropped (freed)
// Rule 3: Ownership can be transferred (moved)

fn main() {
    let s1 = String::from("hello");
    let s2 = s1;  // s1 is MOVED to s2
    // println!("{s1}"); // ERROR: s1 is no longer valid

    // Clone to make a copy
    let s3 = String::from("hello");
    let s4 = s3.clone();  // deep copy, both valid
    println!("{s3} {s4}");

    // Stack types (Copy trait): no move needed
    let n1 = 5;
    let n2 = n1;  // copied, n1 still valid
    println!("{n1} {n2}");
}

// Passing to functions transfers ownership
fn takes_ownership(s: String) {
    println!("{s}");
} // s is dropped here

fn gives_ownership() -> String {
    String::from("new string")  // moved to caller
}

fn main() {
    let s = String::from("hello");
    takes_ownership(s);
    // s no longer valid here!

    let s2 = gives_ownership();  // s2 owns the new string
}

सन्दर्भ और उधार

// Borrow without taking ownership using &
fn calculate_length(s: &String) -> usize {
    s.len()
}  // s goes out of scope, but doesn't drop the String (it's borrowed)

fn main() {
    let s1 = String::from("hello");
    let len = calculate_length(&s1);  // pass reference
    println!("{s1} has {len} characters");  // s1 still valid!

    // Mutable references
    let mut s = String::from("hello");
    change(&mut s);  // pass mutable reference
    println!("{s}");  // "hello, world"
}

fn change(s: &mut String) {
    s.push_str(", world");
}

// Rules:
// 1. Any number of immutable references
// 2. OR exactly one mutable reference
// 3. Never both at the same time
// This prevents data races at compile time!

संरचनाएं और तरीके

#[derive(Debug, Clone)]  // auto-implement Debug and Clone
struct Rectangle {
    width: f64,
    height: f64,
}

impl Rectangle {
    // Associated function (like static method)
    fn new(width: f64, height: f64) -> Self {
        Rectangle { width, height }
    }

    fn square(size: f64) -> Self {
        Rectangle { width: size, height: size }
    }

    // Method — takes &self (immutable borrow)
    fn area(&self) -> f64 {
        self.width * self.height
    }

    fn perimeter(&self) -> f64 {
        2.0 * (self.width + self.height)
    }

    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }

    // Mutable method
    fn scale(&mut self, factor: f64) {
        self.width *= factor;
        self.height *= factor;
    }
}

fn main() {
    let mut rect = Rectangle::new(30.0, 50.0);
    println!("{rect:?}");  // Debug output: Rectangle { width: 30.0, height: 50.0 }
    println!("Area: {}", rect.area());
    rect.scale(2.0);
    println!("After scaling: {}x{}", rect.width, rect.height);
}

गणनाएँ और पैटर्न मिलान

// Enum with data
#[derive(Debug)]
enum Shape {
    Circle(f64),                      // radius
    Rectangle(f64, f64),              // width, height
    Triangle { base: f64, height: f64 },  // named fields
}

impl Shape {
    fn area(&self) -> f64 {
        match self {
            Shape::Circle(r) => std::f64::consts::PI * r * r,
            Shape::Rectangle(w, h) => w * h,
            Shape::Triangle { base, height } => 0.5 * base * height,
        }
    }
}

// Option<T> — replace null
fn find_user(id: u32) -> Option<String> {
    if id == 1 { Some(String::from("Alice")) } else { None }
}

fn main() {
    match find_user(1) {
        Some(name) => println!("Found: {name}"),
        None => println!("Not found"),
    }

    // if let — match one arm
    if let Some(name) = find_user(1) {
        println!("User: {name}");
    }

    // unwrap_or, map, and_then
    let name = find_user(2).unwrap_or(String::from("Guest"));
    let length = find_user(1).map(|n| n.len()).unwrap_or(0);
}

// Result<T, E> — error handling
fn parse_number(s: &str) -> Result<i32, std::num::ParseIntError> {
    s.trim().parse()
}

fn main() {
    match parse_number("42") {
        Ok(n) => println!("Parsed: {n}"),
        Err(e) => println!("Error: {e}"),
    }

    // ? operator — propagate errors
    fn add_strings(a: &str, b: &str) -> Result<i32, std::num::ParseIntError> {
        let x: i32 = a.trim().parse()?;  // return Err if fails
        let y: i32 = b.trim().parse()?;
        Ok(x + y)
    }
}

संग्रह

use std::collections::HashMap;

fn main() {
    // Vec — dynamic array
    let mut v: Vec<i32> = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);

    // Iterate
    for n in &v { print!("{n} "); }

    // Useful methods
    v.retain(|x| *x > 1);  // remove elements
    let sum: i32 = v.iter().sum();
    let doubled: Vec<i32> = v.iter().map(|x| x * 2).collect();

    // HashMap
    let mut scores: HashMap<String, u32> = HashMap::new();
    scores.insert(String::from("Alice"), 95);
    scores.insert(String::from("Bob"), 87);

    // Only insert if not present
    scores.entry(String::from("Alice")).or_insert(100);

    // Lookup
    if let Some(score) = scores.get("Alice") {
        println!("Alice: {score}");
    }

    // Iterate
    for (name, score) in &scores {
        println!("{name}: {score}");
    }
}

कार्गो: पैकेज मैनेजर

# Cargo.toml
[package]
name = "my_app"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.12", features = ["json"] }
anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }

cargo add serde --features derive    # add dependency
cargo update                         # update dependencies
cargo test                           # run tests
cargo bench                          # run benchmarks
cargo doc --open                     # generate and open docs
cargo clippy                         # linting
cargo fmt                            # auto-format

आरंभ करने का रोडमैप

  1. के पहले 8 अध्याय पढ़ेंजंग किताब(doc.rust-lang.org पर निःशुल्क)
  2. पूरासरसराहट– इंटरैक्टिव अभ्यास
  3. के साथ एक सीएलआई टूल बनाएंताली
  4. के साथ एक REST API लिखेंaxum
  5. ओपन-सोर्स रस्ट प्रोजेक्ट में योगदान करें

रस्ट का सीखने का दौर वास्तविक है लेकिन इसका लाभ स्थायी है। एक बार जब आप स्वामित्व और उधार को आत्मसात कर लेंगे, तो आप हर भाषा में सुरक्षित, तेज़ कोड लिखेंगे। 2026 में रस्ट इकोसिस्टम वेब सर्वर, सीएलआई, एंबेडेड और वेबअसेंबली के लिए पर्याप्त परिपक्व है।

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা