如何从Visual Studio Code启动Rust应用程序? [英] How to launch a Rust application from Visual Studio Code?

查看:197
本文介绍了如何从Visual Studio Code启动Rust应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Rust安装了Visual Studio Code扩展:

I have installed the Visual Studio Code extensions for Rust:

我想运行我的项目,但我不知道在哪里单击.

I want to run my project and I don't understand where to click.

我尝试单击运行任务运行构建任务配置默认构建任务,但是没有任何合理的反应.

I tried clicking Run Task, Run build task, Configure Default build task, but nothing reasonable happens.

推荐答案

使用集成终端

运行集成终端的快捷方式:Ctrl +`
( Ctrl +反引号)
然后在集成终端中运行以下命令:

Using the integrated terminal

Shortcut to run the integrated terminal: Ctrl + `
(Ctrl + backtick)
Then run the following command in the integrated terminal:

cargo run

注意:从项目文件夹中打开代码编辑器(在项目文件夹终端中,或者在GUI模式下,在项目文件夹中单击code .命令:在项目文件夹中右键单击并选择Open With Code),然后按Ctrl +`(Ctrl +勾号) )以打开集成终端,然后输入:cargo run

Notes: Open the Code editor from your project folder ( code . command inside project folder terminal, or in GUI mode: right-click inside project folder and select Open With Code) then press Ctrl + ` ( Ctrl + backtick ) to open integrated terminal, then enter: cargo run

运行任务的快捷方式:Ctrl + Shift + B
添加cargo run作为默认任务:按如下所示将.vscode/tasks.json文件添加到您的项目中,要使用cargo run运行该项目,请按以下方式更改.vscode/tasks.json的内容:

