20250804-daily-rust
发表于|更新于|daily
|总字数:76|阅读时长:1分钟|浏览量:
Rust Tips
一种非常方便的方法来过滤 HashMap 数据
1 | use std::collections::HashMap; |
文章作者: fan-tastic-z
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 DoYouDo!
相关推荐
2025-07-29
20250729-daily-rust
Rust TipsRust 中 iterators 非常强大,两个实用的函数: peekable: 允许查看下一个元素但不使用它 partition(cond_fn): 根据条件将迭代器划分为单独的集合 1234567891011fn main() { let mut iter = vec![1, 2, 3].into_iter().peekable(); let a = iter.peek(); println!("{:?}", a); let b = iter.next(); println!("{:?}", b); let (events, odds): (Vec<_>, Vec<_>) = (1..6).partition(|&x| x % 2 == 0); println!("{:?}", events); // [2, 4] println!("&...
2025-07-30
20250730-daily-rust
Rust Tips12345678910111213141516fn main() { my_assert(false);}#[track_caller]fn my_assert(condition: bool) { if !condition { let caller = std::panic::Location::caller(); panic!( "Assertion failed at {}:{}:{}", caller.file(), caller.line(), caller.column(), ); }} Rust中的track_caller宏,可以帮我们精准定位代码调用点。上面的代码,当我们使用了 track_caller,运行代码提示: 123thread 'main...
2025-07-31
20250731-daily-rust
Rust Tipsdrain 从 vector 批量删除指定范围,并以迭代器的形式返回所有移除的元素。 123456fn main() { let mut numbers = vec![1, 2, 3, 4, 5]; let evens: Vec<_> = numbers.drain(..).filter(|&x| x % 2 == 0).collect(); println!("Even numbers: {:?}", evens); // Even numbers: [2, 4] println!("Remaining numbers: {:?}", numbers); // Remaining numbers: []} AI 趣事https://x.com/dotey/status/1950733173714756042
2025-08-01
20250801-daily-rust
Rust TipsRust 2024: let chains in if and while 123456789101112131415161718fn main() { let numbers = [10, 20, 30, 40]; match sum_first_two(&numbers) { Some(sum) => println!("The sum of the first two numbers is: {}", sum), None => println!("Not enough numbers to sum."), }}fn sum_first_two(nums: &[u8]) -> Option<u8> { let mut iter = nums.iter(); if let Some(first) = iter.next() &...
2025-08-08
20250808-daily-rust
Rust 学习 https://www.tisonkun.org/2025/08/05/trickle-down-oss/: 文章介绍了“涓滴开源”模式,即从商业软件开发中提炼出微小且完整的模块并开源。 以Cronexpr为例,该库是为ScopeDB云数据库解析Crontab表达式而开发的。作者在调研现有库后,决定自行开发,Cronexpr实现了对时区信息的支持,并使用了可靠的jiff和winnow库。Cronexpr已发布1.0版本,稳定运行。 涓滴开源模式使ScopeDB能够获得反馈,并为其他用户提供价值。作者认为这种模式具有可持续性,并可提升技术品牌影响力。 https://xuanwo.io/2025/05-learn-rust-by-reasoning-with-code-agents/:文章探讨了通过代码代理进行Rust学习的方法——“通过推理学习”。这种方法强调,学习不应仅限于阅读或实践,更重要的是理解代码背后的逻辑。作者提出,通过阅读代码、提出问题并思考“为什么”和“如果会怎样”,可以更深入地理解Rust的复杂特性,例如所有权、特征和生命周期。文章详细介绍了“推理...
2025-07-31
tips-for-faster-rust-compile
Updatge Rust Compiler And ToolChainRust 一直在持续不断的优化编译的速度,因此定期更新你的 Rust 编译器和工具链是非常重要的。 1rustup update Cargo CheckCargo check 是一个非常有用的工具,它可以在不编译代码的情况下检查你的代码是否有语法错误或其他问题。这可以大大加快你的开发速度。 Remove Unused Dependencies1cargo install cargo-machete && cargo machete Update Dependencies cargo update 会自动更新所有依赖项到最新版本。 cargo tree --duplicate 查找多个版本的依赖项,目的是通过更新依赖旧版本的依赖项来合并到单个版本(待验证) Faster Linker Linux/Macos: https://lld.llvm.org/ Linux: https://github.com/rui314/mold 目前github 上比较活跃的还有: https:/...
评论