本文簡要介紹rust語言中 std::vec::Vec.leak
的用法。
用法
pub fn leak<'a>(self) -> &'a mut [T] where A: 'a,
消耗並泄漏 Vec
,返回對內容的可變引用 &'a mut [T]
。請注意,類型 T
必須比選擇的生命周期長 'a
。如果類型隻有靜態引用,或者根本沒有,那麽可以選擇 'static
。
從 Rust 1.57 開始,此方法不會重新分配或收縮 Vec
,因此泄漏的分配可能包括不屬於返回切片的未使用容量。
此函數主要用於在程序剩餘生命周期中存在的數據。刪除返回的引用將導致內存泄漏。
例子
簡單用法:
let x = vec![1, 2, 3];
let static_ref: &'static mut [usize] = x.leak();
static_ref[0] += 1;
assert_eq!(static_ref, &[2, 2, 3]);
相關用法
- Rust Vec.len用法及代碼示例
- Rust Vec.drain用法及代碼示例
- Rust Vec.into_raw_parts用法及代碼示例
- Rust Vec.resize用法及代碼示例
- Rust Vec.swap_remove用法及代碼示例
- Rust Vec.is_empty用法及代碼示例
- Rust Vec.reserve_exact用法及代碼示例
- Rust Vec.retain_mut用法及代碼示例
- Rust Vec.try_reserve_exact用法及代碼示例
- Rust Vec.new_in用法及代碼示例
- Rust Vec.insert用法及代碼示例
- Rust Vec.retain用法及代碼示例
- Rust Vec.with_capacity用法及代碼示例
- Rust Vec.from_raw_parts用法及代碼示例
- Rust Vec.into_boxed_slice用法及代碼示例
- Rust Vec.reserve用法及代碼示例
- Rust Vec.dedup_by用法及代碼示例
- Rust Vec.shrink_to_fit用法及代碼示例
- Rust Vec.as_mut_slice用法及代碼示例
- Rust Vec.dedup用法及代碼示例
- Rust Vec.set_len用法及代碼示例
- Rust Vec.as_ptr用法及代碼示例
- Rust Vec.capacity用法及代碼示例
- Rust Vec.into_raw_parts_with_alloc用法及代碼示例
- Rust Vec.splice用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::vec::Vec.leak。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。