株のシステムトレードをしよう - 1から始める株自動取引システムの作り方

株式をコンピュータに売買させる仕組みを少しずつ作っていきます。できあがってから公開ではなく、書いたら途中でも記事として即掲載して、後から固定ページにして体裁を整える方式で進めていきます。

Rust開発環境のセットアップとHelloWorld

下記の記事を参考に進める。なお、実行環境は intel mac (Big Sur) である。

note.com

インストール

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 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.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /Users/user_name/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /Users/user_name/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /Users/user_name/.cargo/bin

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

  /Users/user_name/.profile
  /Users/user_name/.bash_profile
  /Users/user_name/.bashrc
  /Users/user_name/.zshenv

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

Current installation options:


   default host triple: x86_64-apple-darwin
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
info: profile set to 'default'
info: default host triple is x86_64-apple-darwin
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2021-09-09, rust version 1.55.0 (c8dfcfe04 2021-09-06)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 17.1 MiB /  17.1 MiB (100 %)  10.5 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 21.0 MiB /  21.0 MiB (100 %)  10.5 MiB/s in  2s ETA:  0s
info: downloading component 'rustc'
 75.9 MiB /  75.9 MiB (100 %)  10.2 MiB/s in  7s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 17.1 MiB /  17.1 MiB (100 %)   5.1 MiB/s in  2s ETA:  0s
info: installing component 'rust-std'
 21.0 MiB /  21.0 MiB (100 %)  12.1 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 75.9 MiB /  75.9 MiB (100 %)  13.6 MiB/s in  5s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-apple-darwin'

  stable-x86_64-apple-darwin installed - rustc 1.55.0 (c8dfcfe04 2021-09-06)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

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

バージョン確認

$ rustc --version
rustc 1.55.0 (c8dfcfe04 2021-09-06)

$ rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.55.0 (c8dfcfe04 2021-09-06)`

$ cargo --version
cargo 1.55.0 (32da73ab1 2021-08-23)

actix-web のインストール

Hello Worldには actix-web は必須ではないが、あとで使いたいのでインストールしておきたい。 下記で実行したところ、うまくいかなかった。後で挽回する。

$ cargo install actix-web
    Updating crates.io index
  Downloaded actix-web v3.3.2
  Downloaded 1 crate (134.0 KB) in 0.75s
error: there is nothing to install in `actix-web v3.3.2`, because it has no binaries
`cargo install` is only for installing programs, and can't be used with libraries.
To use a library crate, add it as a dependency in a Cargo project instead.

プロジェクトの作成

$ cargo new time_and_sales_deliver

使用ライブラリの列挙とインストール

$ vim Cargo.toml
(前略)
[dependencies]
actix-web = "3"
$ cargo install --path .
  Installing time_and_sales_deliver v0.1.0 (/Users/user_name/project/github/time_and_sales_deliver)
    Updating crates.io index
  Downloaded sha-1 v0.9.8
  Downloaded thiserror-impl v1.0.29
(中略)
   Compiling actix-router v0.2.7
   Compiling actix-http v2.2.1
   Compiling awc v2.0.3
   Compiling actix-web v3.3.2
   Compiling time_and_sales_deliver v0.1.0 (/Users/user_name/project/github/time_and_sales_deliver)
    Finished release [optimized] target(s) in 2m 30s
  Installing /Users/user_name/.cargo/bin/time_and_sales_deliver
   Installed package `time_and_sales_deliver v0.1.0 (/Users/user_name/project/github/time_and_sales_deliver)` (executable `time_and_sales_deliver`)

2分30秒かかった。

プログラムの実行

$ cargo run
    Updating crates.io index
   Compiling proc-macro2 v1.0.29
   Compiling unicode-xid v0.2.2
   Compiling syn v1.0.76
(中略)
   Compiling actix-http v2.2.1
   Compiling awc v2.0.3
   Compiling actix-web v3.3.2
   Compiling time_and_sales_deliver v0.1.0 (/Users/user_name/project/github/time_and_sales_deliver)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 14s
     Running `target/debug/time_and_sales_deliver`
Hello, world!

1分14秒程かかった。一度ビルドすれば、次回からは実行が早い。

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/time_and_sales_deliver`
Hello, world!

(C) 2020 dogwood008 禁無断転載 不許複製 Reprinting, reproducing are prohibited.