1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rustを始めよう!入門編

Last updated at Posted at 2020-12-28

はじめに

今人気爆上げ中の言語、Rust。
今回は、Rust入門編ということで、Rustの特徴およびインストール方法についてまとめていこうと思います。

対象者

  • Rustの特徴について知りたい人
  • brewを使ってinstallしたい人

Rust入門

Rustとは何か?

  • Rustはシステムプログラミング言語であり、C言語Haskellの特徴を持つ言語といえます。

  • ちなみに、Mozillaが中心となりオープンソースで開発しました。

システムプログラミング言語とは?

  • コンピューターのハードウェアの操作や制御のために設計されたソフトウェアで、アプリケーションソフトウェアを実行するためのプラットフォームを書くために設計されているような言語です。
  • CPUやメモリなどの詳細な制御(例えば、メモリの領域を確保したり、メモリの割り当てを解放すること)が可能であることから、上記のようなことがいえます。

Rustの特徴

  • Rustは、コンパイル言語です。なので、Windowsですとexeファイルの作成ができます。
  • ちなみに、プログラミング言語では、**コンパイル言語(静的プログラミング言語)スクリプト言語(動的プログラミング言語)**があります。
  • また、関数型言語のアイデアを多く取り入れていますが、関数型言語ではありません。

安全性

  • Rustでは、Rust上に存在する変数に入れられ、その変数のことをその値の所有者と呼びます。
  • 値の所有者になれる変数は、**ある期間(スコープ)**に一つのみです。
  • 所有者である変数がスコープから外れた場合は、その値は利用できなくなります。
  • また、Rustはnull安全な言語です。

→ その為、スコープを与えることにより、メモリリークやリソースリークのバグの多くをコンパイル時点で発見することができます。

→ さらに、所有者を決めることにより、複数箇所から同時にメモリを書き替えることができなくなります。こちらもコンパイラによってチェックされます。

null安全とは?

null安全とは、null(nil, Noneなど)が原因で実行時エラーにならない仕組みです。

他のコンパイル言語でのメモリ管理はどうなっていた?

  • Javaなどでは、ガベージコレクションにより、自動で不要なメモリをかき集める仕組みがあります。
  • C言語などでは、mallocやnewで動的に確保可能なメモリの領域(ヒープ)から領域を確保して、free,deleteで開放するようにしていました。

速度

  • C言語やC++に匹敵する速度を持っているようです。(Rustでは、メモリ効率が高くランタイムやガベージコレクタがありません。)

並行性

  • 所有者を決めるため、マルチスレッドでも安全に実行できます。

インストール方法

  • 最新のバージョンは、1.48.0(2020/12/28時点)です。参照: Latest version
  • install方法はいろいろあるが、今回はbrewを用いてinstallしていきます。
  • brewの場合だと、下記のコマンドを実行することでinstallできます。
brew install rust
  • Rustのinstallが終わると、rustcコマンドが使えるようになります。
  • 下記は実行結果です。
❯ rustc
Usage: rustc [OPTIONS] INPUT

Options:
    -h, --help          Display this message
        --cfg SPEC      Configure the compilation environment
    -L [KIND=]PATH      Add a directory to the library search path. The
                        optional KIND can be one of dependency, crate, native,
                        framework, or all (the default).
    -l [KIND=]NAME      Link the generated crate(s) to the specified native
                        library NAME. The optional KIND can be one of
                        static, framework, or dylib (the default).
        --crate-type [bin|lib|rlib|dylib|cdylib|staticlib|proc-macro]
                        Comma separated list of types of crates
                        for the compiler to emit
        --crate-name NAME
                        Specify the name of the crate being built
        --edition 2015|2018
                        Specify which edition of the compiler to use when
                        compiling code.
        --emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
                        Comma separated list of types of output for the
                        compiler to emit
        --print [crate-name|file-names|sysroot|target-libdir|cfg|target-list|target-cpus|target-features|relocation-models|code-models|tls-models|target-spec-json|native-static-libs]
                        Compiler information to print on stdout
    -g                  Equivalent to -C debuginfo=2
    -O                  Equivalent to -C opt-level=2
    -o FILENAME         Write output to <filename>
        --out-dir DIR   Write output to compiler-chosen filename in <dir>
        --explain OPT   Provide a detailed explanation of an error message
        --test          Build a test harness
        --target TARGET Target triple for which the code is compiled
    -W, --warn OPT      Set lint warnings
    -A, --allow OPT     Set lint allowed
    -D, --deny OPT      Set lint denied
    -F, --forbid OPT    Set lint forbidden
        --cap-lints LEVEL
                        Set the most restrictive lint level. More restrictive
                        lints are capped at this level
    -C, --codegen OPT[=VALUE]
                        Set a codegen option
    -V, --version       Print version info and exit
    -v, --verbose       Use verbose output

Additional help:
    -C help             Print codegen options
    -W help             Print 'lint' options and default settings
    --help -v           Print the full set of options rustc accepts
  • versionを確認してみます。
❯ rustc --version
rustc 1.48.0

まとめ

  • 特徴を見てわかるように、C言語やC++を使っていた人が興味を持つのは間違いなさそうです。
  • 次回は、公式のドキュメントの1章,3章当たりをやってみようと思います。

参考文献

1
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?