WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Rust Β· Intermediate Β· question 23 of 100

Explain the differences between Rc and Arc, and when to use each.?

πŸ“• Buy this interview preparation book: 100 Rust questions & answers β€” PDF + EPUB for $5

Rc and Arc are both reference-counted pointer types in Rust, but they differ in their ownership and thread-safety features.

Rc (short for Reference Counted) is a type that enables multiple ownership of the same data. It is used to achieve sharing of data between multiple parts of a program, without having to copy the data. It allows creating multiple mutable or immutable references to the same data, and each reference increments a reference count to the data, and when there are no more references, the data is deallocated. Rc is not thread-safe and can only be used in single-threaded environments or where all accesses to the data are synchronized externally (such as using a mutex). Here’s an example:

    use std::rc::Rc;
    
    struct Person {
        name: String,
        age: u8,
    }
    
    let person = Person { name: "John".to_string(), age: 30 };
    let rc_person = Rc::new(person);
    
    let rc_person2 = rc_person.clone(); // increment the reference count
    
    println!("Name: {}", rc_person.name); // prints "Name: John"

Arc (short for Atomic Reference Counted) is a thread-safe version of Rc that can be safely used in multi-threaded environments. Arc uses atomic operations to perform the reference counting, which can be safely accessed concurrently by multiple threads. Like Rc, Arc allows multiple ownership of the same data, but it is thread-safe. Here’s an example:

    use std::sync::Arc;
    use std::thread;
    
    struct Person {
        name: String,
        age: u8,
    }
    
    let person = Person { name: "John".to_string(), age: 30 };
    let arc_person = Arc::new(person);
    
    let arc_person2 = arc_person.clone();
    
    // Move the Arc to another thread
    let handle = thread::spawn(move || {
        println!("Name: {}", arc_person.name); // prints "Name: John"
    });
    
    handle.join().unwrap();

In general, use Rc when you have a single-threaded environment or when you do not need thread safety. Use Arc when you need to share data across multiple threads or when you want to use the data in a concurrent environment. Note that Rc cannot be safely moved between threads, while Arc can be safely moved between threads.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Rust interview β€” then scores it.
πŸ“ž Practice Rust β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Rust questions & answers β€” PDF + EPUB for $5

All 100 Rust questions Β· All topics