Rust版本更新: 1.84.1

1.84.1修复了在1.84.0中引入的一些回归问题: 修复ICE 132920中的重复箱诊断。 修复增量编译过程中的错误,涉及到重叠实现。 修复与下一代特征求解器相关的慢速编译。 修复LLVM的位置判别符值限制超过时的debuginfo。 此外,还包括一些从源代码构建Rust时的修复: 只有在LLVM工具被启用时才尝试分发llvm-objcopy。 为非Git来源添加配置覆盖选项。 在拷贝它们之前,解除LLVM工具二进制文件的符号链接。 使它可能使用ci-rustc在tarball源上。

二月 1, 2025 · 1 分钟 · anoya

Rust常用设计模式

设计模式的使用,是对开发者的考验。比如说在 Golang 中最常用的是工厂模式,创建模块时,以 Struct 为基础,常命名为 NewXX 开头发的方法作为工厂模式。而在 Rust 中,常用的则是建造模式(builder),由此来创建某一模块或功能的集合。 另外在 Rust 中独有的模式,例如 Drop Builder Builder1 is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code. 建造者是一个创造性的设计模式,让你一步步构建复杂的对象。模式允许你使用相同的结构代码构建不同的类型和表现对象。 例子使用Box Builder,构建不同尺寸的盒子,通过链式调用设置不同的盒子属性 struct Box { width: u32, height: u32, } struct BoxBuilder { width: Option<u32>, height: Option<u32>, } impl BoxBuilder { fn new() -> Self { BoxBuilder { width: None, height: None, } } fn default() -> Self { BoxBuilder { width: Some(30), height: Some(30), } } fn set_width(mut self, width: u32) -> Self { self.width = Some(width); self } fn set_height(mut self, height: u32) -> Self { self.height = Some(height); self } fn build(self) -> Box { let width = self.width.unwrap(); let height = self.height.unwrap(); Box { width, height } } } fn main() { let box1: Box = BoxBuilder::new().set_height(20).set_width(10).build(); println!( "the box1 width is {}, height is {}.", box1.width, box1.height ); let box2: Box = BoxBuilder::default().build(); println!( "the box2 width is {}, height is {}.", box2.width, box2.height ); } output: ...

一月 24, 2025 · 4 分钟 · anoya

Rust常用模块之Env log

1 安装 Install with Crate # 创建名为log的项目 cargo new mylog # 进入到目录执行命令,将所需模块加载当前环境目录中 cargo add log env_logger 在 Cargo.toml 中的[dependencies]可以看到具体模块信息如下方所示 [package] name = "mylog" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] env_logger = "0.10.0" log = "0.4.19" 2 使用 通过拷贝文档中的Example1,并添加方法体 use log::{debug, error, info, log_enabled, Level}; fn main() { env_logger::init(); debug!("this is a debug {}", "message"); error!("this is printed by default"); if log_enabled!(Level::Info) { let x = 3 * 4; // expensive computation info!("the answer was: {}", x); } } Output: ...

一月 24, 2025 · 1 分钟 · anoya

(译文)Rust版本更新: 1.84.0

Cargo考虑Rust版本来选择依赖版本 1.84.0稳定了支持的最低Rust版本(MSRV)感知解析器,该解析器首选与项目声明的MSRV兼容的依赖版本。通过MSRV感知版本选择,维护者无需为每个依赖项手动选择旧版本,减少了支持旧工具链的工作量。 您可以通过.cargo/config.toml选择加入MSRV-aware解析器: [resolver] incompatible-rust-versions = "fallback" 然后,在添加依赖项时: $ cargo add clap Updating crates.io index warning: ignoring clap@4.5.23 (which requires rustc 1.74) to maintain demo's rust-version of 1.60 Adding clap v4.0.32 to dependencies Updating crates.io index Locking 33 packages to latest Rust 1.60 compatible versions Adding clap v4.0.32 (available: v4.5.23, requires Rust 1.74) 当验证CI中的最新依赖项时,您可以覆盖此: $ CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=allow cargo update Updating crates.io index Locking 12 packages to latest compatible versions Updating clap v4.0.32 -> v4.5.23 您还可以通过在Cargo.toml清单文件中设置package.resolver = "3"来选择加入,尽管这将需要将您的MSRV提高到1.84。默认情况下,新解析器将为使用2024版本(将在1.85中稳定)的项目启用。 ...

一月 9, 2025 · 1 分钟 · anoya

(原文)Rust版本更新: 1.84.0

Cargo considers Rust versions for dependency version selection 1.84.0 stabilizes the minimum supported Rust version (MSRV) aware resolver, which prefers dependency versions compatible with the project’s declared MSRV. With MSRV-aware version selection, the toil is reduced for maintainers to support older toolchains by not needing to manually select older versions for each dependency. You can opt-in to the MSRV-aware resolver via .cargo/config.toml: [resolver] incompatible-rust-versions = "fallback" Then when adding a dependency: $ cargo add clap Updating crates.io index warning: ignoring clap@4.5.23 (which requires rustc 1.74) to maintain demo's rust-version of 1.60 Adding clap v4.0.32 to dependencies Updating crates.io index Locking 33 packages to latest Rust 1.60 compatible versions Adding clap v4.0.32 (available: v4.5.23, requires Rust 1.74) When verifying the latest dependencies in CI, you can override this: ...

一月 9, 2025 · 4 分钟 · anoya