Skip to content

Latest commit

 

History

History
184 lines (137 loc) · 4.13 KB

100.md

File metadata and controls

184 lines (137 loc) · 4.13 KB

Rust 学习 100 个练习

如何调试

如何写 cli

如何读取 cli 配置

如何 ctrlc

cargo-debug 里 ctrlc 做法

// Override ctrl+c handler to avoid premature exit
// TODO: this... doesn't stop the rust process exiting..?
ctrlc::set_handler(move || {
    warn!("CTRL+C");
    let mut then = b.lock().unwrap();
    let now = SystemTime::now();
    if now.duration_since(*then).unwrap() > Duration::from_secs(1) {
        std::process::exit(0);
    } else {
        *then = now;
    }
}).expect("Error setting Ctrl-C handler");

如何执行 scripts

mdbook serve . -o这样的命令我不想每次都翻查记录,我希望像 npm 一样,比如 npm run docs 就可以直接执行 于是,找到了下面这个解决方案。

cargo-run-script

参考https://github.com/JoshMcguigan/cargo-run-script

$ cargo install cargo-run-script

配置文件

[package.metadata.scripts]
hello = "echo Hello"
goodbye = "echo Goodbye"

$ cargo run-script hello

看起来还行,可是还是很烦。

cargo-make

好朋友 fundon 推荐 cargo-make,于是又试了一下

修改 cargo.toml

[package]
name = "rust-fe"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]


[tasks.run]
script = "echo hello"
➜  rust-fe git:(main) ✗ cargo install --no-default-features --force cargo-make
➜  rust-fe git:(main) ✗ cargo make run
[cargo-make] INFO - cargo make 0.36.2
[cargo-make] INFO - Project: rust-fe
[cargo-make] INFO - Build File: Makefile.toml
[cargo-make] INFO - Task: run
[cargo-make] INFO - Profile: development
[cargo-make] ERROR - Task run not found
[cargo-make] WARN - Build Failed.
➜  rust-fe git:(main) ✗ cargo make --makefile Cargo.toml run
[cargo-make] INFO - cargo make 0.36.2
[cargo-make] WARN - Found unknown key: package in file: ./Cargo.toml
[cargo-make] WARN - Found unknown key: dependencies in file: ./Cargo.toml
[cargo-make] INFO - Project: rust-fe
[cargo-make] INFO - Build File: Cargo.toml
[cargo-make] INFO - Task: run
[cargo-make] INFO - Profile: development
[cargo-make] INFO - Running Task: legacy-migration
[cargo-make] INFO - Running Task: run
hello
[cargo-make] INFO - Build Done in 1.56 seconds.

我不太理解为啥 cargo-make 不默认读 cargo.toml。另外,这安装依赖也大的离谱,读 cargo-make 源码

[dependencies]
cargo_metadata = "^0.15"
ci_info = "^0.14.7"
cliparser = "^0.1.2"
colored = "^2"
ctrlc = "^3.2.3"
dirs-next = "^2"
duckscript = "^0.7.5"
duckscriptsdk = { version = "^0.8.16", default-features = false }
envmnt = "^0.10.4"
fern = "^0.6"
fsio = { version = "^0.4", features = ["temp-path"] }
git_info = "^0.1.2"
glob = "^0.3"
home = "^0.5"
indexmap = { version = "^1", features = ["serde-1"] }
ignore = "^0.4"
lenient_semver = "^0.4.2"
log = "^0.4"
once_cell = "^1.16.0"
petgraph = "^0.6.2"
regex = "^1.7.0"
run_script = "^0.10"
rust_info = "^0.3.1"
semver = "^1"
serde = "^1"
serde_derive = "^1"
serde_ignored = "^0.1"
serde_json = "^1"
shell2batch = "^0.4.5"
toml = "^0.5"

以我的安装方式https://github.com/sagiegurari/cargo-make#installation,已经非常小了,不可思议。

   Compiling cargo-make v0.36.2
    Finished release [optimized] target(s) in 1m 56s
  Installing /Users/i5ting/.cargo/bin/cargo-make
  Installing /Users/i5ting/.cargo/bin/makers
   Installed package `cargo-make v0.36.2` (executables `cargo-make`, `makers`)

just

https://github.com/casey/just

最简单的make

Makefile

test:
	cargo test && cd proxy && cargo test && cd ..

build:
	cargo build --release

fmt: 
	cargo fix --allow-dirty --allow-staged && cargo fmt && cd proxy && cargo fix --allow-dirty --allow-staged && cargo fmt && cd .. 

cloc:
	cloc . --exclude-dir=target, proxy/target, .vscode, .idea

curl:
	curl -I http://httpbin.org -v -x 127.0.0.1:8080 --proxy-user rust:proxy

justfile 里学习到的内容

https://github.com/casey/just/blob/master/justfile

jq 一样的链式操作

火焰图

http://carol-nichols.com/2017/04/20/rust-profiling-with-dtrace-on-osx/

perf flamegraph VTune

Updating crates.io index