Using mpsc::Receiver and mpsc::Sender in struct fields

I am trying to create following struct ThreadsPool:

use std::sync::mpsc::{Receiver, Sender};
use std::sync::mpsc;

struct ThreadsPool {
    // ...
    finish_recv: Receiver<String>,
    finish_send: Sender<String>,
}

fn some_func() -> ThreadsPool {
    let (finish_recv, finish_send): (Sender<String>, Receiver<String>) = mpsc::channel();

    ThreadsPool {
        // ...
        finish_recv,
        finish_send,
    }
}

In function some_func I am trying to create a new ThreadsPool struct with finish_recv and finish_send, but for some reason when I run cargo check I get two following errors:

error[E0308]: mismatched types
  --> src/ThreadsPool/mod.rs:25:13
   |
25 |             finish_recv,
   |             ^^^^^^^^^^^ expected struct `std::sync::mpsc::Receiver`, found struct `Sender`
   |
   = note: expected struct `std::sync::mpsc::Receiver<i32>`
              found struct `Sender<i32>`

error[E0308]: mismatched types
  --> src/ThreadsPool/mod.rs:26:13
   |
26 |             finish_send,
   |             ^^^^^^^^^^^ expected struct `Sender`, found struct `std::sync::mpsc::Receiver`

You swapped the names.

2 Likes

Thanks a lot!