LoginSignup
1

More than 3 years have passed since last update.

[Rust]Actixインストール手順

Last updated at Posted at 2019-04-13

1. Rustのインストール

https://www.rust-lang.org/tools/install
こちらで推奨されている通り、下記コマンドでインストールします。

curl https://sh.rustup.rs -sSf | sh

下記の通り、コンソールログが表示されます。

kanayatBookpuro:~ taichikanaya$ 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:

  /Users/taichikanaya/.cargo/bin

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

  /Users/taichikanaya/.profile
  /Users/taichikanaya/.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-apple-darwin
     default toolchain: stable
  modify PATH variable: yes

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

上記で止まりますので、1を入れてEnter押下します。そしてしばらく待ちます。

>1

info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2019-04-11, rust version 1.34.0 (91856ed52 2019-04-10)
info: downloading component 'rustc'
 78.9 MiB /  78.9 MiB (100 %)   1.7 MiB/s ETA:   0 s                
info: downloading component 'rust-std'
 51.1 MiB /  51.1 MiB (100 %)   1.6 MiB/s ETA:   0 s                
info: downloading component 'cargo'
  3.4 MiB /   3.4 MiB (100 %)   1.6 MiB/s ETA:   0 s                
info: downloading component 'rust-docs'
 10.2 MiB /  10.2 MiB (100 %)   1.5 MiB/s ETA:   0 s                
info: installing component 'rustc'
 78.9 MiB /  78.9 MiB (100 %)  10.8 MiB/s ETA:   0 s                
info: installing component 'rust-std'
 51.1 MiB /  51.1 MiB (100 %)  16.0 MiB/s ETA:   0 s                
info: installing component 'cargo'
info: installing component 'rust-docs'
 10.2 MiB /  10.2 MiB (100 %)   1.0 MiB/s ETA:   0 s                
info: default toolchain set to 'stable'

  stable installed - rustc 1.34.0 (91856ed52 2019-04-10)


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
kanayatBookpuro:~ taichikanaya$ 

2. IntelliJで新規Rustプロジェクト作成

https://github.com/intellij-rust/intellij-rust
こちらに載っている通り、IntelliJにRustプラグインを入れます。
・Toolchain locationにはRustのパスを設定します。
・Standard libraryはToolchain locationを入れると「rust up」リンクが表示されますので、
 それをクリックすると自動設定されます。
スクリーンショット 2019-04-13 17.34.45.png

下記の通り新規プロジェクトが作成されます。
スクリーンショット 2019-04-13 17.37.07.png

3. Cargo.tomlにactix設定追加

https://actix.rs/docs/installation/
こちらを参考に、dependenciesを追記します。

Cargo.toml
[package]
name = "xxxxxxx"
version = "0.1.0"
authors = ["xxxxxxxxx"]
edition = "2018"

[dependencies]
actix-web = "0.7" # これを追加

4. main.rsを実装

indexはhttpリクエストの受け口です。
mainではAPサーバを起動しています。

main.rs
extern crate actix_web;
use actix_web::{server, App, HttpRequest};

fn index(_req: &HttpRequest) -> &'static str {
    "Hello world!"
}

fn main() {
    server::new(|| App::new().resource("/", |r| r.f(index)))
        .bind("127.0.0.1:8088")
        .unwrap()
        .run();
}

5. いざ実行

cargo.tomlのあるディレクトリまで移動してから下記コマンドを実行します。

cargo run

6. Webブラウザからアクセス

このようにHello worldが表示されればOKです。
スクリーンショット 2019-04-13 18.24.30.png

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