Confusing report for `let guard` borrows of owned values used in match expression

Abierto
#166,180 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust
Área
compilers

Línea de trabajo

Start by reproducing the supplied example with rustc and inspect the borrow-checker diagnostic code that reports E0505, especially how it labels borrows and moves in if let match guards. Compare the current spans with the expected output and check nearby diagnostic tests for similar cases. Done means the diagnostic points to both relevant locations and includes the suggested borrowing help without applying it automatically.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

A-borrow-checker A-diagnostics A-patterns T-compiler
Code
pub fn foo(v: Option<Vec<u8>>) {
    match v {
        Some(v) if let Some(r) = v.strip_prefix(b"foo ") => {
            println!("{:?}", r);
        }
        _ => ()
    }
}
Current output
error[E0505]: cannot move out of `v.0` because it is borrowed
 --> src/lib.rs:3:14
  |
3 |         Some(v) if let Some(r) = v.strip_prefix(b"foo ") => {
  |              ^
  |              |
  |              move out of `v.0` occurs here
  |              borrow of `v.0` occurs here
4 |             println!("{:?}", r);
  |                              - borrow later used here
Desired output
error[E0505]: cannot move out of `v.0` because it is borrowed
 --> src/lib.rs:3:14
  |
3 |         Some(v) if let Some(r) = v.strip_prefix(b"foo ") => {
  |                                  ^                       ^
  |                                  |                       |
  |                   borrow of `v.0` occurs here     move out of `v.0` occurs here
4 |             println!("{:?}", r);
  |                              - borrow later used here
help: try borrowing the value here:
  |         Some(ref v) if let Some(r) = v.strip_prefix(b"foo ") => {
Rationale and extra context

Currently the error is quite opaque and points to entirely the wrong location.

What's doing the borrow is pretty easy to infer but why the value is being moved out is not as easy, especially since having to think about the data flow of patterns has become a lot less routine since match ergonomics.

I would assume before if_let_guard the only (easy?) way to get this error was using at-patterns which mixed moving things and creating overlapping references, then trying to use those references in the expression, which would be why the compiler points everything into the pattern.

The suggestion would not be machine applicable, as there could also be a legitimate borrow error (e.g. the expression tries to use both the owned value and its sub-borrow), in which case the suggestion is incorrect

Rust Version
rustc 1.100.0 (0ef1bc386 2026-11-09)
binary: rustc
commit-hash: 0ef1bc386059596dada8fb65b50425aab2ea637a
commit-date: 2026-11-09
host: x86_64-unknown-linux-gnu
release: 1.100.0
LLVM version: 23.1.5
Lenguaje dominante
Rust
Estrellas
122k
Forks
16.7k
Merge medio
2 d 16 h
PR fusionados (30 d)
515

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rust

Todos los issues de rust-lang/rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.