General Rust Flashcards
Why use Self
When u change the Struct name u dont have to change the return Type
Explain:
- What is Arc
- How does it work
- Usecase of Arc
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>
- 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
Explain:
- What is Rc
- How does it work
- Usecase of Rc
- 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
- 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
- Caching