Rust搭建 Visual Studio Code 开发环境
下载完 Visual Studio Code 安装包之后启动安装向导安装(此步骤不在此赘述)。
安装完 Visual Studio Code (下文简称 VSCode)之后运行 VSCode。
在左边栏里找到 "Extensions",搜索 rls 扩展,安装之。
安装两个插件用于程序调试,1:Rust (rls) 2:CodeLLDB ,这两个直接搜索即可下载,安装完成后,F5开始调试,上方弹出的调试环境选择LLDB,再次F5弹出对话框,选择yes,然后.jison自动生成,再次F5调试即可成功。
重新启动 VSCode,Rust 的开发环境就搭建好了。
现在新建一个文件夹,如 rust。
在 VSCode 中打开新建的文件夹,打开文件夹之后选择菜单栏中的"终端"-"新建终端",会打开一个新的终端:
在终端中输入命令:
cargo new greeting
当前文件下下会构建一个名叫 greeting 的 Rust 工程目录。
在终端里输入以下三个命令:
cd ./greeting cargo build cargo run
系统在创建工程时会生成一个 Hello, world 源程序 main.rs,这时会被编译并运行:
GrantdeMacBook-Pro:rust grantbai$ ls greeting Cargo.toml src GrantdeMacBook-Pro:rust grantbai$ cd ./greeting GrantdeMacBook-Pro:greeting grantbai$ cargo build Compiling greeting v0.1.0 (/Users/grantbai/Documents/mystuff/projects/rust/greeting) Finished dev [unoptimized + debuginfo] target(s) in 4.67s GrantdeMacBook-Pro:greeting grantbai$ cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/greeting` Hello, world!
至此,你成功的构建了一个 Rust 命令行程序!
mac 上visual studio code 的launch.json配置, type 为lldb需安装codelldb插件。
{ "version": "0.2.0", "configurations": [ { "name": "(lldb) 启动", "type": "lldb", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "lldb" } ] }