hello rust
Hello Rust in MacOS.
Install rust
run curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
to install rust tools.
Hello world
cargo new greeting
cargo new hello-world
cd greeting
cargo run
VSCode Debug
- install
rust-analyzer
- install CodeLLDB
- or download it form marketplace and install it locally with
code --install-extension b6790052-5574-45be-98b7-11426ff972d0.vsix
- or download it form marketplace and install it locally with
- create a lunch.json file
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Cargo launch",
"cargo": {
"args": [
"build"
]
},
"args": []
}
]
}
CLI Debug
$ cargo build
$ lldb ./target/debug/greeting
(lldb) breakpoint set -file main.rs -line 3
to set the breakpoint(lldb) run
to run program(lldb) frame variable
to show variables(lldb) thread step-over
to step over(lldb) thread continue
to continue running(lldb) exit
to exit program
More information you can see here: tutorial
History details:
➜ greeting git:(master) ✗ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
➜ greeting git:(master) ✗ lldb ./target/debug/greeting
(lldb) target create "./target/debug/greeting"
Current executable set to '/Users/zhoukeke/Downloads/rust/greeting/target/debug/greeting' (x86_64).
(lldb) breakpoint set -file main.rs -line 3
Breakpoint 1: where = greeting`greeting::main::h32c37006812263d7 + 32 at main.rs:3:5, address = 0x0000000100004d90
(lldb) run
Process 24395 launched: '/Users/zhoukeke/Downloads/rust/greeting/target/debug/greeting' (x86_64)
Process 24395 stopped
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100004d90 greeting`greeting::main::h32c37006812263d7 at main.rs:3:5
1 fn main() {
2 let mut name = String::from("pipe");
-> 3 name.push_str(" zkk");
4 println!("Hello, {}!", name);
5 }
Target 0: (greeting) stopped.
(lldb) frame variable
(alloc::string::String) name = {
vec = {
buf = {
ptr = {
pointer = (pointer = "pipe")
_marker = {}
}
cap = 4
alloc = {}
}
len = 4
}
}
(lldb) thread step-over
Process 24395 stopped
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x0000000100004dc4 greeting`greeting::main::h32c37006812263d7 at main.rs:4:5
1 fn main() {
2 let mut name = String::from("pipe");
3 name.push_str(" zkk");
-> 4 println!("Hello, {}!", name);
5 }
Target 0: (greeting) stopped.
(lldb) frame variable
(alloc::string::String) name = {
vec = {
buf = {
ptr = {
pointer = (pointer = "pipe zkk@\U00000004\U00000004\U00000010")
_marker = {}
}
cap = 8
alloc = {}
}
len = 8
}
}
(lldb) thread continue
Resuming thread 0xad6e3 in process 24395
Process 24395 resuming
Hello, pipe zkk!
Process 24395 exited with status = 0 (0x00000000)
(lldb) exit
➜ greeting git:(master) ✗