main

この新年から始まった持ち回りシリーズ、最後の新カテゴリは、Rust。

Rust programming language

プログラミング言語Rustのススメ - Qiita
Install - Rust programming language

凄く簡単に導入できるということだが・・・。

まずダウンロードだが、

$ curl https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming 
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:

  /home/xxx/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:

  /home/xxx/.profile
  /home/xxx/.bash_profile

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2019-01-17, rust version 1.32.0 (9fda7c223 2019-01-16)
info: downloading component 'rustc'
 79.5 MiB /  79.5 MiB (100 %)   2.5 MiB/s ETA:   0 s                
info: downloading component 'rust-std'
 54.3 MiB /  54.3 MiB (100 %)   2.3 MiB/s ETA:   0 s                
info: downloading component 'cargo'
  4.4 MiB /   4.4 MiB (100 %)   2.7 MiB/s ETA:   0 s                
info: downloading component 'rust-docs'
  8.5 MiB /   8.5 MiB (100 %)   2.3 MiB/s ETA:   0 s                
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: default toolchain set to 'stable'

  stable installed - rustc 1.32.0 (9fda7c223 2019-01-16)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH 
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env
$

こうする。

で、Cargoを使うという意味でも、rustcを使うという意味でも、

$ source $HOME/.cargo/env

これは一応やっといたほうがいいかな。(もしかしたらいらないかもだけど、確認の意味で。)

で、確かに、

$ cargo new main

このようにしてmainプロジェクトを作成し、

$ cargo run

でそのままビルドと実行をしてくれるが、".git"や".gitignore"がある辺り、レポジトリを作ってしまってるっぽい。
なので、

Rust触ってみたらかなり良かった話 | ブログ :: Web notes.log

ここではrustcコマンドでコンパイルし、実行するような流れとする。

$ vi main.rs

でmain.rsを開き、

と書く。

rust コーディングルール メモ - Qiita

今度はfnの後にmain()・・・。
インデントはスペース4つ・・・。
また違う・・・。

$ vi main.rs
$ cat main.rs 
fn main(){
    println!("ABCDE");
}
$ source $HOME/.cargo/env
$ rustc --version
rustc 1.32.0 (9fda7c223 2019-01-16)
$ rustc main.rs 
$ ls
main  main.rs
$ ./main 
ABCDE
$

sourceであったり、mainバイナリが出来てるかなど、ひととおり確認しつつ、ビルドから実行までを行い、"ABCDE"の出力を確認。
println!もこれまでにない書き方なので気になるが、その辺は後で・・・。

Sample/rust/main/main/src/main at master · bg1bgst333/Sample · GitHub