rust学习
Rust (github)
1. install (https://rustup.rs/)
2. play on line
curl https://sh.rustup.rs -sSf | sh echo 'PATH="$PATH:$HOME/.cargo/bin"' >> ~/.bashrc
rustup doc
https://www.jdoodle.com/execute-rust-online
https://www.tutorialspoint.com/compile_rust_online.php
Tutorial org learn (entry)
1. org examaple (CN)
# install book local git clone https://github.com/rust-lang-cn/rust-by-example-cn cd rust-by-example-cn cargo install mdbook mdbook build mdbook serve
grammar
2. book (Translations/CN) (Chinese)
rustup docs --book
4. Rust 基础笔记
5. view a project
cargo doc --open
6. Rust 语言中文版
7. Rust教程
8. Rust 编程语言入门
Grow with Rust
Comprehensive guide to the Rust standard library APIs.
Guide to the Rust editions.
A book on Rust’s package manager and build system.
Learn how to make awesome documentation for your crate.
Familiarize yourself with the knobs available in the Rust compiler.
In-depth explanations of the errors you may see from the Rust compiler.
Build your skills in an application domain
Learn how to build effective command line applications in Rust.
Use Rust to build browser-native libraries through WebAssembly.
Become proficient with Rust for Microcontrollers and other embedded systems.
Master Rust
The Rustonomicon is your guidebook to the dark arts of unsafe Rust. It’s also sometimes called “the ’nomicon.”
READ THE ’NOMICONThe Unstable Book has documentation for unstable features that you can only use with nightly Rust.
READ THE UNSTABLE BOOKrust web framework comparison
rust python example
awesome
awesome rust (CN)
awesome c(CN)
awesome python
debugger
cargo install debug-here-gdb-wrapper # if you are on linux cd debug-here/debug-me echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope cargo run
# In Cargo.toml. [dependencies] debug-here = "0.2"
# in source file. Now it looks like this: #[macro_use] extern crate debug_here; fn factorial(n: usize) -> usize { let mut res = 1; debug_here!(); 1 }
cat ~/.Xresources
config as follow:
1 ! Use a nice truetype font and size by default... 2 xterm*faceName: DejaVu Sans Mono Book 3 xterm*faceSize: 11 4 5 ! Every shell is a login shell by default (for inclusion of all necessary environment variables) 6 xterm*loginshell: true 7 8 ! I like a LOT of scrollback... 9 xterm*savelines: 16384 10 11 ! double-click to select whole URLs :D 12 xterm*charClass: 33:48,36-47:48,58-59:48,61:48,63-64:48,95:48,126:48 13 14 ! DOS-box colours... 15 xterm*foreground: rgb:a8/a8/a8 16 xterm*background: rgb:00/00/00 17 xterm*color0: rgb:00/00/00 18 xterm*color1: rgb:a8/00/00 19 xterm*color2: rgb:00/a8/00 20 xterm*color3: rgb:a8/54/00 21 xterm*color4: rgb:00/00/a8 22 xterm*color5: rgb:a8/00/a8 23 xterm*color6: rgb:00/a8/a8 24 xterm*color7: rgb:a8/a8/a8 25 xterm*color8: rgb:54/54/54 26 xterm*color9: rgb:fc/54/54 27 xterm*color10: rgb:54/fc/54 28 xterm*color11: rgb:fc/fc/54 29 xterm*color12: rgb:54/54/fc 30 xterm*color13: rgb:fc/54/fc 31 xterm*color14: rgb:54/fc/fc 32 xterm*color15: rgb:fc/fc/fc 33 34 ! right hand side scrollbar... 35 xterm*rightScrollBar: true 36 xterm*ScrollBar: true 37 38 ! stop output to terminal from jumping down to bottom of scroll again 39 xterm*scrollTtyOutput: false
Tell your X server to incorporate those tweak
xrdb -merge ~/.Xresources
debug test case
How do I debug a failing cargo test in gdb?
Ask GDB to list all functions in a program
info functions info functions regexp
16 Examining the Symbol Table (16 Examining the Symbol Table.)
rust-gdb /vagrant/target/debug/deps/libzfs-37d0dad38d98d030 b lib.rs:533 run --test
How do I debug a failing cargo test in GDB?
debug info
1 $ cargo test --lib 2 Finished dev [unoptimized + debuginfo] target(s) in 0.03s 3 Running target/debug/deps/hello_cargo-b786631aa108fec0 4 5 running 3 tests 6 test tests::it_works ... ok 7 test tests::test_add ... ok 8 test tests::test_bad_add ... FAILED 9 10 failures: 11 12 ---- tests::test_bad_add stdout ---- 13 thread 'tests::test_bad_add' panicked at 'assertion failed: `(left == right)` 14 left: `-1`, 15 right: `3`', src/lib.rs:32:9 16 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace. 17 18 19 failures: 20 tests::test_bad_add 21 22 test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out 23 24 error: test failed, to rerun pass '--lib' 25 26 27 $ rust-gdb target/debug/deps/hello_cargo-b786631aa108fec0 28 GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 29 Copyright (C) 2016 Free Software Foundation, Inc. 30 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 31 This is free software: you are free to change and redistribute it. 32 There is NO WARRANTY, to the extent permitted by law. Type "show copying" 33 and "show warranty" for details. 34 This GDB was configured as "x86_64-linux-gnu". 35 Type "show configuration" for configuration details. 36 For bug reporting instructions, please see: 37 <http://www.gnu.org/software/gdb/bugs/>. 38 Find the GDB manual and other documentation resources online at: 39 <http://www.gnu.org/software/gdb/documentation/>. 40 For help, type "help". 41 Type "apropos word" to search for commands related to "word"... 42 Reading symbols from target/debug/deps/hello_cargo-b786631aa108fec0...done. 43 (gdb) info functions test_bad_add 44 All functions matching regular expression "test_bad_add": 45 46 File src/lib.rs: 47 static void hello_cargo::tests::test_bad_add::_$u7b$$u7b$closure$u7d$$u7d$::hacffd70b508c276a(struct closure *); 48 static void hello_cargo::tests::test_bad_add::h938c2acc2927d730(void); 49 (gdb) b hello_cargo::tests::test_bad_add::h938c2acc2927d730 50 Breakpoint 1 at 0x139f7: file src/lib.rs, line 32. 51 (gdb) run --test test_bad_add 52 Starting program: /home/shhfeng/test/hello_cargo/target/debug/deps/hello_cargo-b786631aa108fec0 --test test_bad_add 53 [Thread debugging using libthread_db enabled] 54 Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". 55 56 running 1 test 57 [New Thread 0x7ffff6ec4700 (LWP 22620)] 58 [Switching to Thread 0x7ffff6ec4700 (LWP 22620)] 59 60 Thread 2 "tests::test_bad" hit Breakpoint 1, hello_cargo::tests::test_bad_add::h938c2acc2927d730 () at src/lib.rs:32 61 32 assert_eq!(bad_add(1, 2), 3); 62 (gdb) list 63 27 } 64 28 65 29 #[test] 66 30 fn test_bad_add() { 67 31 // 这个断言会导致测试失败。注意私有的函数也可以被测试! 68 32 assert_eq!(bad_add(1, 2), 3); 69 33 } 70 34 } 71 (gdb) s 72 hello_cargo::bad_add::h460b365704f64b13 (a=1, b=2) at src/lib.rs:13
debug detail
1 (gdb) bt 2 #0 hello_cargo::bad_add::h460b365704f64b13 (a=1, b=2) at src/lib.rs:13 3 #1 0x0000555555567a06 in hello_cargo::tests::test_bad_add::h938c2acc2927d730 () at src/lib.rs:32 4 #2 0x00005555555676ea in hello_cargo::tests::test_bad_add::_$u7b$$u7b$closure$u7d$$u7d$::hacffd70b508c276a () at src/lib.rs:30 5 #3 0x000055555556754e in core::ops::function::FnOnce::call_once::h859d77482d101f3d () 6 at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libcore/ops/function.rs:231 7 #4 0x000055555557005f in {{closure}} () at src/libtest/lib.rs:1497 8 #5 call_once<closure,()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libcore/ops/function.rs:231 9 #6 call_box<(),closure> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/liballoc/boxed.rs:749 10 #7 0x00005555555b3fda in __rust_maybe_catch_panic () at src/libpanic_unwind/lib.rs:87 11 #8 0x000055555558d168 in try<(),std::panic::AssertUnwindSafe<alloc::boxed::Box<FnBox<()>>>> () 12 at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/panicking.rs:276 13 #9 catch_unwind<std::panic::AssertUnwindSafe<alloc::boxed::Box<FnBox<()>>>,()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/panic.rs:388 14 #10 {{closure}} () at src/libtest/lib.rs:1452 15 #11 0x00005555555688a5 in std::sys_common::backtrace::__rust_begin_short_backtrace::h7988d0d59efc7a12 () 16 at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/sys_common/backtrace.rs:135 17 #12 0x0000555555569045 in {{closure}}<closure,()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/thread/mod.rs:469 18 #13 call_once<(),closure> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/panic.rs:309 19 #14 do_call<std::panic::AssertUnwindSafe<closure>,()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/panicking.rs:297 20 #15 0x00005555555b3fda in __rust_maybe_catch_panic () at src/libpanic_unwind/lib.rs:87 21 #16 0x000055555557018d in try<(),std::panic::AssertUnwindSafe<closure>> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/panicking.rs:276 22 #17 catch_unwind<std::panic::AssertUnwindSafe<closure>,()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/panic.rs:388 23 #18 {{closure}}<closure,()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libstd/thread/mod.rs:468 24 #19 call_box<(),closure> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/liballoc/boxed.rs:749 25 #20 0x00005555555b371e in call_once<(),()> () at /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/liballoc/boxed.rs:759 26 #21 start_thread () at src/libstd/sys_common/thread.rs:14 27 #22 thread_start () at src/libstd/sys/unix/thread.rs:81 28 #23 0x00007ffff77b56ba in start_thread (arg=0x7ffff6ec4700) at pthread_create.c:333 29 #24 0x00007ffff72d541d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
Debugging Rust code with vim and Conque-GDB
# install Vundle firstl list then vimproc.vim git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim Plugin 'vim-scripts/Conque-GDB' # in vim, :PluginInstall , :ConqueGdbExe rust-gdb , :ConqueGdb target/debug/$name
BKM:
How can I integrate gdb with Vim?
Using ConqueGDB to debug ARM microcontroller with OpenOCD
vi/vim使用进阶: 在VIM中使用GDB调试 – 使用vimgdb (vimgdb)
rustc -g main.rs --emit="obj,link" gdb main # created two aliases for my bash to make things simple: alias rd='rustc -g --emit="obj,link"' compile_and_run() { rustc -g --emit="obj,link" $1 && gdb ${1%.*} } alias rdr=compile_and_run
REPL
# Link failure: cannot find -lncursesw https://github.com/murarth/rusti/issues/92 sudo apt-get install libncursesw5-dev # completion https://github.com/racer-rust/racer#installation rustup toolchain add nightly cargo +nightly install racer export PATH="$PATH:$HOME/.cargo/bin" rustup install nightly-2016-08-01 rustup run nightly-2016-08-01 cargo install --git https://github.com/murarth/rusti # https://github.com/murarth/rusti/issues/90 error while loading shared libraries: librustc_driver-c8005792.so rustup run nightly-2016-08-01 ~/.cargo/bin/rusti rustup run nightly-2016-08-01 target/debug/rusti
IDE
edition
Rustup for managing Rust versions
Tips
1. 调试宏代码
rustc --pretty expanded
2. Rust进阶之条件编译
#[cfg(foo)] #[cfg(bar = "baz")] #[cfg(any(unix, windows))] #[cfg(all(unix, target_pointer_width = "32"))] #[cfg(not(foo))]
Gramma
1. PhantomData
#![allow(unused)] use std::marker::PhantomData; use std::mem; fn main() { #[allow(dead_code)] struct Slice<'a, T: 'a> { start: *const T, end: *const T, phantom: PhantomData<&'a T>, } struct Slice1<T:> { start: *const T, end: *const T, } println!("{:?}", mem::size_of::<Slice<i32>>()); println!("{:?}", mem::size_of::<Slice1<i32>>()); } // output: // 16 // 16
2. Vec.with_capacity
3. typyof by type_name
base type of rust from Reference Doc
#![feature(core_intrinsics)] use std::mem; fn print_type_of<T>(_: &T) { println!("{}", unsafe { std::intrinsics::type_name::<T>() }); } fn main() { print_type_of(&32.90); // prints "f64" print_type_of(&vec![1, 2, 4]); // prints "std::vec::Vec<i32>" print_type_of(&"foo"); // prints "&str" let ln = mem::size_of::<i32>() as u64; print_type_of(&ln) // prints "u64" }
4. 错误处理(match, let and ? 用法)
Result is a good example.
5. 闭包
6. Option
简易教程:
极客学院: