记录一些Rust中的小技巧
2024年12月11日大约 1 分钟
记录一些Rust中的小技巧
使用指定的Rust版本
对于管理多版本,直接使用Rustup即可。在cmd中输入
rustup show
即可查看已安装的工具链版本,以及现在使用的默认版本。使用
rustup default 1.72.0-x86_64-pc-windows-gnu
即可切换到对应版本。
但如果不想切换默认Rust版本,只想为某一项目使用指定版本,可以在项目所在目录新建rust-toolchain
文件,在第一行写上版本限制,例如:
1.76.0
1.76.0-x86_64-pc-windows-gnu
如果需要更详细地指定工具链版本,可以新建一个rust-toolchain.toml
文件,例如:
[toolchain]
channel = "nightly-2024-11-07"
components = [ "rustfmt", "rust-analyzer", "miri", "rust-docs", "clippy", "rust-src"]
profile = "default"
以上配置将在此项目中启用nightly特性。
pub关键字
pub
关键字能控制字段、方法的可访问性,且能限定到在特定模块。
/// 在本项目内public
pub(crate) fn hello() {
}
/// 仅在mod `ir`内public
pub struct Graph {
pub(in crate::ir) globals: GlobalValueMapCell,
pub(in crate::ir) func_tys: FuncTypeMapCell,
values: HashMap<Value, ValueData>,
bbs: HashMap<BasicBlock, BasicBlockData>,
}
Rust中的@lombok.Delegate
#[derive(Default)]
struct A {
inner: B,
}
#[derive(Default)]
struct B {
a: i32,
b: u64,
}
impl B {
pub fn hello(&self) {
todo!()
}
}
fn test() {
let a = A::default();
a.hello();
}
// 实现了以下两个方法后,可以把self.inner的方法代理到外部对象
impl std::ops::Deref for A {
type Target = B;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'a> std::ops::DerefMut for A {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
Ref
- https://github.com/pku-minic/koopa/blob/master/src/ir/dfg.rs#L15