XCLUB-COOL STUFF AROUND YOU

 找回密码
 Register
查看: 67|回复: 2
打印 上一主题 下一主题

Rust for Python Developers: Environment Setup

[复制链接]

3469

主题

5万

帖子

5万

积分

Sapphire Diamond LV41

Daily Check-in2019sign star1sign star2sign star3sign star4post star1post star2post star3post star4post star5X'Club badge exclusive for IndiaXclub Kol

跳转到指定楼层
#1
发表于 2019-04-01 13:10:42 来自手机 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
Edited by Dinesh Vishwakarma at 2019-04-01 10:42

[p name="3661" id="3661"] [br]

[p name="3661" id="3661"][br]

[p name="3661" id="3661"]In my last article, I tried to make my[em][a href="https://medium.com/@rajasekar3eg/making-a-case-rust-for-python-developers-1a114e2d89f4" target="_blank"] [/a]case for learning rust for python developers[/em] or developers from other higher level programming languages. I have decided to contribute towards the goal of making Rust approachable for Python developers. I am writing a small series of Rust tutorials as time permits aiming at challenges I have faced while learning Rust and solutions I have found along the way.

[p name="53b3" id="53b3"]This is my first article in the series of articles about making Rust approachable for Python developers and developers from other high-level languages. Here, I am going to describe my typical environment setup for programming in Rust.

[p name="d5b3" id="d5b3"]I work exclusively on Linux environment. So I can’t suggest much for Windows and MacOS users. However, I guess most of these should be very similar in all these platforms.

[h3 name="154a" id="154a"][br][/h3][h3 name="b386" id="b386"]IDEs:[/h3][h4 name="f66a" id="f66a"]Vim + Tmux = love[/h4][p name="0471" id="0471"]I am most at home in Vim. Tmux and Vim satisfy most of my needs while working on a new project. Technically I use NeoVim. This is how my typical environment looks like:

[figure name="58e9" id="58e9"][div][div][/div][div][br][/div][div] [br][/div][div][br][/div][div][canvas width="75" height="41"][/canvas]NeoVim with Rust plugins[/div][/div][/figure][figure name="31c6" id="31c6"][div][div][/div][div][br][/div][div] [br][/div][div][br][/div][div][canvas width="75" height="41"][/canvas]Tmux + Vim -> My Typical Rust Environment[/div][/div][/figure][p name="4bfe" id="4bfe"]Rust has a language server called RLS. It integrates well enough with vim plugin “ale”. It provides some functionalities like jump to definition, formatting the code, code completion, basic refactoring,etc., It also checks your code for error as you write the code.

[p name="db8f" id="db8f"]Install the following cargo tools.

[pre name="00f3" id="00f3"]rustup component add rustfmt[/pre][pre name="7042" id="7042"][div class="blockcode"][div id="code_jY1"][ol][li]rustup component add rls rust-analysis rust-src[/li][/ol][/div][em onclick="copycode($('code_jY1'));"]复制代码[/em][/div][/pre][pre name="6106" id="6106"]cargo +nightly install racer[/pre][p name="fcd3" id="fcd3"]If you are using Vundle plug-in manager, adding the following to your [em]vimrc[/em]or [em]nvim/init.vim[/em] will setup the Rust environment for Vim/NeoVim.

[pre name="0149" id="0149"]Plugin 'racer-rust/vim-racer'[br]Plugin 'rust-lang/rust.vim'[br]Plugin 'w0rp/ale'[/pre][pre name="0a26" id="0a26"]" change the bin path as your system settings[/pre][pre name="a88a" id="a88a"]let g:racer_cmd = "/home/raja/.cargo/bin/racer" [br]let g:racer_experimental_completer = 1[/pre][pre name="11fd" id="11fd"]let g:ale_python_auto_pipenv=1[br]let b:ale_linters = {[br] \ ‘python’: <‘pylint’>,[br] \ ‘rust’: <‘rustup’, ‘run’, ‘nightly’, ‘rls’>,[br] \ ‘go’: <‘go-langserver’> }[/pre][h4 name="c714" id="c714"][strong]Clippy:[/strong][/h4][p name="b45c" id="b45c"]Clippy is Rust linter. While you are in learning phase, Clippy is extremely helpful. It identifies common mistakes and provides suggestions for modifying your code.

[p name="ca5a" id="ca5a"]Install clippy using the following command:

