rustvectormove-semanticsborrow-checkerrust-tokio

How do I move a Sender object out of a mutable reference to a Vector of tuples of Strings and Sender Objects?


This is not a solution to my problem. The suggested answer replaces the values with Nones. I quite literally require the size of my vector to reduce.

MRE:

use tokio::sync::mpsc;

#[tokio::main]
async fn main() {

    // Pits the singers against each other...
    // Please do not get confused by the type of the sender used in here.
    // This still will showcase the same problem.
    fn pit(singers: &mut Vec<(String, mpsc::Sender<String>)>) {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        let i1 = rng.gen_range(0..singers.len());
        let i2 = rng.gen_range(0..singers.len());
        // This will add dunsel "singers" to the vector who will get
        // pitted against one another. The actual code makes sure that
        // there are at least 2 singers in queue. What is does not do - 
        // and neither should it - is check if they are dummy objects.
        // Rust did not like me using singers[i1].take().unwrap() and
        // complained about iterators not being implemented for Sender?
        let (dunsel, _) = mpsc::channel::<String>(1);
        let s1 = std::mem::replace(&mut singers[i1], (String::new(), dunsel.clone()));
        let s2 = std::mem::replace(&mut singers[i2], (String::new(), dunsel.clone()));
        // returns s1, s2... not needed for MRE
    }

    let mut v = vec![];
    let (tx, _) = mpsc::channel::<String>(256); // receiver is not needed for MRE
    for name in vec!["name1".to_string(), "name2".to_string(), "name3".to_string()] {
        // In the actual code, the singer clients are sent this tx
        // and they send their transmitter channels via this tx, which
        // is then pushed to the vector. Here, it just accepts `String`s
        // So, tx is of the type mpsc::Sender<mpsc::Sender<String>>!!
        v.push((name.clone(), tx.clone()));
    }
    tokio::spawn(async move {
        let mut v = v;
        pit(&mut v); 
    });
}

Solution

  • Just remove them:

    let i1 = rng.gen_range(0..singers.len());
    let s1 = singers.remove(i1);
    let i2 = rng.gen_range(0..singers.len());
    let s2 = singers.remove(i2);
    
    // use s1 and s2 as you please, you are the owner and
    // they are no longer in the singers vec
    

    If you do not care about preserving the order in singers then swap_remove can be more efficient.