rust学习: 有用的命令

在学习rust的过程中, 发现一些有用的命令, 记录之.

cargo subcommands

cargo 子命令(cargo subcommands)是一种有用的机制, 可以对名为cargo-xxx的命令通过 cargo xxx来调用.

cargo官方整理了一份cargo subcommands在这里:
https://github.com/rust-lang/cargo/wiki/Third-party-cargo-subcommands

cargo-expand

可以将rust中的宏展开.

代码仓库: https://github.com/dtolnay/cargo-expand

例子:
cargo new demo

修改main.rs内容如下:

rust 复制代码
#[derive(Debug)]
struct User {
    name: String,
    age: u8,
}

fn main() {
    let u = User{
        name: String::from("John"),
        age: 30,
    };
    println!("{:?}", u);
}

运行cargo expand, 效果:

rust 复制代码
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
struct User {
    name: String,
    age: u8,
}
#[automatically_derived]
impl ::core::fmt::Debug for User {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(
            f,
            "User",
            "name",
            &self.name,
            "age",
            &&self.age,
        )
    }
}
fn main() {
    let u = User {
        name: String::from("John"),
        age: 30,
    };
    {
        ::std::io::_print(format_args!("{0:?}\n", u));
    };
}

这样方便我们了解宏展开的效果.

其他

tokei

统计代码, 据说是非常快. 我用过的统计代码的工具还有 https://github.com/AlDanial/cloc.

这里简单做个比较, 统计的仓库是 neo4j的5.4版本 :

可以看到, tokei统计的文件数比cloc要多, 应该是两个命令默认的规则不一样. 时间上面 tokei 是"秒出结果", 而cloc总用时11秒多.

确实如 tokei 介绍的一样: Tokei is very fast

(持续更新)

相关推荐
全栈陈序员3 小时前
基于Rust 实现的豆瓣电影 Top250 爬虫项目
开发语言·爬虫·rust
百锦再3 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
JosieBook5 小时前
【Rust】基于Rust + WebAssembly;实现人机记忆井字棋游戏(人机对战)
游戏·rust·wasm
万事可爱^9 小时前
GitHub爆火开源项目——RustScan深度拆解
c语言·开发语言·rust·开源·github·rustscan
受之以蒙15 小时前
具身智能的“任督二脉”:用 Rust ndarray 打通数据闭环的最后一公里
人工智能·笔记·rust
有梦想的攻城狮15 小时前
初识Rust语言
java·开发语言·rust
JosieBook1 天前
【Rust】基于Rust 设计开发nginx运行日志高效分析工具
服务器·网络·rust
四问四不知1 天前
Rust语言入门
开发语言·rust
JosieBook1 天前
【Rust】 基于Rust 从零构建一个本地 RSS 阅读器
开发语言·后端·rust
云边有个稻草人1 天前
部分移动(Partial Move)的使用场景:Rust 所有权拆分的精细化实践
开发语言·算法·rust