I am struggling to apply the knowledge I received in the chapter The ? Operator Can Be Used in Functions That Return Result to my code with shared mpsc channel.
Here is minimal code:
use std::sync::{ mpsc, Arc, Mutex };
use std::error::Error;
fn main()
{
}
fn sh_rx_get( sh_rx : Arc< Mutex< mpsc::Receiver< usize > > > ) -> Result< usize, Box<dyn Error> >
{
let rx = sh_rx.lock()?;
let r = rx.try_recv()?;
Ok( r )
}
The error I got:
error[E0515]: cannot return value referencing function parameter `sh_rx`
--> src/main.rs:10:12
|
10 | let rx = sh_rx.lock()?;
| -----^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `sh_rx` is borrowed here
Online playground.
Any suggestion or hint?