[pre name="561e" id="561e"][div class="blockcode"][div id="code_zv9"][ol][li]rustup component add clippy[/li][/ol][/div][em onclick="copycode($('code_zv9'));"]复制代码[/em][/div][/pre][p name="90c7" id="90c7"]You can run Clippy inside your project folder as follows:

[pre name="45dc" id="45dc"][div class="blockcode"][div id="code_p5F"][ol][li]cargo clippy[/li][/ol][/div][em onclick="copycode($('code_p5F'));"]复制代码[/em][/div][/pre][p name="b6e2" id="b6e2"]You can find all the Clippy lints here.

[p name="020c" id="020c"]While this is enough for me most of the times, most developers prefer to use IDEs. There are good reasons for that. When projects become big enough, IDEs become more convenient when working with many developers. Vim + RLS can at sometimes become unreliable and buggy as the project size grows. Refactoring is also another major hindrance as far as I have observed in Vim.

[h4 name="9586" id="9586"]VS-Code:[/h4][p name="a3f9" id="a3f9"]While technically not an IDE, it generally provides a good experience for me. It also utilizes rust-rls to provide typical IDE experience. So it can also be buggy sometimes.

[p name="5b8f" id="5b8f"]All you have to do is to install official Rust[a href="https://marketplace.visualstudio.com/items?itemName=rust-lang.rust" rel="nofollow noopener" target="_blank"] ([/a]rls) plugin. You are good to go. VSCode also provides debugging functionality using CodeLLDB.

[p name="3230" id="3230"]To debug Rust programs, install the following extensions.

[pre name="cd20" id="cd20"]CodeLLDB[/pre][pre name="bcb9" id="bcb9"]C/C++[a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools" rel="nofollow noopener" target="_blank"] [/a]extension[/pre][p name="9316" id="9316"]Change the [em]program field[/em] in [em]launch.json[/em] to your debug binary as follows.

[pre name="b8fd" id="b8fd"][br]"configurations": <[br]        {[br]            "name": "(gdb) Launch",[br]            "type": "lldb",[br]            "request": "launch",[br]            "program": "[strong]path_to_your_project/target/debug/project_name[/strong]",[br]            "args": <>,[br]            "sourceLanguages": <"rust">,[br]            "cwd": "${workspaceFolder}",[br]            "environment": <>,[br]            "externalConsole": true,[br]                    "description": "Enable pretty-printing for gdb",[br]                    "text": "-enable-pretty-printing",[br]                    "ignoreFailures": true[br]                }[br]            >[br]        }[br]    >[/pre][figure name="7267" id="7267"][div][/div][div][br][/div][div][br][/div][div][canvas width="75" height="41"][/canvas] Rust in VSCode[br][/div][/figure][h4 name="9cc1" id="9cc1"]IntelliJ Rust:[/h4][p name="6798" id="6798"]This is by far the most reliable IDE for Rust based on my experience. Code completion, jump to definition, refactoring, on the fly error analysis,etc., all work very well and it’s butter smooth. However, debugging is available only on CLion(paid version). You have to install Rust-plugin and it just works.

[figure name="6ca0" id="6ca0"][div][div][/div][div][br][/div][div] [br][/div][div][canvas width="75" height="41"][/canvas]IntelliJ Rust[/div][/div][/figure][p name="a560" id="a560"]So overall, for a relatively small personal project, I tend to use Vim mostly. For large projects, I tend to use IntelliJ Rust while developing and use VSCode while debugging.

[h3 name="d9aa" id="d9aa"][br][/h3]
回复

使用道具 举报

4076

主题

2万

帖子

2万

积分

Jade Diamond LV32

Diamondsign star1sign star2post star1post star2sign star3post star3post star4post star5sign star4X'Club badge exclusive for India

#2
发表于 2019-04-01 14:11:07 来自手机 | 只看该作者
Good brother
回复

使用道具 举报

3469

主题

5万

帖子

5万

积分

Sapphire Diamond LV41

Daily Check-in2019sign star1sign star2sign star3sign star4post star1post star2post star3post star4post star5X'Club badge exclusive for IndiaXclub Kol

#3
 楼主| 发表于 2019-04-01 14:34:52 来自手机 | 只看该作者
Thanks brother!
回复

使用道具 举报

高级模式
B Color Link Quote Code Smilies |上传

本版积分规则

Infinix Official Website|Infinix official mall|infinix Note 4|XCLUB-COOL STUFF AROUND YOU

GMT+8, 2025-01-13 17:28 , Processed in 0.036210 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表