Skip to content

Instantly share code, notes, and snippets.

@waynewyang
Created March 19, 2019 02:03
Show Gist options
  • Save waynewyang/f6cb6f4c9fbb3c0f221c9f39853299a2 to your computer and use it in GitHub Desktop.
Save waynewyang/f6cb6f4c9fbb3c0f221c9f39853299a2 to your computer and use it in GitHub Desktop.
instructions for getting started with rust, vim, rustfmt, and stuff

Rust setup

  1. Install the following nix packages

    Also make sure you're running a recent nix version. 17.x had rustup 1.3 which wasn't working for me with the rust servers, but 18.03 has rustup 1.11.0 (currently) which works.

    • rustup
    • binutils
    • gcc
    • openssl
    • pkgconfig
    • gnumake
  2. Install the stable toolchain

    Includes rustc, cargo, and friends. The install command might fail with an obscure error if your rustup is too old. The default command activates the stable toolchain for commands which don't specify their toolchain.

    $ rustup install stable
    $ rustup default stable
  3. Set up vim

    1. Syntax highlighting & rustfmt on save

      Install rustfmt first. The first command lists all available components. The second adds rustfmt to your current toolchain.

      $ rustup component list
      $ rustup component add rustfmt-preview

      Get the rust.vim plugin installed.

      $ cd ~/.vim/bundle
      $ git clone --depth=1 https://github.com/rust-lang/rust.vim.git

      Configure rust.vim to autoformat on save and tell it how to find rustfmt.

      let g:rustfmt_autosave = 1
      let g:rustfmt_command = 'rustup run stable rustfmt'
    2. Code completion & navigation

      Install racer. This will download and compile a lot of rust code. You'll also need to install the rust source tree.

      $ cargo install racer
      $ rustup component add rust-src

      Get the vim-racer plugin installed.

      $ cd ~/.vim/bundle
      $ git clone https://github.com/racer-rust/vim-racer.git

      Configure vim-racer with keyboard commands and tell it how to find the racer executable.

      let g:racer_cmd = '~/.cargo/bin/racer'
      let g:racer_experimental_completer = 1
      au FileType rust nmap gd <Plug>(rust-def)
      au FileType rust nmap gs <Plug>(rust-def-split)
      au FileType rust nmap gx <Plug>(rust-def-vertical)
      au FileType rust nmap gr <Plug>(rust-doc)
    3. Tagbar for navigation in large files

      Make sure you have the following nix packages installed.

      • ctags

      Get the tagbar plugin installed.

      $ cd ~/.vim/bundle
      $ git clone https://github.com/majutsushi/tagbar.git`

      Configure tagbar to be togglable via the F8 key.

      nmap <F8> :TagbarToggle<CR>
      let g:tagbar_type_rust = {
          \ 'ctagstype' : 'rust',
          \ 'kinds' : [
              \'T:types,type definitions',
              \'f:functions,function definitions',
              \'g:enum,enumeration names',
              \'s:structure names',
              \'m:modules,module names',
              \'c:consts,static constants',
              \'t:traits',
              \'i:impls,trait implementations',
          \]
          \}
  4. Try out rust

    1. Make a project

      Create a project directory and get into vim.

      $ cargo new --bin try-rust
      $ cd try-rust/
      $ git init .
      $ git add .
      $ vim $(git ls-files)
    2. Use entr for a lightweight tight loop

      Make sure you have the following nix packages installed.

      • entr

      In another terminal, start a watcher to re-check your rust code every time you edit a file.

      $ git ls-files | entr -c cargo check

      When you change your module structure, add, or remove a file, you'll need to restart this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment