LoginSignup
3
3

More than 5 years have passed since last update.

Vim/syntastic/cargo-quickfixを使ってrustcコンパイル結果を確認しながらコードを書く

Last updated at Posted at 2017-01-12

以下の様なことができるようになります。

cargo-quickfix with vim

cargo-quickfixのインストール

cargo-quickfixrustc コンパイル結果をVimのquickfix(errorformat)でパースしやすい形で出力するツールです。 cargo build --message-format json の結果をパースして整形表示しています。

インストールは
cargo-quickfixgit clone してきて cargo install でインストールします。

rustup等を使ってRust, cargoあたりのセットアップは終わってる前提です。

$ git clone https://github.com/hhatto/cargo-quickfix.git
$ cd cargo-quickfix
$ cargo install --force

インストールが終わったら、おもむろにcargo newで作成したプロジェクトで cargo quickfix を実行してみます。

$ cargo quickfix
src/main.rs:3:5: warning: unused import: `std::thread`, #[warn(unused_imports)] on by default
src/main.rs:24:9: warning: unused variable: `stderr`, #[warn(unused_variables)] on by default

ファイル名:行数:カラム数: 警告レベル: メッセージ の形式で1行につき1つのメッセージを表示します。

Vim/syntasticと合わせて使う

cargo-quickfixのインストールに引き続き、Vim/syntastic用に設定を変更します。

$ mkdir -p PATH_TO_VIM_SYNTASTIC/syntax_checkers/rust
$ cp -p vim_syntastic/cargo.vim PATH_TO_VIM_SYNTASTIC/syntax_checkers/rust/
$ vim .vimrc
let g:syntastic_rust_checkers = ['cargo']   "" 'cargo'を追加する

これで冒頭のような環境が整います。

制限事項とか

  • cargo new で作成したプロジェクトディレクトリ直下でしか動作しないと思います。cargo build 実行しているので。
  • cargo build を実行するので意図せず target ディレクトリ配下にバイナリが作られたりします。
  • 初回実行時や巨大なプロジェクトだと実行に時間がかかると思います。( cargo-check とかに置き換えたらちょっとは速くなるかな...?)
  • Rustのファイルさわるときに毎回これ走ってしまう。 Cargo.tomlがあるとかそういうのチェックしたほうがいいかもしれない。

おわりに

rustc.vimだとcargoで作ったプロジェクトとかで外部crateが解決できなかったので、あまり実用的ではなく使っていませんでした。(もしかしたら私の設定がおかしいだけなのかもしれないです。。。) cargo-quickfix使うとそのあたりをうまく処理できるようになったので、少しの間使ってみようかと思います。

みなさんもよければ使ってみてください。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3