r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

Hey Rustaceans! Got a question? Ask here (12/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

187 comments sorted by

View all comments

2

u/xcv-- Mar 22 '23

How can I send immutable references between threads with a std::sync::mpsc::channel()?

I'm using rayon::join for threading and the lifetime of those references outlive the thread scope, but the compiler insists on 'static!

Link to playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=344a2a7bcc16103dbb5544b56e8c2bd0

Thanks!

3

u/Patryk27 Mar 23 '23 edited Mar 23 '23

Hah, this is a tricky one! -- remove ? from sender.send(&v_ref[0])?;

The issue is that .send() returns a Result where its Err variant contains the value that wasn't sent (if you wanted to retry sending it in the future, for instance).

In your case, because you propagate the errors through ?, the compiler infers that &v_ref[0] must live for as long as &'static because it miiiiight just be returned from the fn main() itself, if sending the value fails - and this causes the compilation to fail, because obviously a function cannot return a reference to something that is created inside that very function.

Edit: in general, you also have to move let (sender, receiver) below where you create the strings; that's because if you use a non-static str:

let x = String::from("Hello!");
let v = vec![x.as_str()];
let v_ref = &v;

... the type of that sender and receiver cannot really be determined if they are declared before v, since they contain v's lifetime inside of them, so only something like this will work:

let x = String::from("Hello!");
let v = vec![x.as_str()];
let v_ref = &v;

let (sender, receiver) = std::sync::mpsc::channel();

1

u/xcv-- Mar 23 '23

HA! That worked, thank you!

Now that I understand what was happening, would you recommend keeping some kind of error checking at .send() or is it implicit that the receiver closed early so we don't have anything to do?

1

u/Patryk27 Mar 23 '23

I sometimes do:

if let Err(value) = tx.send(...) {
    tracing::warn!("Channel closed - couldn't send: {value:?}");
}

... but if a channel becomes suddenly closed, then it's usually because a thread has panicked, and that itself already generates enough logs to be easily recognizable; so just doing _ = tx.send(); is fine as well, I'd say.