Skip to content

Clarify statement regarding windows_mut #133628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Voultapher opened this issue Nov 29, 2024 · 3 comments · Fixed by #135773
Closed

Clarify statement regarding windows_mut #133628

Voultapher opened this issue Nov 29, 2024 · 3 comments · Fixed by #135773
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@Voultapher
Copy link
Contributor

Location

https://doc.rust-lang.org/core/primitive.slice.html#method.windows

Summary

The documentation for slice::windows states:

There’s no windows_mut, as that existing would let safe code violate the “only one &mut at a time to the same thing” rule.

Either I don't understand how it would violate the unique reference rules of Rust or that statement is false. Using GATs it's possible to implement a version of windows_mut today as shown here, however that version is limited to slice windows of size known at compile time. But that limitation doesn't change the fact that it is - at leas to my understanding - perfectly fine to have multiple unique references to slice elements as long as they are not pointing to the same element, which can't happen by definition in slice::window.

@Voultapher Voultapher added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Nov 29, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 29, 2024
@jieyouxu jieyouxu added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 30, 2024
@QuineDot
Copy link

QuineDot commented Dec 9, 2024

[it is] perfectly fine to have multiple unique references to slice elements as long as they are not pointing to the same element, which can't happen by definition in slice::window.

You seem to be confusing windows* with chunk* here.

For example

From the windows documentation:

The windows overlap.
[...]

Examples

let slice = ['l', 'o', 'r', 'e', 'm'];
let mut iter = slice.windows(3);
assert_eq!(iter.next().unwrap(), &['l', 'o', 'r']);
assert_eq!(iter.next().unwrap(), &['o', 'r', 'e']);

(Note how 'o' and 'r' are in both slices.)

((Chunks don't overlap and chunks_mut exists.))


Either I don't understand how it would violate the unique reference rules of Rust or that statement is false. Using GATs it's possible to implement a version of windows_mut today as shown here,

The Iterator trait has a non-associated type and thus cannot support lending iterators (e.g. you can always collect all the items). That's the context of the current documentation. It could be more explicit about that:

There’s no windows_mut as an analog of Windows that implements Iterator<Item = &mut [_]> would let safe code violate the “only one &mut at a time to the same thing” rule by returning overlapping &mut [_] slices.

(There is no lending iterator trait in std.)

@Voultapher
Copy link
Contributor Author

The Iterator trait has a non-associated type and thus cannot support lending iterators (e.g. you can always collect all the items). That's the context of the current documentation. It could be more explicit about that:

I think that was the missing piece for me, each individual iteration is valid but only if it has a unique borrow per iteration, the original documentation piece only talks about windows_mut, the implication that the returned value would have to implement the Iterator trait is not stated.

Can we find a wording of that phrase that makes the idea behind it more clear?

@hkBst
Copy link
Member

hkBst commented Jan 20, 2025

The Iterator trait has a non-associated type

This is not correct. The Iterator trait does have an associated type, the difference with LendingIterator is that its associated type is not generic over any lifetime.

@bors bors closed this as completed in b7476f1 Jan 27, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 27, 2025
Rollup merge of rust-lang#135773 - hkBst:patch-10, r=tgross35

Clarify WindowsMut (Lending)Iterator

fixes rust-lang#133628
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this issue Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants