How to split a global vector so left is constant and right is mutable?

⚓ Rust    📅 2025-12-23    👤 surdeus    👁️ 1      

surdeus

I have an application where a global vector is growing as the program proceeds and once an element is created it does not change.

Adding elements to the vector is fast, but using an element of the vector may take a long time.

If I use an RwLock on the vector, the reads will block writes.

Is there a way to do the following:

  1. Initialize the vector with a maximum size, set the elements to a default value that is not used, and set next_index to zero.

  2. Have the writes store useful information at next_index, return next_index for use by future reads, and increment next_index.

  3. Have the reads access elements below the current next_index, at the same time that a write stores information (as described in 2).

4 posts - 3 participants

Read full topic

🏷️ Rust_feed