外贸seo网站建站,wordpress pdf 显示,个人做淘宝客网站要备案,中国佛山手机网站建设一、简介
本文介绍了如何使用vscode编写rust#xff0c;实现打印Hello, world!的程序。
二、工具安装
0. 环境介绍#xff1a;
Linux #xff08;或者windowswsl#xff09;
1. 安装rust编译器rustc和包管理器cargo。
请参考连接#xff1a;Rust 程序设…一、简介
本文介绍了如何使用vscode编写rust实现打印Hello, world!的程序。
二、工具安装
0. 环境介绍
Linux 或者windowswsl
1. 安装rust编译器rustc和包管理器cargo。
请参考连接Rust 程序设计语言 简体中文版-安装。 rust编译器安装完成后会自动安装cargo。cargo是Rust的包管理器可以用来方便的进行包管理、编译运行rust程序。 可以使用以下命令查看是否成功安装rust编译器和cargo。 shell rustc -V shell cargo -V
2. vscode插件安装
安装rust-analyzer和CodeLLDB用于debug。
三、实现helloworld
接下来将使用cargo进行管理我们的程序尽管程序只打印“Hello, world!”一句话。
1. 新建hello_world工程
cargo new hello_world此时目录下自动生成一个名字为hello_world/的文件夹。
2. 使用vscode打开工程目录文件夹
使用vscode打开hello_world/文件夹文件结构如下。
3. 编写main.rs文件
编辑main.rs文件main.rs文件如下
fn main() {println!(Hello, world!);
}4. 编译程序
编译我们的hello_world工程。在hello_world/目录下运行以下命令。
cargo build输出结果如下所示 编译器的输出表示程序hello_world已经编译成功耗时0.30s。
5. 运行程序
使用以下命令运行程序。
cargo run若运行成功运行结果如下 可以看到程序成功输出了Hello, world!字符。
6. 进行debug
在vscode中对rust程序进行debug需要安装前面提到的CodeLLDB插件。首先在vscode的设置中设置Debug: Allow Breakpoints Everywhere。如下所示
将main.rs文件内的代码修改为如下
fn main() {let _a 3.0;let _b _a * _a;println!(_b:{}, _b);
}假设我们在第4行处打断点如下图所示 然后使用LLDB生成launch.json文件。按下ctrlshiftp打开vscode的命令输出栏目输入 LLDB: Generate launch Configurations from Cargo.toml如下图所示 按下回车后LLDB会根据Cargo.toml文件自动生成launch.json文件内容此时文件名还是Untitled-1。我们需要将改文件保存到.vscode/目录下并命名为launch.json如果没有.vscode/文件夹先手动在hello_world/目录下创建.vscode/文件夹。LLDB自动生成的launch.json文件内容如下
{version: 0.2.0,configurations: [{type: lldb,request: launch,name: Debug executable hello_world,cargo: {args: [build,--binhello_world,--packagehello_world],filter: {name: hello_world,kind: bin}},args: [],cwd: ${workspaceFolder}},{type: lldb,request: launch,name: Debug unit tests in executable hello_world,cargo: {args: [test,--no-run,--binhello_world,--packagehello_world],filter: {name: hello_world,kind: bin}},args: [],cwd: ${workspaceFolder}}]
}现在hello_world/目录下的文件结构如下 之后点击vscode的状态栏的Debug excutabel hello_world (hello_world)图标并选择Debug executable hello_world即可进行debug。 即下图红框中的图标 可以看到程序成功运行到我们的断点处并在左侧窗口中显示了中间变量的值。如下图所示
四、参考
[1]. Rust in Visual Studio Code [2]. Hello, Cargo!