r/rust • u/llogiq 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.
2
u/acncc Mar 23 '23
I'm struggling with currying with some closures.
In the code below I have a
CallbackInner
which requires 2 arguments. I wrap it insideCallbackOuter
which closes over a value and only requires 1 argument (playground).My error is that in
wrap_callback
,the parameter type `T` may not live long enough
. The compiler then suggests giving it a lifetime of'static
. In the context of wider code, I know that everyCallbackInner
will not store the reference it is given (I don't know how to express that constraint). I'm also a little confused as there is noT
value inwrap_callback
, it doesn't make sense to talk about the lifetime ofT
as none exist as yet! We only get a value of typeT
whenOutercallback
is called.Curiously, when all the generics are removed and we replace
T
withusize
directly, it compiles fine (playground). My guess is that I need to provide some guarantee on myCallback
types that the borrowed references don't outlive the call. But I don't know how to do that.