Shortcut to run the Task: Ctrl + Shift + B
Add cargo run as a default Task: add .vscode/tasks.json file to your project as follows, to use cargo run to run the project, change the contents of .vscode/tasks.json as follows:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cargo run",
            "type": "shell",
            "command": "cargo",
            "args": [
                "run",
                // "--release",
                // "--",
                // "arg1"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

现在按Ctrl + Shift + B键运行任务,或按Ctrl + Shift + P并从命令面板中选择Tasks: Run Build Task.

Now press Ctrl + Shift + B to run the Task, or Press Ctrl + Shift + P and select Tasks: Run Build Task from the Command Palette.

您可以在上面添加诸如注释之类的参数,例如:"args": ["run", "--release", "--", "arg1"],(如果您的应用需要).

You may add arguments like the comment above e.g.: "args": ["run", "--release", "--", "arg1"], (if your app requires it).

(您可以使用Ctrl + Shift + P打开命令面板,然后键入Configure Default Build Task并按Enter进行选择.然后选择Rust: cargo buildOthers.这将在工作区中生成tasks.json文件.vscode文件夹).

(You may open the Command Palette with Ctrl + Shift + P and type in Configure Default Build Task and press Enter to select it. Then select Rust: cargo build or Others. This generates a tasks.json file in your workspace .vscode folder).

运行项目:
Ctrl + F5 或从Run菜单中选择Run Without Debugging,然后查看终端窗口,以获取结果:

To Run the project:
Press Ctrl+F5 or select Run Without Debugging from the Run menu, and see the terminal window, for the result:

第一次(仅一次),请基于LLDB安装本机调试器,或使用命令行进行安装:

For the first time (only once), install the Native debugger based on LLDB, or install using the command line:

code --install-extension vadimcn.vscode-lldb

然后在Visual Studio Code项目中:按下快捷键 Ctrl + F5 ,然后首次选择LLDB然后选择OKYes .vscode/launch.json文件,类似于以下示例,位于项目文件夹中(也可以从调试/运行"面板中选择create a launch.json file):

Then inside your Visual Studio Code project: Press shortcut Ctrl+F5 then for the first time select LLDB then OK and Yes, or create .vscode/launch.json file like the following sample, inside your project folder (Also you may select create a launch.json file from Debug/Run panel too):

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'example'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=example",
                    "--package=example"
                ],
                "filter": {
                    "name": "example",
                    "kind": "bin"
                }
            },
            "args": [
                // "user_arg1",
                // "user_arg2"
            ],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'example'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=example",
                    "--package=example"
                ],
                "filter": {
                    "name": "example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

注意:
我在上面将项目命名为example.
如果您需要args,则可以在// "user_arg1",以上取消注释.

Notes:
I named the project example above.
You may uncomment above // "user_arg1", if you need args.

安装:

rustup component add rust-src
code --install-extension matklad.rust-analyzer

运行代码,请点击fn main()上方的灰色Run文本:

To run the code click on the gray Run text above fn main():

安装扩展名,然后打开源文件,然后您将在右上角具有一个播放按钮,以单击,或使用默认快捷方式:Ctrl+Alt+N(您可以将快捷方式更改为: File>Preferences>Keyboard Shortcuts,然后在搜索框中输入code-runner.run.
注意:要在终端中运行命令,可以在File>Preferences>Settings中将code-runner.runInTerminal设置为true(或按Ctrl+,),然后在搜索框中输入code-runner.runInTerminal.
编辑:此操作仅运行打开的文件,例如:rustc main.rs.您可以编辑code-runner.executorMap来从以下命令更改命令:

Install the extension, then open the source file then you will have a play button in the top right corner to click, or use default shortcut: Ctrl+Alt+N (You may change the shortcut from: File>Preferences>Keyboard Shortcuts and enter code-runner.run in the search box).
Note: To run the command inside terminal You may set code-runner.runInTerminal to true from File>Preferences>Settings (or press Ctrl+,), then enter code-runner.runInTerminal in the search box.
Edit: This runs only open file e.g.: rustc main.rs. You may edit the code-runner.executorMap to change the command from:

"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",

收件人:

"rust": "cargo run",

因此,每次单击播放"按钮(或按键盘快捷键)时,代码运行器都会运行cargo run命令:
从菜单:File>Preferences>Settings(或按Ctrl+,),然后在搜索框中输入:
code-runner.executorMap然后单击Edit in Settings.json,然后将"code-runner.executorMap": and change "rust":"cd $dir && rustc $fileName && $dir$fileNameWithoutExt"编辑为"rust": "cargo run".

So the Code Runner runs the cargo run command each time you click the Play button (or pressing keyboard shortcut):
From menu: File>Preferences>Settings (or press Ctrl+,) then inside search box, enter:
code-runner.executorMap then click Edit in Settings.json then edit "code-runner.executorMap": and change "rust":"cd $dir && rustc $fileName && $dir$fileNameWithoutExt" to "rust": "cargo run".

或者只需在VSCode设置JSON(settings.json文件)中添加以下3行:

Or simply add 3 following lines to VSCode settings JSON (settings.json file):

"code-runner.executorMap": {
  "rust": "cargo run # $fileName"
}


使用Code Runner自定义命令

您可以将自定义命令设置为运行:"code-runner.customCommand": "cargo run"
菜单:File>Preferences>Settings(或按Ctrl+,),然后在搜索框中,输入customCommand并设置要运行的自定义命令:cargo run. 您可以将快捷方式更改为此命令以方便使用:从菜单中选择:File>Preferences>Keyboard Shortcuts,然后在搜索框中输入:customCommand,然后添加/更改键绑定,例如按下:Ctrl+L Ctrl+R


Using the Code Runner custom command

You may set the custom command to run: "code-runner.customCommand": "cargo run"
Menu: File>Preferences>Settings (or press Ctrl+,) then inside search box, enter customCommand and set the custom command to run: cargo run. You may change Shortcut to this command for ease of use: From Menu select: File>Preferences>Keyboard Shortcuts, then inside search box enter: customCommand, then add/change keybinding e.g. press: Ctrl+L Ctrl+R

您可以使用以下命令从命令行安装此扩展程序:

You may install this extension from the command line using:

code --install-extension rust-lang.rust

该插件使用任务:您可以按Ctrl + Shift + B然后选择显示的选项,目前只有两个选项:

The plugin uses tasks: You may press Ctrl + Shift + B then select options presented, for now, there are only two options:

cargo check
cargo build

因此,您需要使用上面提供的cargo run任务(tasks.json文件).

So you need to use the cargo run Task presented above (tasks.json file).

使用 Ctrl + P 安装并输入"ext install vscode-rust".使用 Ctrl + Shift + P 运行,键入"cargo",然后选择"Cargo:Run".

Install with Ctrl+P and type "ext install vscode-rust". Run with Ctrl+Shift+P, type "cargo" then select "Cargo:Run".

您可以在此命令中添加快捷方式以便于使用:
从菜单中选择:File>Preferences>Keyboard Shortcuts,然后在搜索框中输入:Cargo:Run,然后添加快捷键,例如按下:Ctrl+L Ctrl+R,如果您在非RLS模式下使用此扩展名在终端中运行货运命令:您可以在File>Preferences>Settings菜单中设置"rust.executeCargoCommandInTerminal": true(或按Ctrl+,),然后在搜索框中输入executeCargoCommandInTerminal

You may add Shortcut to this command for ease of use:
From Menu select: File>Preferences>Keyboard Shortcuts, then inside search box enter: Cargo:Run, then add keybinding e.g. press: Ctrl+L Ctrl+R, and if you are using this extension in non RLS mode to run Cargo command in terminal: you may set "rust.executeCargoCommandInTerminal": true in File>Preferences>Settings menu (or press Ctrl+,) then enter executeCargoCommandInTerminal inside search box.

这篇关于如何从Visual Studio Code启动Rust应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