0Pricing
Learn Rust Coding · درس

String مقابل &str

ملكية النصوص

String مقابل &str درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Owned vs Borrowed Text

يفصل Rust ملكية النصوص إلى نوعين:

  • String — تمتلك بياناتها على الكومة (heap).
  • &str — تستعير عرضاً لنص لا تملكه.
fn main() {
    let owned = String::from("I own this");
    let borrowed: &str = "I am borrowed";
    println!("{} / {}", owned, borrowed);
}

Where the Data Lives

يخزّن String بايتاته في الذاكرة العشوائية (heap) ويمكنه أن ينمو. أما الحرفي النصي &str فهو مضمّن في ملف البرنامج الثنائي (binary) ويكون ثابت الحجم.

fn main() {
    let mut heap = String::from("grows");
    heap.push_str(" bigger");
    let fixed = "never changes";
    println!("{} | {}", heap, fixed);
}

Ownership Means Responsibility

The owner of a String frees its memory when it goes out of scope. A &str owns nothing, so it never frees anything.

fn main() {
    {
        let s = String::from("temporary");
        println!("{}", s);
    } // s is dropped and its memory freed here
    println!("done");
}

Borrowing a String

You can borrow a String as a &str using &. The String still owns the data; the slice just looks at it.

fn main() {
    let owned = String::from("shared text");
    let view: &str = &owned;
    println!("{} / {}", owned, view);
}

Moving a String

Assigning a String to another variable moves ownership. The original can no longer be used.

fn main() {
    let a = String::from("hello");
    let b = a; // ownership moves to b
    // println!("{}", a); // would error: a was moved
    println!("{}", b);
}

Cloning to Keep Both

If you need two independent owned strings, use clone to copy the data.

fn main() {
    let a = String::from("hello");
    let b = a.clone();
    println!("{} and {}", a, b);
}

Copying a &str Is Cheap

A &str is just a pointer and length, so copying it does not copy the underlying text. It implements Copy.

fn main() {
    let original = "data";
    let copy = original; // both still valid
    println!("{} {}", original, copy);
}

Functions and Ownership

Passing a String by value moves it into the function. Passing &str or &String only borrows, leaving the caller's value usable.

fn length(s: &str) -> usize {
    s.len()
}

fn main() {
    let text = String::from("measure me");
    println!("{}", length(&text));
    println!("still here: {}", text);
}

Returning Owned Strings

When a function must produce new text, it returns an owned String so the caller takes ownership.

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

fn main() {
    let g = make_greeting("Rust");
    println!("{}", g);
}

Choosing the Right Type

Guidelines:

  • Store owned text? Use String.
  • Just read or pass text? Accept &str.
  • Build new text in a function? Return String.
fn first_word(s: &str) -> &str {
    s.split_whitespace().next().unwrap_or("")
}

fn main() {
    let sentence = String::from("learn rust today");
    println!("{}", first_word(&sentence));
}

Why the Distinction Matters

This split lets Rust avoid unnecessary copies and guarantees memory safety without a garbage collector. You always know who owns a piece of text.

fn main() {
    let owned = String::from("safe");
    let borrowed = &owned[..];
    println!("owner {} borrows {}", owned, borrowed);
}

Quick Check

Recall what happens when you move a String.

Recap

String vs &str ownership:

  • String owns heap data and frees it on drop.
  • &str borrows existing text and owns nothing.
  • Moving a String invalidates the original; clone copies it.
  • Accept &str to read, return String to produce new text.
fn shout(s: &str) -> String {
    s.to_uppercase()
}

fn main() {
    let name = String::from("rust");
    let loud = shout(&name);
    println!("{} -> {}", name, loud);
}

الأسئلة الشائعة

هل درس «String مقابل &str» مجاني؟

نعم — نص درس «String مقابل &str» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

ماذا ستتعلم في «String مقابل &str»؟

ملكية النصوص تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟

لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «String مقابل &str»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟

نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. String مقابل &str
  2. المقاطع
  3. أساليب String
  4. UTF-8 والمحارف
← العودة إلى Learn Rust Coding