issues with Lifetime branding example
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
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
state:needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
zed-industries/zed#64680 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
RustPython/RustPython#8802 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
TheLarkInn/aipm#2390 ·