Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

issues with Lifetime branding example

Aperta
#125 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
documentation

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 ()>,
}
Lingua principale
Rust
Stelle
14.9k
Fork
1.2k
Merge medio
13g 16h
PR unite (30g)
3

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.