Rust
跳转到导航
跳转到搜索
优化
使用 -O 进行优化。静态链接时,使用 -C lto 进行链接期优化(可减小生成的可执行文件的大小)。
使用环境变量 RUSTFLAGS="-C target-cpu=broadwell" 可以为指定的 CPU 优化。
动态链接
加-C prefer-dynamic或者指定--extern到动态库,如:
rustc -O a.rs --extern time=/ldata/txtfiles/src/time-rs/target/release/libtime-496a5b4f73037be8.so -L ~txtfiles/src/time-rs/target/release/deps -C rpath
指定-C rpath可以链接到库搜索路径之外的库文件。
Cargo.toml 文件中,同时编译两种库:
[lib]
name = "time"
crate_type = ["dylib", "rlib"]
构建时加--verbose查看编译命令,在最后的rustc命令上加上-C prefer-dynamic。
交叉编译
安装对应平台的库到/usr/share/rustlib下,如 Win32 则将bin/rustlib/i686-pc-windows-gnu安装到/usr/lib/rustlib/i686-pc-windows-gnu。然后配置链接器和 ar,如在~/.cargo/config中写入如下内容:
[target.i686-pc-windows-gnu]
linker = "/usr/bin/i686-w64-mingw32-gcc"
ar = "/usr/i686-w64-mingw32/bin/ar"
# vim:se ft=toml:
则可:
cargo build --release --target i686-pc-windows-gnu
注意:要运行生成的 Win32 可执行文件,需要libgcc_s_dw2-1.dll库文件(在 Windows 平台的 Rust 二进制包中的bin目录)。[2]
当标准库(std)与 rustc 版本不匹配时会报错。一种绕过的方式是修改/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_metadata-*.so文件。步骤如下:
使用 objdump 反汇编,找到如下代码:
0000000000133a70 <csearch::cstore..CStore.CrateStore$LT$$u27$tcx$GT$::used_crate_source::hde93c04a1607e40dkel>:
...
13406d: e8 ee 6e ee ff callq 1af60 <memcmp@plt>
134072: 85 c0 test %eax,%eax
134074: 0f 95 c3 setne %bl
134077: 48 85 ed test %rbp,%rbp
13407a: 74 29 je 1340a5 <csearch::cstore..CStore.CrateStore$LT$$u27$tcx$GT$::used_crate_source::hde93c04a1607e40dkel+0x635>
13407c: 48 8b b4 24 a0 03 00 mov 0x3a0(%rsp),%rsi
134083: 00
134084: 48 85 f6 test %rsi,%rsi
134087: 74 1c je 1340a5 <csearch::cstore..CStore.CrateStore$LT$$u27$tcx$GT$::used_crate_source::hde93c04a1607e40dkel+0x635>
134089: 48 b8 1d 1d 1d 1d 1d movabs $0x1d1d1d1d1d1d1d1d,%rax
134090: 1d 1d 1d
134093: 48 39 c6 cmp %rax,%rsi
134096: 74 0d je 1340a5 <csearch::cstore..CStore.CrateStore$LT$$u27$tcx$GT$::used_crate_source::hde93c04a1607e40dkel+0x635>
134098: ba 01 00 00 00 mov $0x1,%edx
13409d: 48 89 ef mov %rbp,%rdi
1340a0: e8 4b 71 ee ff callq 1b1f0 <__rust_deallocate@plt>
1340a5: 84 db test %bl,%bl
1340a7: 0f 85 c4 18 00 00 jne 135971 <csearch::cstore..CStore.CrateStore$LT$$u27$tcx$GT$::used_crate_source::hde93c04a1607e40dkel+0x1f01>
将最后一行全部改成90(nop)。
参见
外部链接
网络资源
- Mirrors 新增 Rust Crates 源 | What's up, LUG Servers
- Docs.rs
- JSON to Rust Serde
- rust-toolstate | Records build and test status of external tools bundled with the Rust repository.
文档与教程
- Fearless Concurrency with Rust - The Rust Programming Language Blog
- String Types in Rust - Suspect Semantics
- Starting a new Rust project right, with error-chain
- 24 days of Rust - structured logging | siciarz.net (slog)
- Understanding Rust futures by going way too deep - fasterthanli.me
- Common Newbie Mistakes and Bad Practices in Rust: Bad Habits · Michael-F-Bryan
对比
- Using Rust for an Undergraduate OS Course (with C, Java, Python, D and Go)
- More casual hacking: Thoughts about Rust from D programmer (with D)
- A Quick Comparison of Nim vs. Rust (with Nim)
- Memory Management in Rust and Swift — Medium
- Migrating from Go to Rust | corrode Rust Consulting
跨语言调用
- Calling Rust from C (and Python!)
- Can rust library be used from another languages in a way c libraries do? - Stack Overflow
- aisamanra/rust-haskell-ffi: Toy example of calling Rust from Haskell
交叉编译
- Crosscompiling Rust to Arm
- Doc building for android · rust-lang/rust Wiki · GitHub
- [rust-dev] Rust (Servo) Cross-Compile to ARM
- 用 cargo-zigbuild 交叉编译 Rust 项目到 FreeBSD | Xerxes II's Blog
性能
其它
- 说说Phantom Type
- Contributing to the Rust compiler
- Rustlog : Why is a Rust executable large?
- Rust Optimization.md
- Ref patterns, destructuring, and invisible borrows – Robert Grosse – Medium
- Variance in Rust: An intuitive explanation – Ehsan's Blog