05 2022 档案
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 get_mut fn main() { let mut map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]); if let Some(v) = map.get_mut("c") {
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 hasher fn main() { let hasher = RandomState::new(); let map: HashMap<i32, i32> = HashMap::with_hasher(hasher); print
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 into_values fn main() { let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]); map.into_values().for_each(|e| prin
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 new fn main() { let map: HashMap<&str, i32> = HashMap::new(); println!("{:?}", map.capacity()); } with_capacity fn m
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 into_boxed_bytes fn main() { let name = "JiangBo"; println!("{:?}", name.to_owned().into_boxed_str().into_boxed_byte
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 strip_suffix fn main() { let name = "\nJiang\nBo\n"; println!("{:?}", name.strip_suffix('\n')); } parse fn main() {
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 match_indices fn main() { let name = "\nJiang\nBo\n"; name.match_indices(char::is_alphabetic) .for_each(|e| println!
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 rsplit fn main() { let name = "Jiang\nBo"; name.rsplit('\n').for_each(|e| println!("{e:?}")); } split_terminator fn
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 lines fn main() { let name = "Jiang\nBo"; name.lines().for_each(|e| println!("{e:?}")); } encode_utf16 fn main() { l
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 get_unchecked fn main() { let name = "JiangBo"; println!("{:?}", unsafe { name.get_unchecked(5..) }); } get_unchecke
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 len fn main() { let name = "JiangBo"; println!("{:?}", name.len()); } is_empty fn main() { let name = "JiangBo"; pri
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 drain fn main() { let mut name = String::from("JiangBo"); let name = name.drain(..5); println!("{:?}", name); } repl
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 remove fn main() { let mut name = String::from("JiangBo"); name.remove(4); println!("{name:?}"); } retain fn main()
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 capacity fn main() { let name = String::from("JiangBo"); println!("{:?}", name.capacity()); } reserve fn main() { le
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 new fn main() { let s = String::new(); println!("{:?}", s.capacity()); } with_capacity fn main() { let s = String::w
阅读全文
摘要:环境 Time 2022-03-25 Rust 1.59.0 示例 or fn main() { let result: Result<u32, u32> = Ok(44); println!("{:?}", result.or(Err(4))); } or_else fn main() { let
阅读全文
摘要:环境 Time 2022-03-25 Rust 1.59.0 示例 as_deref fn main() { let result: Result<String, u32> = Ok("ok".to_owned()); let result: Result<&str, &u32> = result.
阅读全文
摘要:环境 Time 2022-03-24 Rust 1.59.0 示例 is_ok fn main() { let result: Result<i32, &str> = Ok(44); println!("{:?}", result.is_ok()); } is_err fn main() { let
阅读全文
摘要:环境 Time 2022-03-23 Rust 1.59.0 示例 find_map fn main() { let vec = ["lol", "NaN", "2", "5"]; let result: Option<i32> = vec.iter().find_map(|s| s.parse()
阅读全文
摘要:环境 Time 2022-03-23 Rust 1.59.0 示例 collect fn main() { let vec = vec![0, 1, 2, 3, 4]; let doubled: Vec<_> = vec.iter().map(|e| e * 2).collect(); for el
阅读全文
摘要:环境 Time 2022-03-22 Rust 1.59.0 示例 take_while 在找到返回 false 的值后,忽略剩余所有元素。 fn main() { let vec = vec![0, 1, 2, 3, 4]; vec.iter() .take_while(|&e| e % 2 ==
阅读全文
摘要:环境 Time 2022-03-22 Rust 1.59.0 示例 for_each 和 for 循环类型,遍历每个元素。 fn main() { let vec = vec![0, 1, 2, 3, 4]; vec.iter().for_each(|e| println!("{e}")); } m
阅读全文
摘要:环境 Time 2022-03-22 Rust 1.59.0 示例 iter 生成一个迭代器,可变版本 iter_mut,所有权版本 into_iter。 fn main() { let vec = vec![0, 1, 2, 3, 4]; let iter = vec.iter(); printl
阅读全文
摘要:环境 Time 2022-03-17 Rust 1.59.0 概念 动态数组分配在栈上,长度可以变化。 示例 leak fn main() { let vec = vec![0, 1, 2, 3, 4]; let leak = vec.leak(); println!("{leak:?}"); }
阅读全文
摘要:环境 Time 2022-03-17 Rust 1.59.0 概念 动态数组分配在栈上,长度可以变化。 示例 append 连接两个动态数组。 fn main() { let mut vec = vec![0, 1, 2, 3, 4]; let mut vec1 = vec![44, 55]; ve
阅读全文
摘要:环境 Time 2022-03-16 Rust 1.59.0 概念 动态数组分配在栈上,长度可以变化。 示例 insert 在给定的位置进行插入元素。 fn main() { let mut vec = vec![0, 1, 2, 3, 4]; vec.insert(2, 44); println!
阅读全文
摘要:环境 Time 2022-03-16 Rust 1.59.0 概念 动态数组分配在栈上,长度可以变化。 示例 truncate 从给定的位置进行截取,后面的元素将被丢弃。 fn main() { let mut vec = vec![0, 1, 2, 3, 4]; vec.truncate(2);
阅读全文
摘要:环境 Time 2022-03-16 Rust 1.59.0 概念 动态数组分配在栈上,长度可以变化。 示例 reserve 保留额外空间,相当于扩容,容量可能会比扩容的大,带异常版本:try_reserve。 fn main() { let mut vec = Vec::new(); vec.pu
阅读全文
摘要:环境 Time 2022-03-16 Rust 1.59.0 概念 动态数组分配在栈上,长度可以变化。 示例 new 新建一个动态数组,如果没有增加元素,不会分配堆空间。 fn main() { let vec: Vec<i32> = Vec::new(); println!("{vec:?}");
阅读全文
摘要:环境 Time 2022-03-08 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 sort 对数组切片进行排序。 fn main() { let mut arr = [0, 4, 3, 1, 2]; arr.sort(); println!("{arr:?}"); } s
阅读全文
摘要:环境 Time 2022-03-08 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 split_first 截取第一个元素,可变版本 split_first_mut。 fn main() { let arr = [0, 1, 2, 3, 4]; if let Some((f
阅读全文
摘要:环境 Time 2022-03-07 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 repeat 对元素进行重复。 fn main() { let arr = [0, 1]; let vec = arr.repeat(4); println!("{vec:?}"); //[
阅读全文
摘要:环境 Time 2022-03-07 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 contains 是否包含某个元素。 fn main() { let arr = [0, 1, 2, 3, 4]; let contains = arr.contains(&2); prin
阅读全文
摘要:环境 Time 2022-03-01 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 concat 直接将切片的每个值连接起来。 fn main() { let arr = [[0, 1], [2, 3], [4, 5]]; let concat = arr.concat()
阅读全文
摘要:环境 Time 2022-03-01 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 clone 直接克隆数据。 fn main() { let arr = ["Python", "Java", "C++", "Rust"]; let src = arr.map(|e| e.
阅读全文
摘要:环境 Time 2022-03-01 Rust 1.59.0 概念 数组切片是引用数组中连续的一部分。 示例 chunks 按照固定的长度,截取成一个迭代器,最后一个可能不满足长度要求。 fn main() { let arr = [0, 1, 2, 3, 4]; let chunks = arr.
阅读全文
摘要:环境 Time 2022-02-25 Rust 1.59.0 概念 引用数组中连续的一部分。 示例 二分查找-命中 如果查找到了,返回 Ok 和找到的位置索引。 fn main() { let arr = [1, 2, 3, 4]; let result = arr.binary_search(&2
阅读全文
摘要:环境 Time 2022-02-25 Rust 1.59.0 概念 一个固定长度的集合,存储在栈上。 示例 单个初始化 fn main() { let arr = [0, 1, 2, 3]; println!("{:?}", arr); } 批量初始化 fn main() { let arr = [
阅读全文