translated

This commit is contained in:
geekpi 2020-08-18 08:44:03 +08:00
parent 49058f471d
commit ecb7683654
2 changed files with 216 additions and 217 deletions

View File

@ -1,217 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Set up Vim as your Rust IDE)
[#]: via: (https://opensource.com/article/20/7/vim-rust-ide)
[#]: author: (Daniel Oh https://opensource.com/users/daniel-oh)
Set up Vim as your Rust IDE
======
The Vim text editor is a great development environment for programming
Rust applications.
![Ferris the crab under the sea, unofficial logo for Rust programming language][1]
The [Rust][2] programming language is designed to implement systems programming with safe concurrency and high memory performance in a way that feels familiar to C++ developers. It's also one of the most loved programming languages in [Stack Overflow's 2019 Developer Survey][3].
Text editors and [integrated development environment (IDE) tools][4] make writing Rust code easier and quicker. There are many editors to choose from, but I believe the [Vim editor][5] is a great fit for a Rust IDE. In this article, I'll explain how to set up Vim for Rust application development.
### Install Vim
Vim is one of the most commonly used command-line text editors in Linux and Unix. The latest version (as of this writing) is [8.2][6], and it offers more flexibility than ever in how you can use it.
[Vim's download page][7] provides multiple options to install it with binary or packages. For example, if you use macOS, you can install the [MacVim][8] project, then expand Vim's capabilities by [installing Vim plugins][9].
To set up Rust for development, download [Rustup][10], a handy Rust installer utility, and run the following in your terminal (if you use macOS, Linux, or any other Unix-like operating system):
```
`$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`
```
Choose an installation option from the interactive prompt. Then you will see output like:
```
stable installed - rustc 1.43.1 (8d69840ab 2020-05-04)
Rust is installed now. Great!
To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.
To configure your current shell run source $HOME/.cargo/env
```
### Syntax highlighting
Vim allows you to configure your runtime by defining it in the `.vimrc` file. To enable syntax highlighting, open your `.vimrc` file (or create one if it doesn't exist):
```
`$ vim ~/.vimrc`
```
Add the following in the `.vimrc` file and save it:
```
filetype plugin indent on
syntax on
```
The first line turns on the detection, plugin, and indent configurations all at once. The second line enables syntax highlighting. These features will help you manage your developer workflow in Rust. Learn more in Vim's [help file][11].
### Create a Rust application in Vim
To create a new Rust HelloWorld application (`hello.rs`) using Vim, enter:
```
`$ vim hello.rs`
```
Enter the following Rust code to print **Hello World!** in the console:
```
 fn main() {
      println!("Hello World");
 }
```
It should look something like this:
![Rust code with syntax highlighting][12]
(Daniel Oh, [CC BY-SA 4.0][13])
Here's what it would look like without syntax highlighting:
![Rust code without syntax highlighting][14]
(Daniel Oh, [CC BY-SA 4.0][13])
Did you notice how Vim automatically indented and organized the code? That is because of the first line you entered in the `.vimrc` file.
Great job! Next, you will build this application using Rust's package manager, [Cargo][15].
### Cargo integrations
Cargo makes creating applications easier. To see how, create a Cargo-based HelloWorld application. If you don't already have Cargo installed on your Linux or macOS system, enter:
```
`$ curl https://sh.rustup.rs -sSf | sh`
```
Then create a package with Cargo:
```
`$ cargo new my_hello_world`
```
If you look at the directory structure, you'll see Cargo automatically generated some source code and directories. If you have `tree` installed, run it to see the directory structure:
```
$ tree my_hello_world
my_hello_world
├── Cargo.toml
└── src
    └── main.rs
1 directory, 2 files
```
Open the `main.rs` source code file in Vim:
```
`$ vim my_hello_world/src/main.rs`
```
The code is the same as in the HelloWorld example you created manually above. Replace `World` with `Rust with Vim`:
```
 fn main() {
      println!("Hello, Rust with Vim");
 }
```
Use `:wq` to save your changes and quit Vim.
### Compile your application
Now you can compile your first Rust application using `cargo build`:
```
$ cd my_hello_world
$ cargo build
```
Your terminal output will look similar to this:
```
   Compiling my_hello_world v0.1.0 (/Users/danieloh/cloud-native-app-dev/rust/my_hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.60s
```
You may see a warning message because you reused the sample package name, `my_hello_world`, but you can ignore it for now.
Run the application:
```
$ target/debug/my_hello_world
Hello, Rust with Vim!
```
You can also use `cargo run` to build and run the application all at once:
```
$ cargo run
 
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/my_hello_world`
Hello, Rust with Vim!!
```
Congratulations! You set up Vim editor for Rust IDE on your local machine, developed your first Rust application, and built, tested, and ran it using the Cargo package manager tool. Run `cargo help` If you want to learn other Cargo commands.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/7/vim-rust-ide
作者:[Daniel Oh][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/daniel-oh
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rust_programming_crab_sea.png?itok=2eWLz8A5 (Ferris the crab under the sea, unofficial logo for Rust programming language)
[2]: https://www.rust-lang.org/
[3]: https://insights.stackoverflow.com/survey/2019#technology-_-most-loved-dreaded-and-wanted-languages
[4]: https://en.wikipedia.org/wiki/Integrated_development_environment
[5]: https://opensource.com/resources/what-vim
[6]: https://github.com/vim/vim
[7]: https://www.vim.org/download.php
[8]: https://github.com/macvim-dev/macvim
[9]: https://opensource.com/article/20/2/how-install-vim-plugins
[10]: https://rustup.rs/
[11]: http://vimdoc.sourceforge.net/htmldoc/filetype.html#:filetype-overview
[12]: https://opensource.com/sites/default/files/uploads/rust_helloworld.png (Rust code with syntax highlighting)
[13]: https://creativecommons.org/licenses/by-sa/4.0/
[14]: https://opensource.com/sites/default/files/uploads/rust_helloworld_no-syntax.png (Rust code without syntax highlighting)
[15]: https://opensource.com/article/20/3/rust-cargo

View File

@ -0,0 +1,216 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Set up Vim as your Rust IDE)
[#]: via: (https://opensource.com/article/20/7/vim-rust-ide)
[#]: author: (Daniel Oh https://opensource.com/users/daniel-oh)
将 Vim 设置为 Rust IDE
======
Vim 编辑器是很好的 Rust 应用开发环境。
![Ferris the crab under the sea, unofficial logo for Rust programming language][1]
[Rust][2] 语言旨在以 C++ 开发人员熟悉的方式实现具有安全并发性和高内存性能的系统编程。它也是 [Stack Overflow 的 2019 年开发人员调查][3]中最受欢迎的编程语言之一。
文本编辑器和[集成开发环境IDE工具][4]使编写 Rust 代码更加轻松快捷。有很多编辑器可供选择,但是我相信 [Vim 编辑器][5]非常适合 Rust IDE。在本文中我将说明如何为 Rust 应用开发设置 Vim。
### 安装 Vim
Vim 是 Linux 和 Unix 中最常用的命令行文本编辑器之一。最新版本(在编写本文时)是 [8.2][6],它在使用方式上提供了前所未有的灵活性。
[Vim 的下载页面][7]提供了多种二进制或软件包形式安装。例如,如果使用 macOS那么可以安装 [MacVim][8] 项目,然后通过[安装 Vim 插件][9] 扩展 Vim 的功能。
要设置 Rust 进行开发,请下载 [Rustup][10],这是一个方便的 Rust 安装器工具,并在你的终端上运行以下命令(如果你使用 macOS、Linux 或任何其他类 Unix 系统):
```
`$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`
```
在提示中选择安装选项。然后,你将看到如下输出:
```
stable installed - rustc 1.43.1 (8d69840ab 2020-05-04)
Rust is installed now. Great!
To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.
To configure your current shell run source $HOME/.cargo/env
```
### 语法高亮
Vim 能让你通过 `.vimrc` 文件配置你的运行时。要启用语法高亮,请打开 `.vimrc` 文件(如果不存在就创建一个):
```
`$ vim ~/.vimrc`
```
`.vimrc` 中添加以下内容并保存:
```
filetype plugin indent on
syntax on
```
第一行同时打开检测、插件和缩进配置。第二行启用语法高亮。这些功能将帮助你在 Rust 中管理开发流程。在 Vim 的[帮助文件][11]中了解更多信息。
### 在 Vim 中创建一个 Rust 应用
要使用 Vim 创建一个新的 Rust HelloWorld 应用(`hello.rs`),请输入:
```
`$ vim hello.rs`
```
输入以下 Rust 代码在控制台中打印 **Hello World!**
```
 fn main() {
      println!("Hello World");
 }
```
它看起来应该像这样:
![Rust code with syntax highlighting][12]
(Daniel Oh, [CC BY-SA 4.0][13])
没有语法高亮的样子如下:
![Rust code without syntax highlighting][14]
(Daniel Oh, [CC BY-SA 4.0][13])
你是否注意到 Vim 自动缩进和组织代码?那是因为你在 `.vimrc` 文件中输入了第一行。
很好!接下来,你将使用 Rust 的包管理器 [Cargo][15] 构建此应用。
### Cargo 集成
Cargo 使创建应用更加容易。要查看操作方法,请创建一个基于 Cargo 的 HelloWorld 应用。如果你尚未在 Linux 或 macOS 系统上安装 Cargo请输入
```
`$ curl https://sh.rustup.rs -sSf | sh`
```
然后使用 Cargo 创建包:
```
`$ cargo new my_hello_world`
```
如果查看目录结构,你会看到 Cargo 自动生成一些源码和目录。如果你安装了 `tree`,请运行它查看目录结构:
```
$ tree my_hello_world
my_hello_world
├── Cargo.toml
└── src
    └── main.rs
1 directory, 2 files
```
在 Vim 中打开 `main.rs` 源码文件:
```
`$ vim my_hello_world/src/main.rs`
```
它与你在上面手动创建的 HelloWorld 示例中的代码相同。用 `Rust with Vim` 代替 `World`
```
 fn main() {
      println!("Hello, Rust with Vim");
 }
```
使用 `:wq` 保存更改并退出 Vim。
### 编译你的应用
现在你可以使用 `cargo build` 编译你的第一个 Rust 应用:
```
$ cd my_hello_world
$ cargo build
```
你的终端输出将类似于以下内容:
```
   Compiling my_hello_world v0.1.0 (/Users/danieloh/cloud-native-app-dev/rust/my_hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.60s
```
你可能会看到一条警告消息,因为你重用了示例包名 `my_hello_world`,但现在可以忽略它。
运行应用:
```
$ target/debug/my_hello_world
Hello, Rust with Vim!
```
你也可以使用 `cargo run` 一次构建和运行应用:
```
$ cargo run
 
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/my_hello_world`
Hello, Rust with Vim!!
```
恭喜!你在本地的 VIm 编辑器中设置了 Rust IDE开发了第一个 Rust 应用,并使用 Cargo 包管理器工具构建、测试和运行了它。如果你想学习其他 Cargo 命令,请运行 `cargo help`
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/7/vim-rust-ide
作者:[Daniel Oh][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/daniel-oh
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rust_programming_crab_sea.png?itok=2eWLz8A5 (Ferris the crab under the sea, unofficial logo for Rust programming language)
[2]: https://www.rust-lang.org/
[3]: https://insights.stackoverflow.com/survey/2019#technology-_-most-loved-dreaded-and-wanted-languages
[4]: https://en.wikipedia.org/wiki/Integrated_development_environment
[5]: https://opensource.com/resources/what-vim
[6]: https://github.com/vim/vim
[7]: https://www.vim.org/download.php
[8]: https://github.com/macvim-dev/macvim
[9]: https://opensource.com/article/20/2/how-install-vim-plugins
[10]: https://rustup.rs/
[11]: http://vimdoc.sourceforge.net/htmldoc/filetype.html#:filetype-overview
[12]: https://opensource.com/sites/default/files/uploads/rust_helloworld.png (Rust code with syntax highlighting)
[13]: https://creativecommons.org/licenses/by-sa/4.0/
[14]: https://opensource.com/sites/default/files/uploads/rust_helloworld_no-syntax.png (Rust code without syntax highlighting)
[15]: https://opensource.com/article/20/3/rust-cargo