Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

issues with Lifetime branding example

Abierto
#125 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust

Línea de trabajo

Start in rust-patterns-book/src/ch04-phantomdata-types-that-carry-no-data.md around line 44 and review the Lifetime branding example and its surrounding explanation. Compile both arrangements shown in the issue, then update the example so handles cannot be used with a different arena and verify the intended success and failure cases.

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

Descripción

https://github.com/microsoft/RustTraining/blob/278f1d7ee0e5432c1a4e1e4d2a749796f60e7711/rust-patterns-book/src/ch04-phantomdata-types-that-carry-no-data.md?plain=1#L44

The example as written fails to compile when we uncomment the code:

fn main() {
    with_arena(|arena1| {
        let handle1 = arena1.alloc("hello".to_string());
        println!("{}", arena1.get(&handle1)); // ✅

        // Can't use handle1 with a different arena — compile-time error
        with_arena(|arena2| {
            arena2.get(&handle1); // ❌ borrowed data escapes outside of closure
        });
    });
}

If we rearrange the code a bit:

fn main() {
    with_arena(|arena1| {
        // Can't use handle1 with a different arena — compile-time error
        with_arena(|arena2| {
            let handle1 = arena1.alloc("hello".to_string());
            println!("{}", arena1.get(&handle1)); // ✅
            arena2.get(&handle1); // ❌ borrowed data escapes outside of closure
        });
    });
}

This compiles - even though it isn't supposed to.

By moving the brand to the Arena instead of keeping it on the ArenaHandle it works as intended:

/// A handle branded to a specific arena instance.
/// Invariant over 'arena — prevents using a handle from one arena with another.
struct ArenaHandle<'arena> {
    index: usize,
    _phantom: PhantomData<&'arena ()>,
}

/// An arena that brands each handle with its unique lifetime.
struct Arena<'arena> {
    data: RefCell<Vec<String>>,
    _brand: PhantomData<*mut &'arena ()>,
}
Lenguaje dominante
Rust
Estrellas
14.9k
Forks
1.2k
Merge medio
13 d 16 h
PR fusionados (30 d)
3

Guía de contribución

Abrir la guía de contribución

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.

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.