Rustを一から勉強1:HelloWorld

March 24, 2018

Rustというプログラミング言語に興味を持ち、勉強を始めることにしました。

今回の目標は下記の通りです。

  1. 開発環境をdockerで用意する
  2. プロジェクトを新規作成する
  3. ビルドする

Ⅰ.開発環境をdockerで用意する

まず、開発環境を準備します。今回はCentos7のdockerを使用しましたが、macやcoreos、windowsなどでもdockerがあれば同じ方法で試せると思います。適当なディレクトリにDockerfileとdocker-compose.ymlを作成。コンテナの中で作業するので、ファイル編集やビルドを実行するユーザーを追加しています。UID、GID、グループ名、ユーザー名はホストの環境に応じて変更します(UID,GID,UNAME,GNAME)。また、コンテナの中でファイルを編集するため、vimをインストールしています。vimの設定ファイルであるdot.vimrcは日本語の文字化けを回避したりracer、rustfmtというrust関連ツールをvimから使えるようにするためのものです。

FROM rust:1.24.1

ARG UID="500"
ARG GID="500"
ARG UNAME="admacks"
ARG GNAME="admacks"
RUN set -xe; apt-get update; apt-get install -y vim
RUN set -xe; groupadd -g ${GID} ${GNAME}; \
      useradd -m -g ${GID} -u ${UID} ${UNAME}
COPY dot.vimrc /home/${UNAME}/.vimrc
RUN set -xe; chown ${GNAME}:${UNAME} /home/${UNAME}/.vimrc
USER ${UNAME}
RUN cargo install racer
RUN rustup component add rust-src
RUN rustup component add rustfmt-preview
RUN set -xe; curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh -o /tmp/installer.sh; sh /tmp/installer.sh ~/.vim/dein
WORKDIR /usr/src/myapp
COPY . .
version: '2'

services:
  rust:
    build: .
    image: myrust
    user: 500:500
    environment:
      USER: myname
    volumes:
      - .:/usr/src/myapp
set encoding=utf-8
set fileencodings=utf-8,cp932
set fileencoding=utf-8
set fileformats=unix

"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
  endif

" Required:
set runtimepath+=~/.vim/dein/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state('~/.vim/dein')
  call dein#begin('~/.vim/dein')

  " Let dein manage dein
  " Required:
    call dein#add('~/.vim/dein/repos/github.com/Shougo/dein.vim')

  " Add or remove your plugins here:
  call dein#add('Shougo/neosnippet.vim')
  call dein#add('Shougo/neosnippet-snippets')

  " For rust
  call dein#add('rust-lang/rust.vim')
  call dein#add('racer-rust/vim-racer')

  " You can specify revision/branch/tag.
  "  call dein#add('Shougo/deol.nvim', { 'rev': 'a1b5108fd' })

  " Required:
  call dein#end()
  call dein#save_state()
endif

" Required:
filetype plugin indent on
syntax enable

" If you want to install not installed plugins on startup.
if dein#check_install()
  call dein#install()
endif

"End dein Scripts-------------------------

" racer
let g:racer_cmd = "/usr/local/cargo/bin/racer"
au FileType rust nmap gd (rust-def)
au FileType rust nmap gs (rust-def-split)
au FileType rust nmap gx (rust-def-vertical)
au FileType rust nmap gd (rust-doc)

 

そして下記のコマンドを実行してコンテナに入ります。

docker-compose run rust bash

 

Ⅱ.プロジェクトを新規作成する

RustではCargoというビルドシステムを使います。これでプロジェクトの雛形を作成します。上記のコマンドでコンテナに入った状態で、下記のコマンドを実行します。オプションの–binはライブラリではなく実行可能アプリケーションを生成する場合に必要。

cargo new --bin helloworld

これにより、helloworldというフォルダが作成されてその下に、雛形となるソースコードと設定ファイルが生成されます。srcフォルダの下にあるmain.rsがソースコードのようです。

fn main() {
    println!("Hello, world!");
}

なお、vimのプラグインの使い方としては、ソースコードを開いた後、:RustFmtで整形、挿入モード中にC-x C-oでracerによる補完を実行、:RustRunで実行、などです。

Ⅲ.ビルドする

このプログラムをビルド、実行するには、helloworldのフォルダで下記のコマンドを実行します。

cargo run

これでソースファイルがコンパイルされて、エラーがなければ実行されます。コンパイルした結果はtarget/debugの下に出力されました。デフォルトではデバッグ用のビルドになるようです。 なお、cargo buildならデバッグ用ビルド、cargo build –releaseならリリース用ビルド(最適化してくれる)をします。