Skip to content

strange cannot move out of a mutable reference error #139670

Open
@Sherlock-Holo

Description

@Sherlock-Holo

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ac7fef256eba962ef5516954ca18da64

I expected to see this happen:

codes can be complied

Instead, this happened:

it compile failed

   Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of `buf`, a captured variable in an `FnMut` closure
  --> src/lib.rs:14:54
   |
11 |     async fn recv(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
   |                          --- captured outer variable
12 |         self.0
13 |             .async_io(Interest::READABLE, |fd| {
   |                                           ---- captured by this `FnMut` closure
14 |                 let (data, _) = rustix::io::read(fd, buf)
   |                                                      ^^^ move occurs because `buf` has type `&mut [MaybeUninit<u8>]`, which does not implement the `Copy` trait

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` (lib) due to 1 previous error

however, if I try https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=e8ee087d0def6e5885e8687d577ea190

it can compile but clippy says

warning: this call to `as_mut` does nothing
  --> src/lib.rs:14:54
   |
14 |                 let (data, _) = rustix::io::read(fd, buf.as_mut())
   |                                                      ^^^^^^^^^^^^ help: try: `buf`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_asref
   = note: `#[warn(clippy::useless_asref)]` on by default

I think clippy is right, re-borrow a &mut doesn't do anything, so I think maybe rustc has problem? If not, please tell me how to avoid the compile error and clippy warn

Meta

rustc --version --verbose:

rustc 1.88.0-nightly (0fe8f3454 2025-04-10)
binary: rustc
commit-hash: 0fe8f3454dbe9dda52a254991347e96bec579a6f
commit-date: 2025-04-10
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-clippyArea: ClippyA-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.L-false-positiveLint: False positive (should not have fired).T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions