1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Rust 勸退系列 01:打造開發(fā)環(huán)境

        共 6562字,需瀏覽 14分鐘

         ·

        2021-04-10 14:07

        閱讀本文大概需要 7 分鐘。

        大家好,我是站長 polarisxu。

        今天起正式 Rust “勸退”之旅。我也是在學習中,大家一起探討交流,看誰先被勸退。在這個系列教程中,我通過和 Go 對比學習,同時只講 Rust 最常用的特性,盡量避免入門就勸退。

        工欲善其事必先利其器!本文我們先打造一個 Rust 的開發(fā)環(huán)境。

        01 安裝 Rust

        打開 Rust 官網(wǎng):https://rust-lang.org,可以切換到簡體中文界面。在頂部菜單有「安裝」。

        對于類 Unix 系統(tǒng)(MacOS、Linux 等),官方推薦下載 Rustup 并安裝 Rust(如果是 Windows,但使用 Linux 子系統(tǒng),即 WSL,安裝方式和 Linux 一樣),在終端執(zhí)行如下命令進行安裝:

        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

        如果是 Windows 用戶,會讓你下載預編譯好的 RUSTUP-INIT.exe 文件(32 位或 64 位),一步步操作即可。

        安裝完成后,在終端執(zhí)行 rustup --version,看到了版本信息即表示安裝成功。

        也可以 https://rustup.rs 網(wǎng)站可以看到查看 rustup:

        rustup 是什么

        在終端執(zhí)行 rustup,看到類似如下信息:

        $ rustup 
        rustup 1.23.1 (3df2264a9 2020-11-30)
        The Rust toolchain installer
        ...

        這是一個 Rust 工具鏈安裝器,專門用于安裝 Rust,管理 Rust 很方便:安裝、升級、卸載等,還可以切換版本,包括 nightly,beta 和 stable。

        相比較而言,Go 語言的安裝、卸載、更新,多版本管理就麻煩很多,所以 Go 社區(qū)出現(xiàn)了好幾個 Go 自身版本管理的工具,比如我之前介紹的 goup。(我之前寫過一篇文章:《Go 官方應(yīng)該搞一個類似 Rustup 的管理工具》

        Go 的安裝,如果不用第三方工具,一般通過下載對應(yīng)操作系統(tǒng)的預編譯版本,然后跟普通軟件一樣安裝即可。也可以通過對應(yīng)系統(tǒng)的工具進行安裝,比如 Mac 下的 homebrew,不過很可能安裝的不是最新版本的 Go。此外,還可以通過源碼安裝 Go。

        一般地,為了省事,我推薦使用類似 goup 這樣的工具來安裝、管理 Go 版本,就跟 Rust 自帶的 rustup 工具一樣。

        回到 rustup 命令,看看幫助文檔:

        $ rustup
        rustup 1.23.1 (3df2264a9 2020-11-30)
        The Rust toolchain installer

        USAGE:
            rustup [FLAGS] [+toolchain] <SUBCOMMAND>

        FLAGS:
            -v, --verbose    Enable verbose output
            -q, --quiet      Disable progress output
            -h, --help       Prints help information
            -V, --version    Prints version information

        ARGS:
            <+toolchain>    release channel (e.g. +stable) or custom toolchain to set override

        SUBCOMMANDS:
            show           Show the active and installed toolchains or profiles
            update         Update Rust toolchains and rustup
            check          Check for updates to Rust toolchains
            default        Set the default toolchain
            toolchain      Modify or query the installed toolchains
            target         Modify a toolchain's supported targets
            component      Modify a toolchain'
        s installed components
            override       Modify directory toolchain overrides
            run            Run a command with an environment configured for a given toolchain
            which          Display which binary will be run for a given command
            doc            Open the documentation for the current toolchain
            man            View the man page for a given command
            self           Modify the rustup installation
            set            Alter rustup settings
            completions    Generate tab-completion scripts for your shell
            help           Prints this message or the help of the given subcommand(s)

        DISCUSSION:
            rustup installs The Rust Programming Language from the official
            release channels, enabling you to easily switch between stable,
            beta, and nightly compilers and keep them updated. It makes
            cross-compiling simpler with binary builds of the standard library
            for common platforms.

            If you are new to Rust consider running `rustup doc --book` to
            learn Rust.

        其中的具體選項和子命令不一一介紹,大概有一個印象即可,知道 rustup 是干嘛的后,有相應(yīng)需求時,可以看看這個文檔應(yīng)該就知道怎么處理了。

        值得一提的時候,安裝 Rust 后,很多文檔都在本地有一份,你可以通過 rustup doc 查看。

        02 編輯器

        安裝了 Rust,接下來需要搞定 Rust 的編輯器。目前還沒有專門為 Rust 開發(fā)的 IDE,但主流的文本編輯器都有插件支持 Rust 開發(fā)。得益于微軟發(fā)起的(很大程度因為 VSCode 的存在) Language Server Protocol,讓文本編輯器擁有很多 IDE 的特性。(關(guān)于 LSP 相關(guān)知識可以閱讀我之前寫的文章。)

        (官方列表中沒有直接列出 CLion,這是 JetBrains 專門為 C/C++ 打造的 IDE。據(jù)說很多 Rust 喜歡基于它,安裝 Rust 插件進行開發(fā))。

        因為寫 Go 一直使用 VSCode,因此寫 Rust 也使用它了。

        在 VSCode 中搜索 Rust,排在前面的兩個插件:

        • Rust:這是官方開發(fā)的;
        • rust-analyzer:這是社區(qū)開發(fā)的;

        官方的 Rust 插件截圖:

        社區(qū)的 rust-analyzer 截圖:

        官方的 4 星,社區(qū)的 5 星。

        一般我們都會安裝官方的。但我用了后,發(fā)現(xiàn)官方的有些功能缺失,比如沒法在 VSCode 中直接運行 Rust。于是我換成社區(qū)的,發(fā)現(xiàn)可以。查了一些資料,目前看社區(qū)版本確實比官方的好。因此目前建議大家安裝社區(qū)版的 rust-analyzer。該插件的完整手冊地址:https://rust-analyzer.github.io/manual.html。(如果兩個都安裝了,記得把官方的 disabled,否則很可能沖突)

        安裝 rust-analyzer 后,如果提示你安裝其他組件,按提示安裝即可。

        反觀 Golang 開發(fā)工具,JetBrains 為其定制了 GoLand IDE,這一定程度也說明 Go 的歡迎程度,期待其為 Rust 定制 IDE 的那一天。而 Go 的 VSCode 插件:vscode-go,最開始并不是 Go 官方開發(fā)的,而是 VSCode 團隊開發(fā)的,只是后來轉(zhuǎn)給了 Go 官方維護。

        和 Rust 插件不同的是,Go 安裝完 VSCode 插件后,會提示用戶安裝一堆工具,因為很多的功能是通過一堆工具組裝的。在安裝這些工具過程中,很可能會安裝失敗,幸好有了 GOPROXY,讓安裝更順暢。關(guān)于 VSCode 打造 Go 開發(fā)環(huán)境,可以參考我之前寫的文章:《VSCode 開發(fā) Go 程序也可以和 GoLand 一樣強大》。

        03 小結(jié)

        環(huán)境是第一步,打造一個好的環(huán)境,對后面的學習會有極大的幫助。

        在本節(jié),安裝完 Rust 后,我主要介紹了 rustup 工具,關(guān)于 Rust 工具鏈的其他工具,后面用到時再一一介紹。

        準備好了嗎?Rust “勸退”之旅,看你能堅持多久~




        往期推薦


        歡迎關(guān)注我

        都看到這里了,隨手點個贊支持下唄!


        瀏覽 133
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产精品传媒 | 18禁色网站 | 女女互摸吃奶互慰视频免费app | 公和我乱做好爽添厨房h | 伊人操 | 爱搞搞就爱搞搞 | 特级西西人体www高清大胆 | 30分钟无遮挡高清视频 | 人妻懂色av粉嫩av浪潮av | 91国语对白精品露脸 |