The use bounds, as the document says, are used to opt-out some un-needed things. The default hehaviour is capturing all.
So, I think it is OK if I don't add use<'_, B>
, leaving the default behaviour of capturing all parameters takes effect.
But the code can't be acceppted without it.
fn some_fn<const B: bool>(&self) -> impl DerefMut<Target = Vec<Pair>> + use<'_, B> {
self.mutex.lock().unwrap()
}
Edit:
I found the problem, there are two targets in my cargo.toml; I set rust 2024 only at only one of them
jofas
March 13, 2025, 10:52am
2
Would you mind providing a minimal reproducible example? I can't replicate the need for use<'_, B>
.
What edition do you have in your Cargo.toml
? The behaviour of capturing everything by default is something that was introduced in the 2024 edition. For earlier editions, the default is to capture nothing (EDIT to correct: only lifetime parameters are not captured by default, type and const generics are captured in all editions).
The code can be found at PlayGround ;
It seems because it is rust 2021, with 2024, the problem gone;
While my real code is indeed rust 2024, (I started with rust 2021, but changed to 2024 by modifying Cargo.toml manually), and the problem araises;
So, what can I do to fix that? I remember I have done cargo clean after the changing
jofas
March 13, 2025, 11:08am
5
I believe I found the restriction :
use<..>
, if provided, must include all in-scope type and const generic parameters.
opened 12:59PM - 06 Sep 24 UTC
closed 09:24AM - 07 Sep 24 UTC
C-discussion
T-types
F-precise_capturing
The current implementation of precise capture requires that `use<>` bounds inclu… de all type parameters in scope. This is a temporary limitation that should be lifted. Example:
```rust
#![allow(warnings)]
fn main() {
let mut data = vec![1, 2, 3];
let mut i = indices(&data);
data.push(4);
i.next();
}
fn indices<T>(
slice: &[T],
) -> impl Iterator<Item = usize> + use<> {
0 .. slice.len()
}
```
This code should compile, but it current gives an error ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b5a00260b648f8c7ce0949a3bab6af99)):
```
error: `impl Trait` must mention all type parameters in scope in `use<...>`
--> src/main.rs:12:6
|
10 | fn indices<T>(
| - type parameter is implicitly captured by this `impl Trait`
11 | slice: &[T],
12 | ) -> impl Iterator<Item = usize> + use<> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: currently, all type parameters are required to be mentioned in the precise captures list
```
It's the default for Edition 2024, not previous editions. Doc
It is not because of const B: bool; the problem appears when the no use<'_, B>
at all (not only '_)
kpreid
March 13, 2025, 1:26pm
9
You can set the edition for the whole [package]
at once, not just individual targets. That will likely be less error-prone.
I set 2024 at the package, unfortunatelly, there is a 2021 left at the bin