Rust指針選擇:
1.優先使用引用:安全訪問數據
fn process(data: &[i32]) { /* ... */ }
2.需要所有權轉移時用 Box
fn create() -> Box<Data> { Box::new(Data::new()) }
3.共享數據用 Rc/Arc
// 單線程
let shared = Rc::new(data);// 多線程
let thread_safe = Arc::new(Mutex::new(data));
4.內部可變性用 RefCell
let cell = RefCell::new(42);
*cell.borrow_mut() = 100;
5.底層操作/FFI 用裸指針
extern "C" {fn c_function(ptr: *const libc::c_char);
}
注:學好rust從入門到入土!!!