← 返回卡片

原书出处

Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)

2 Chapter 2. Traits Item 11: Implement the Drop trait for RAII patterns

First Edition · 核验版本的摘录快照 · 导入于 2026-11-14T02:52:10.271Z

de; to ensure this,

use blocks to restrict the scope of RAII items. This leads to slightly odd indentation, but it’s worth it for the added safety and lifetime precision: impl ThreadSafeInt { fn add_with_extras(&self, delta: i32) { // ... more code here that doesn't need the lock { let mut v = self.value.lock().unwrap(); *v += delta; } // ... more code here that doesn't need th

e lock } } Having pr

此预览只包含定位所需的摘录和上下文。

原书定位详情

来源:a19c7933-4eda-4176-aebd-30fe7e1ad292

正文位置:extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html#file_raii_md

EPUB SHA-256:523496927c4fd2d3fc11c9b81ad96b76201e66d746b6ac29d71d61d3517181c1