المؤشرات الذكية Box وRc وArc
افهموا كيف تدير `Box` التخصيص على الكومة، وتدير `Rc` الملكية المشتركة، وتدير `Arc` الملكية المشتركة الآمنة على مستوى الخيوط للبيانات.
المؤشرات الذكية Box وRc وArc درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 3 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What are Smart Pointers?
Rust's ownership system is great for memory safety, but sometimes you need more flexibility. That's where smart pointers come in!
Smart pointers are data structures that act like pointers but also have additional metadata and capabilities. They manage memory, ownership, and other resources automatically.
Box: Storing Data on the Heap
The simplest smart pointer is Box<T>. It allows you to store data on the heap instead of the stack.
- Stack: Fast, fixed-size data.
- Heap: Slower, flexible-size data, allocated at runtime.
When you put a value in a Box, the Box itself is on the stack, but the data it points to lives on the heap.
Using Box for Heap Allocation
Let's see how Box moves data to the heap. This is useful for large data or when you don't know the size at compile time.
Run this code to observe a value being boxed.
fn main() {
let x = 5; // x is on the stack
let boxed_x = Box::new(x); // x's value is moved to the heap, boxed_x is on stack
println!("Value on stack: {}", x);
println!("Value in Box (on heap): {}", *boxed_x); // Dereference to get value
}When Box is Useful
You might use Box<T> in these situations:
- When you have a type whose size can't be known at compile time, and you need to store it somewhere with a known, fixed size.
- When you have a large amount of data and want to transfer ownership without copying the data itself.
- When you want to own a trait object (e.g.,
Box<dyn Trait>).
Rc: Multiple Owners (Single Thread)
Rust's ownership rules mean a value usually has only one owner. But what if multiple parts of your program need to "own" the same data?
Rc<T>, or Reference Counted, allows multiple owners of data in a single-threaded scenario. It keeps track of how many references point to the data.
When the count drops to zero, the data is cleaned up.
Sharing Data with Rc
Rc::clone() increments the reference count. This isn't a deep copy; it just creates another pointer to the same data.
Notice how the data is shared, and its value is accessible from different "owners".
use std::rc::Rc;
fn main() {
let value = Rc::new(String::from("Shared String"));
println!("Count after creation: {}", Rc::strong_count(&value));
let value_clone_a = Rc::clone(&value); // Increment count
println!("Count after clone A: {}", Rc::strong_count(&value));
{
let value_clone_b = Rc::clone(&value); // Increment count
println!("Count after clone B: {}", Rc::strong_count(&value));
println!("Data from A: {}", value_clone_a);
println!("Data from B: {}", value_clone_b);
} // value_clone_b goes out of scope, count decreases
println!("Count after B goes out of scope: {}", Rc::strong_count(&value));
}Arc: Shared Ownership (Multi-Thread)
Rc<T> works well for single-threaded applications. However, if you need to share data between multiple threads, Rc<T> is not safe.
Arc<T>, or Atomic Reference Counted, is the thread-safe version of Rc<T>. It uses atomic operations to update the reference count, ensuring safety across threads.
Arc has a slight performance overhead compared to Rc due to atomic operations.
Sharing Data Across Threads with Arc
This example shows how Arc allows multiple threads to safely access and read the same shared data. Each thread gets its own `Arc` clone.
The main thread waits for all spawned threads to complete.
use std::sync::Arc;
use std::thread;
fn main() {
let data = Arc::new(vec![1, 2, 3, 4, 5]);
let mut handles = vec![];
for i in 0..3 {
let data_clone = Arc::clone(&data); // Clone Arc for each thread
let handle = thread::spawn(move || {
println!("Thread {} has data: {:?}", i, *data_clone);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("All threads finished.");
}Box, Rc, or Arc?
Choosing the right smart pointer depends on your needs:
Box<T>: When you need to put data on the heap, typically for single ownership or when dealing with recursive types.Rc<T>: When you need multiple owners for data in a single-threaded context.Arc<T>: When you need multiple owners for data in a multi-threaded (concurrent) context.
Always prefer Box or Rc if you don't need thread safety, as Arc has a performance cost.
Smart Pointer Check
You need to store a large image file on the heap, and only one part of your program will own and manage it. Which smart pointer should you use?
Recap: Smart Pointers
In this lesson, you learned about three fundamental Rust smart pointers:
Box<T>: For allocating data on the heap with single ownership.Rc<T>: For enabling multiple owners of data in a single-threaded environment.Arc<T>: For enabling multiple, thread-safe owners of data in a multi-threaded environment.
These smart pointers give you more control over memory management while still leveraging Rust's safety guarantees.
الأسئلة الشائعة
هل درس «المؤشرات الذكية Box وRc وArc» مجاني؟
نعم — نص درس «المؤشرات الذكية Box وRc وArc» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 3 دروس في المجموع.
ماذا ستتعلم في «المؤشرات الذكية Box وRc وArc»؟
افهموا كيف تدير `Box` التخصيص على الكومة، وتدير `Rc` الملكية المشتركة، وتدير `Arc` الملكية المشتركة الآمنة على مستوى الخيوط للبيانات. تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟
لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 3.
كم من الوقت يستغرق درس «المؤشرات الذكية Box وRc وArc»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟
نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- المؤشرات الذكية Box وRc وArc
- القابلية الداخلية للتغيير: RefCell وCell
- التزامن الآمن مع الخيوط