本文簡要介紹rust語言中 slice.swap
的用法。
用法
pub fn swap(&mut self, a: usize, b: usize)
交換切片中的兩個元素。
參數
- a - 第一個元素的索引
- b - 第二個元素的索引
Panics
如果 a
或 b
超出範圍,則會出現Panics。
例子
let mut v = ["a", "b", "c", "d", "e"];
v.swap(2, 4);
assert!(v == ["a", "b", "e", "d", "c"]);
相關用法
- Rust slice.swap_unchecked用法及代碼示例
- Rust slice.swap_with_slice用法及代碼示例
- Rust slice.sort_unstable_by_key用法及代碼示例
- Rust slice.sort_unstable_by用法及代碼示例
- Rust slice.sort用法及代碼示例
- Rust slice.split_array_mut用法及代碼示例
- Rust slice.splitn_mut用法及代碼示例
- Rust slice.split_first用法及代碼示例
- Rust slice.splitn用法及代碼示例
- Rust slice.split_array_ref用法及代碼示例
- Rust slice.sort_by用法及代碼示例
- Rust slice.split用法及代碼示例
- Rust slice.split_inclusive用法及代碼示例
- Rust slice.split_mut用法及代碼示例
- Rust slice.strip_suffix用法及代碼示例
- Rust slice.split_last_mut用法及代碼示例
- Rust slice.split_first_mut用法及代碼示例
- Rust slice.select_nth_unstable用法及代碼示例
- Rust slice.sort_unstable用法及代碼示例
- Rust slice.split_at_mut用法及代碼示例
- Rust slice.select_nth_unstable_by用法及代碼示例
- Rust slice.split_at_unchecked用法及代碼示例
- Rust slice.split_at用法及代碼示例
- Rust slice.split_at_mut_unchecked用法及代碼示例
- Rust slice.strip_prefix用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 slice.swap。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。