Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

issues with Lifetime branding example

Đang mở
#125 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
documentation

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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 ()>,
}
Ngôn ngữ chính
Rust
Star
14.9k
Fork
1.2k
Merge trung bình
4 giờ 19 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.