issues with Lifetime branding example
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
- Área
- documentation
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
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
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Issues similares
-
Replayed reasoning items send "content": null, which the Responses API schema does not permit Abiertobug CLI custom-model
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
rust-bitcoin/rust-bitcoin#6930 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
fulcrumgenomics/ferro-hgvs#2251 ·
-
Missing examples for `Allocator` AbiertoA-allocators A-docs C-enhancement T-libs
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100