Skip to content
เข้าสู่ระบบ

Rc Smart Pointer

Rc (Reference Counted) pointer สำหรับ shared ownership ใน single-threaded scenarios

Rc<T> ให้หลาย owners เป็นเจ้าของ data เดียวกันได้:

  • นับจำนวน references อัตโนมัติ
  • Data ถูก drop เมื่อ count = 0
  • ไม่ thread-safe (ใช้ Arc สำหรับ multi-thread)
use std::rc::Rc;
fn main() {
// ============================================
// Rc basics
// ============================================
let shared = Rc::new(vec![1, 2, 3]);
println!("Count: {}", Rc::strong_count(&shared)); // 1
// Clone เพิ่ม count (ไม่ clone data)
let clone1 = Rc::clone(&shared);
println!("After clone: {}", Rc::strong_count(&shared)); // 2
// ทุก Rc ชี้ไป data เดียวกัน
println!("shared: {:?}", shared);
println!("clone1: {:?}", clone1);
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด