rustを動かしてみる

テンプレあった 。

https://hub.docker.com/_/rust

FROM rust:1.31

WORKDIR /usr/src/myapp
COPY . .

RUN cargo install --path .

CMD ["myapp"]

これでいける!!

❯ docker build -t my-rust-app .                                                                                                                                                                                                                                      Sending build context to Docker daemon   38.4kB                                                                                                                                                                                                                      Step 1/5 : FROM rust:1.31                                                                                                                                                                                                                                            1.31: Pulling from library/rust                                                                                                                                                                                                                                      cd8eada9c7bb: Pull complete                                                                                                                                                                                                                                          c2677faec825: Pull complete                                                                                                                                                                                                                                          fcce419a96b1: Pull complete                                                                                                                                                                                                                                          045b51e26e75: Pull complete                                                                                                                                                                                                                                          3b969ad6f147: Pull complete                                                                                                                                                                                                                                          2074c6bfed7d: Pull complete                                                                                                                                                                                                                                          Digest: sha256:e2c4e3751290e30c3f130ef3513c7999aee87b5e7ac91e2fc9f3addcdf1f1387                                                                                                                                                                                      Status: Downloaded newer image for rust:1.31                                                                                                                                                                                                                          ---> 6f61eb35ad91                                                                                                                                                                                                                                                   Step 2/5 : WORKDIR /usr/src/myapp                                                                                                                                                                                                                                     ---> Running in 95da21250478                                                                                                                                                                                                                                        Removing intermediate container 95da21250478                                                                                                                                                                                                                          ---> 30baffbb7ef5                                                                                                                                                                                                                                                   Step 3/5 : COPY . .                                                                                                                                                                                                                                                   ---> 1fa37b80b9c8                                                                                                                                                                                                                                                   Step 4/5 : RUN cargo install --path .                                                                                                                                                                                                                                 ---> Running in 8525c7bd068f                                                                                                                                                                                                                                        error: `/usr/src/myapp` is not a crate root; specify a crate to install from crates.io, or use --path or --git to specify an alternate source                                                                                                                                                                                                                                                                                                                                                                                             Caused by:                                                                                                                                                                                                                                                             failed to read `/usr/src/myapp/Cargo.toml`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Caused by:                                                                                                                                                                                                                                                             No such file or directory (os error 2)                                                                                                                                                                                                                             The command '/bin/sh -c cargo install --path .' returned a non-zero code: 101 

いけない。そういうもんだよね。。

cargoはいってないんじゃね?? cargo -vあるかな??

❯ docker build -t my-rust-app .                                                                                                                                                                                                                                      Sending build context to Docker daemon   38.4kB                                                                                                                                                                                                                      Step 1/6 : FROM rust:1.31                                                                                                                                                                                                                                             ---> 6f61eb35ad91                                                                                                                                                                                                                                                   Step 2/6 : WORKDIR /usr/src/myapp                                                                                                                                                                                                                                     ---> Using cache                                                                                                                                                                                                                                                     ---> 30baffbb7ef5                                                                                                                                                                                                                                                   Step 3/6 : COPY . .                                                                                                                                                                                                                                                   ---> 15a7e974ab07                                                                                                                                                                                                                                                   Step 4/6 : RUN cargo -v                                                                                                                                                                                                                                               ---> Running in 647ee25bda4d                                                                                                                                                                                                                                        Rust's package manager                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    USAGE:                                                                                                                                                                                                                                                                   cargo [OPTIONS] [SUBCOMMAND]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          OPTIONS:                                                                                                                                                                                                                                                                 -V, --version           Print version info and exit                                                                                                                                                                                                                      --list              List installed commands                                                                                                                                                                                                                          --explain <CODE>    Run `rustc --explain CODE`                                                                                                                                                                                                                   -v, --verbose           Use verbose output (-vv very verbose/build.rs output)                                                                                                                                                                                        -q, --quiet             No output printed to stdout                                                                                                                                                                                                                      --color <WHEN>      Coloring: auto, always, never                                                                                                                                                                                                                    --frozen            Require Cargo.lock and cache are up to date                                                                                                                                                                                                      --locked            Require Cargo.lock is up to date                                                                                                                                                                                                             -Z <FLAG>...            Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details                                                                                                                                                                      -h, --help              Prints help information                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Some common cargo commands are (see all commands with --list):                                                                                                                                                                                                           build       Compile the current package                                                                                                                                                                                                                              check       Analyze the current package and report errors, but don't build object files                                                                                                                                                                              clean       Remove the target directory                                                                                                                                                                                                                              doc         Build this package's and its dependencies' documentation                                                                                                                                                                                                 new         Create a new cargo package                                                                                                                                                                                                                               init        Create a new cargo package in an existing directory                                                                                                                                                                                                      run         Build and execute src/main.rs                                                                                                                                                                                                                            test        Run the tests                                                                                                                                                                                                                                            bench       Run the benchmarks                                                                                                                                                                                                                                       update      Update dependencies listed in Cargo.lock                                                                                                                                                                                                                 search      Search registry for crates                                                                                                                                                                                                                               publish     Package and upload this package to the registry                                                                                                                                                                                                          install     Install a Rust binary                                                                                                                                                                                                                                    uninstall   Uninstall a Rust binary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   See 'cargo help <command>' for more information on a specific command.                                                                                                                                                                                               Removing intermediate container 647ee25bda4d                                                                                                                                                                                                                          ---> 620da2e768fd                                                                                                                                                                                                                                                   Step 5/6 : RUN cargo install --path .                                                                                                                                                                                                                                 ---> Running in 8f8b93d706b9
error: `/usr/src/myapp` is not a crate root; specify a crate to install from crates.io, or use --path or --git to specify an alternate source

