General Rust Flashcards

1
Q

Why use Self

A

When u change the Struct name u dont have to change the return Type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain:

  1. What is Arc
  2. How does it work
  3. Usecase of Arc
A

1 and 2. The type Arc<T> provides shared ownership of a value of type T, allocated in the heap. Invoking clone on Arc produces a new Arc instance, which points to the same allocation on the heap as the source Arc, while increasing a reference count. When the last Arc pointer to a given allocation is destroyed, the value stored in that allocation (often referred to as “inner value”) is also dropped.</T>

  1. When to Use Arc:

Multiple Ownership Across Threads:

When data needs to be shared between threads
When multiple threads need ownership

Shared Resources:

Database connections
Configuration data
Cache implementations
Connection pools

Thread Safety Requirements:

When you need thread-safe reference counting
When data must be shared across thread boundaries

Long-lived Data:

Resources that outlive their original scope
Data that needs to persist across async operations

Worker Patterns:

Thread pools
Background task processors
Event handlers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain:

  1. What is Rc
  2. How does it work
  3. Usecase of Rc
A
  1. What is Rc?

Single-threaded reference counting smart pointer
Enables multiple ownership of the same data
Keeps track of the number of references to a value
Only for use in single-threaded scenarios

  1. Memory Management:

Data is freed when reference count reaches zero
All clones share the same data
Thread-unsafe but more efficient than Arc

3.
1. Multiple ownership situation

2.Data Structures:
Graphs
Trees
Linked Lists
Any structure where nodes might have multiple owners

  1. Caching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly