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

(持续更新)

相关推荐
无名之逆15 小时前
你可能不需要WebSocket-服务器发送事件的简单力量
java·开发语言·前端·后端·计算机·rust·编程
Source.Liu17 小时前
【egui】界面的坐标系统:f32 一统江湖
rust·egui
班公湖里洗过脚1 天前
Rust解析mp3文件及工作目录下多个项目维护示例
rust
sunny_1 天前
构建工具的第三次革命:从 Rollup 到 Rust Bundler,我是如何设计 robuild 的
前端·rust·前端工程化
用户0235087373122 天前
第02篇:5分钟上手 blockcell —— 从安装到第一次对话
rust
badmonster02 天前
我写了一个超轻量MCP,让编码Agent真正理解你的代码——Token消耗减少70%,1分钟接入
rust·ai编程
RoyLin2 天前
10美元硬件中可运行的隐私 LLM 推理引擎
人工智能·rust·agent
Source.Liu2 天前
【egui】[特殊字符] 窗口配置小抄:eframe::NativeOptions
rust·egui
Hello.Reader2 天前
Tauri 项目结构前端壳 + Rust 内核,怎么协作、怎么构建、怎么扩展
开发语言·前端·rust
Source.Liu2 天前
【egui】[特殊字符]简单、快速、跨平台的 Rust GUI 库
rust·egui