Caused by:
  failed to read `/usr/src/myapp/Cargo.toml`

Caused by:
  No such file or directory (os error 2)
The command '/bin/sh -c cargo install --path .' returned a non-zero code: 101

あるね。。

あ。。

tomlがないのね なにそれ

バージョン管理ね https://doc.rust-lang.org/cargo/reference/manifest.html

バージョン管理してるんなら、yarnと一緒でしょ。。init あるでしょ

あるね https://doc.rust-lang.org/cargo/commands/cargo-init.html

環境変数$USERがないと。。

たしかに、必要って書いてる https://doc.rust-lang.org/cargo/commands/cargo-init.html

こうやるのね https://stackoverflow.com/a/39598751/10677105

どうかな

よさそう ご飯たべるのわすれてたよ

うごいたうごいた

これだと、dockerにはいって頑張るしかないなめんどい

composeにしよう

これはいい感じに見える https://techblog.primestructure.co.jp/2019/08/18/docker-%E3%81%A7-rust-%E3%81%8C%E5%8B%95%E3%81%8F%E7%92%B0%E5%A2%83%E3%82%92%E6%A7%8B%E7%AF%89%E3%81%99%E3%82%8B/

"????? だぶるーくぉーてーしょんね http://www.eonet.ne.jp/~usakuma/hptec/hp_tec/hptec_style_41.htm

だぶるくぉーてーしょんにした

いろいろコピペしてきた 動くかな

はい、ダブルクォーテーションね

うごいたうごいた

さんぷるをやっていこう

いろいろやれそう https://doc.rust-lang.org/book/ch00-00-introduction.html

rust language server いれよう https://marketplace.visualstudio.com/items?itemName=rust-lang.rust

rustupないっていうな https://github.com/rust-lang/rls-vscode/issues/585 あと、どっかにでふぉはnightlyだから、stableにしろってかいてた。

うごかないのな

あ、rust入れたのwslの中のdocker内部だけだから、wslにインストールしないといけない。いれよう https://www.rust-lang.org/tools/install

rustupないって。。

https://github.com/rust-lang/rls-vscode/issues/237#issuecomment-478299249 なるほど、

うごいたうごいた

shift + alt + noneはキー配列いじった結果です。すいません

rustのif, roopなどは式であるため、値を返す。rubyっぽい

rustのifはbool以外をエラーとする。jsは0, 1を評価できる。

rustには所有権がある。下記プログラムを実行するとs1は所有権を失っているため、エラーになる

おもしろいな。

しかし、リテラルを扱う場合、スタックに存在するため、問題ない。例えば、下記なら、Hello, world!が出力できる。

また、メモリの有効範囲は下記になっている。作成したらメモリにpush。スコープを外れたら、メモリをpop(rustではdrop)。

また、& を使用すると、参照を使用するため、所有権は変わらない。

rustは関数内で引数に対して変更を実行できない(default)。

mutableにすることで可能になる。しかし、スコープ内で一度しかできない。もし複数回使用したければ、スコープを別にする。

rustは不変参照を複数回実行できるが、不変参照と、可変参照をくみあわせることはできない。

rustは終了する参照をリターンできない。下記の場合、ちょくせつstringを返すのが正解。

最終更新

役に立ちましたか?