ããã¯ããªã«ãããããŠæžãããã®ïŒ
Rustã®å匷ãå§ããããšæããŸããŠããŸãã¯ã€ã³ã¹ããŒã«ãããšããããšã§ã
Rust
Rustã®Webãµã€ãã¯ãã¡ãã
Rustã®ç¹åŸŽã¯ä»¥äžã®ããã«æžãããŠããŸãã
- ããã©ãŒãã³ã¹
- éåžžã«é«éã§ã¡ã¢ãªãŒå¹çã«åªãã
- ã©ã³ã¿ã€ã ãã¬ããŒãžã³ã¬ã¯ã·ã§ã³ããªããããããã©ãŒãã³ã¹ãéèŠãªãµãŒãã¹ã«åããŠãã
- ä¿¡é Œæ§
- è±å¯ãªåã·ã¹ãã ãšæææš©ã¢ãã«ã«ãããã¡ã¢ãªãŒã®å®å šæ§ãšã¹ã¬ããã®å®å šæ§ãä¿èšŒãã
- ã³ã³ãã€ã«æã«å€ãã®ãã°ãæé€ã§ãã
- çç£æ§
- ããã¥ã¡ã³ããã³ã³ãã€ã©ãŒãããã±ãŒãžãããŒãžã£ãŒããã«ãããŒã«ããšãã£ã¿ãŒã®ãµããŒãããã©ãŒããã¿ãŒãªã©ãæã£ãŠãã
ããã¥ã¡ã³ãã¯ãããèŠãŠããããã§ãã
Learn Rust - Rust Programming Language
æšæºã©ã€ãã©ãªãŒã®ããã¥ã¡ã³ãã¯ãã¡ãã
ãŸãã¯ããããå§ããŸãããã
Getting Started - The Rust Programming Language
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ããUbuntu Linux 24.04 LTSã§ãã
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.1 LTS Release: 24.04 Codename: noble $ uname -srvmpio Linux 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec 5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
Rustãã€ã³ã¹ããŒã«ãã
æåã¯Rustã®ã€ã³ã¹ããŒã«ããã§ãããRustã®ã€ã³ã¹ããŒã«ã¯ãrustupãšããCLIã䜿ã£ãŠè¡ãããã§ãã
Installation - The Rust Programming Language
Install Rust - Rust Programming Language
Getting started - Rust Programming Language
ãšããããã§ããŸãã¯rustupã®ã€ã³ã¹ããŒã«ããã§ããã
$ curl --proto '=https' --tlsv1.2 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.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
$HOME/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
$HOME/.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:
$HOME/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
$HOME/.profile
$HOME/.bashrc
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 (default)
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
ããèŠããšãrustupãCargoã䜿ããã£ã¬ã¯ããªã衚瀺ãããŠããŸãã
ãŸãæåŸã«éžæè¢ã衚瀺ãããŠããŸããããã®ãŸãŸEnterãæŒããŠããã©ã«ãã®1ãéžæã
ã€ã³ã¹ããŒã«ãå®äºããŸããã
info: profile set to 'default' info: default host triple is x86_64-unknown-linux-gnu info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: latest update on 2024-11-28, rust version 1.83.0 (90b35a623 2024-11-26) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' 16.4 MiB / 16.4 MiB (100 %) 10.8 MiB/s in 1s ETA: 0s info: downloading component 'rust-std' 26.1 MiB / 26.1 MiB (100 %) 10.9 MiB/s in 3s ETA: 0s info: downloading component 'rustc' 69.3 MiB / 69.3 MiB (100 %) 10.1 MiB/s in 8s ETA: 0s info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' 16.4 MiB / 16.4 MiB (100 %) 4.3 MiB/s in 3s ETA: 0s info: installing component 'rust-std' 26.1 MiB / 26.1 MiB (100 %) 8.7 MiB/s in 3s ETA: 0s info: installing component 'rustc' 69.3 MiB / 69.3 MiB (100 %) 9.6 MiB/s in 8s ETA: 0s info: installing component 'rustfmt' info: default toolchain set to 'stable-x86_64-unknown-linux-gnu' stable-x86_64-unknown-linux-gnu installed - rustc 1.83.0 (90b35a623 2024-11-26) 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, you need to source the corresponding env file under $HOME/.cargo. This is usually done by running one of the following (note the leading DOT): . "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh source "$HOME/.cargo/env.fish" # For fish
bashã®å Žåã$HOME/.bashrcã®æåŸã«ä»¥äžã®è¡ã远å ãããŠããŸããã
. "$HOME/.cargo/env"
ãã®ãããã·ã§ã«ã«ãã°ã€ã³ããªãããšrustupã䜿ããããã«ãªã£ãŠããŸãã
$ rustup --version rustup 1.27.1 (54dd3d00f 2024-04-24) info: This is the version for the rustup toolchain manager, not the rustc compiler. info: The currently active `rustc` version is `rustc 1.83.0 (90b35a623 2024-11-26)`
rustupã ãã§ã¯ãªããrustcãªã©ãã§ããã
$ rustc --version rustc 1.83.0 (90b35a623 2024-11-26) $ cargo --version cargo 1.83.0 (5ffbef321 2024-10-29)
ãã®ããããããŒãžã§ã³ãæã£ãŠãããã§ããã
rustupã§ã€ã³ã¹ããŒã«ãããããŒã«ãã¢ããããŒãããã«ã¯rustup updateãå®è¡ããããã§ãã
$ rustup update
Installation - The Rust Programming Language
rustupèªäœãã¢ãããŒãããã«ã¯rustup self updateãå®è¡ããããã§ãã
$ rustup self update
Basic usage / Keeping rustup up to date
Hello World
ãªã«ã¯ãšããããHello Worldãã
Hello, World! - The Rust Programming Language
ã²ãšãŸããã®ãŸãŸæžãåããŠã¿ãŸãã
main.rs
fn main() { println!("Hello, world!"); }
$ rustc main.rs
å®è¡ãã¡ã€ã«ãã§ããŸããã
$ ll åèš 3772 drwxrwxr-x 2 xxxxx xxxxx 4096 12æ 31 22:09 ./ drwxrwxr-x 3 xxxxx xxxxx 4096 12æ 31 21:56 ../ -rwxrwxr-x 1 xxxxx xxxxx 3849096 12æ 31 22:09 main* -rw-rw-r-- 1 xxxxx xxxxx 45 12æ 31 22:08 main.rs
å®è¡ã
$ ./main Hello, world!
åããŸããã
ã²ãšãŸããåããŠã®Rustã³ãŒããæžããŠåãããŠã¿ãŸããã
rust-analyzerïŒEmacs lsp-modeã䜿ã
Getting Startedã§ã¯ãã®åŸã«Cargoã®ç޹ä»ã«ç§»ãã®ã§ãããå ã«ãšãã£ã¿ãŒããªããšãããããšæããŸãã
èªåã¯Emacsã䜿ã£ãŠããã®ã§lsp-modeãšçµã¿åããããã®ã§ãããLanguage Serverã«ã¯rust-analyzerã䜿ãããã§ãã
Rust (rust-analyzer) - LSP Mode - LSP support for Emacs
rust-analyzerãrustupã§ã€ã³ã¹ããŒã«ã§ããŸãã
$ rustup component add rust-analyzer
ããšã¯rust-modeãã€ã³ã¹ããŒã«ããŠä»¥äžã远å ïŒlsp-modeã¯èšå®æžã¿ãšããŸãïŒã
;; rust (use-package lsp-mode :ensure t :commands (lsp lsp-deferred) :hook (rust-mode . lsp-deferred))
rust-modeã¯ãã¡ãã
GitHub - rust-lang/rust-mode: Emacs configuration for Rust
ã¡ãªã¿ã«ã䜿ã£ãŠã¿ãæãã ãšCargo.tomlããªããšåããªãæããããã®ã§å
ã«é²ã¿ãŸãã
Cargoã§ãããžã§ã¯ããäœæãã
次ã¯Cargoã䜿ã£ãŠã¿ãŸãããã
Hello, Cargo! - The Rust Programming Language
Cargoã¯Rustã®ãã«ãããŒã«ããã³ããã±ãŒãžãããŒãžã£ãŒã ããã§ãã
ãããžã§ã¯ãã®äœæã
$ cargo new hello-cardo
ãããžã§ã¯ãå ã«ç§»åã
$ cd hello-cardo
äœæããããããžã§ã¯ãã®äžèº«ã
$ tree
.
âââ Cargo.toml
âââ src
âââ main.rs
2 directories, 2 files
Cargo.toml
[package] name = "hello-cardo" version = "0.1.0" edition = "2021" [dependencies]
src/main.rs
fn main() { println!("Hello, world!"); }
Cargo.tomlã®editionãšããã®ã¯Rustã®èšèªã®ããŒãžã§ã³ã®ãããªãã®ã¿ããã§ããã
Introduction - The Rust Edition Guide
çŸåšã®ææ°ã®ãšãã£ã·ã§ã³ã¯2021ã§ãrustcã®ããã©ã«ãã¯2015ã®ããã§ãã
--edition 2015|2018|2021|2024
Specify which edition of the compiler to use when
compiling code. The default is 2015 and the latest
stable edition is 2021.
ã§ã¯ããã«ãããŠã¿ãŸãã
$ cargo build
ãããªè¡šç€ºãåºãŠ
$ cargo build
Compiling hello-cardo v0.1.0 ($HOME/hello-cardo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.18s
ãã«ãçµæã¯target/debugé
äžã«ã§ãããããªã®ã§ããããå®è¡ã
$ ./target/debug/hello-cardo Hello, world!
OKã§ããã
carog runã§ãå®è¡ã§ããããã§ãã
$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello-cardo`
Hello, world!
1床targetãã£ã¬ã¯ããªãcargo cleanã§åé€ããŠ
$ cargo clean
Removed 21 files, 7.5MiB total
å床cargo runãããšãã³ã³ãã€ã«ããå®è¡ãŸã§äžæ°ã«è¡ãããã§ãã
$ cargo run
Compiling hello-cardo v0.1.0 (/$HOME/hello-cardo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.21s
Running `target/debug/hello-cardo`
Hello, world!
ã¡ãªã¿ã«ã衚瀺ãèŠããšæ°ã¥ããŸããããã¯ãããã°ãã«ãïŒéçºçšïŒã§ãã
ãªãªãŒã¹ãã«ããäœæããã«ã¯ã--releaseãã€ããŠãã«ãããŸãã
$ cargo build --release
Compiling hello-cardo v0.1.0 ($HOME/hello-cardo)
Finished `release` profile [optimized] target(s) in 0.58s
ãã«ãçµæã¯target/releaseé
äžã«ã§ããŸãã
$ ./target/release/hello-cardo Hello, world!
ãªãªãŒã¹ãã«ãã¯æé©åãè¡ããããªã®ã§ã¢ããªã±ãŒã·ã§ã³ã®å®è¡é床ã¯åäžããããã§ããã代ããã«ã³ã³ãã€ã«ãé·ããªããšãã
ãã¬ãŒããªããããããã§ãã
å¿
èŠã«å¿ããŠéçºçšãšãªãªãŒã¹çšã®ãããã¡ã€ã«ã䜿ãåããŠããããã§ããããšãã°ããã³ãããŒã¯ããšãæã«ã¯--releaseã
ä»ããŠãã«ãããå¿
èŠããããŸãã
äŸåé¢ä¿ã远å ãã
ããã¥ã¡ã³ãã§ã®Cargoã䜿ã£ãGetting Startedã¯ããã§çµããã®ã§ããããã¡ãã«ã¯äŸåé¢ä¿ã远å ããäŸãããã®ã§è©ŠããŠã¿ãŸãããã
Getting started - Rust Programming Language
ferris-saysãšããäŸåé¢ä¿ã远å ããŸãã
$ cargo add ferris-says
ãããã远å ãããŸããã
Updating crates.io index
Adding ferris-says v0.3.2 to dependencies
Features:
- clippy
Updating crates.io index
Locking 11 packages to latest compatible versions
Adding aho-corasick v1.1.3
Adding ferris-says v0.3.2
Adding memchr v2.7.4
Adding regex v1.11.1
Adding regex-automata v0.4.9
Adding regex-syntax v0.8.5
Adding smallvec v1.13.2
Adding smawk v0.3.2
Adding textwrap v0.16.1
Adding unicode-linebreak v0.1.5
Adding unicode-width v0.1.14
Cargo.tomlã®dependenciesã«è¿œå ãããŠããŸãã
Cargo.toml
[package] name = "hello-cardo" version = "0.1.0" edition = "2021" [dependencies] ferris-says = "0.3.2"
ãã®è¿œå ããäŸåé¢ä¿ã®ããšãcrateïŒã¯ã¬ãŒãïŒãšåŒã¶ããã§ãã
远å ããã¯ã¬ãŒãã®ããã¥ã¡ã³ãã«æžãããŠããã³ãŒãäŸãããã®ãŸãŸäœ¿ã£ãŠã¿ãŸãã
ãããªæãã§ããã
src/main.rs
use ferris_says::say; use std::io::{stdout, BufWriter}; fn main() { let out = "Hello fellow Rustaceans!"; let width = 24; let mut writer = BufWriter::new(stdout()); say(out, width, &mut writer).unwrap(); }
cargo runã§çŽæ¥å®è¡ã
$ cargo run
Downloaded smawk v0.3.2
Downloaded ferris-says v0.3.2
Downloaded smallvec v1.13.2
Downloaded unicode-linebreak v0.1.5
Downloaded textwrap v0.16.1
Downloaded memchr v2.7.4
Downloaded aho-corasick v1.1.3
Downloaded regex-syntax v0.8.5
Downloaded regex v1.11.1
Downloaded unicode-width v0.1.14
Downloaded regex-automata v0.4.9
Downloaded 11 crates (1.9 MB) in 0.33s
Compiling memchr v2.7.4
Compiling regex-syntax v0.8.5
Compiling unicode-linebreak v0.1.5
Compiling unicode-width v0.1.14
Compiling smawk v0.3.2
Compiling smallvec v1.13.2
Compiling textwrap v0.16.1
Compiling aho-corasick v1.1.3
Compiling regex-automata v0.4.9
Compiling regex v1.11.1
Compiling ferris-says v0.3.2
Compiling hello-cardo v0.1.0 ($HOME/hello-cardo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.65s
Running `target/debug/hello-cardo`
__________________________
< Hello fellow Rustaceans! >
--------------------------
\
\
_~^~^~_
\) / o o \ (/
'_ - _'
/ '-----' \
ååã®ã³ãã³ãå®è¡æã«ã¯ã¬ãŒããããŠã³ããŒãããŠããã¿ããã§ããå®è¡ãç¡äºã§ããŸãããã
Rustã£ãŠäŸåããã¢ãžã¥ãŒã«ãäžç·ã«ãã«ããããã§ããããšå®è¡ãã°ãèŠãªããæããŸããã
ãŸãcargo build --releaseããã£ãŠã¿ããšæããã«é
ããªããŸããããã«ãã«æéãããããšã¯èããããšããããŸããããä»åæžãã
ãœãŒã¹ã³ãŒãã¯æ°è¡ãªã®ã§ç¢ºãã«éãããã§ããã
ãŸããä»åã¯ãããªãšããã§ããããã
æåŸã«lddã§äŸåããŠããå ±æã©ã€ãã©ãªãŒãèŠãŠãããŸãã
$ ldd target/release/hello-cardo
linux-vdso.so.1 (0x00007ffde8334000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x0000707799e99000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0000707799800000)
/lib64/ld-linux-x86-64.so.2 (0x0000707799ee3000)
確ãã«èšèªåºæã®å
±æã©ã€ãã©ãªãŒãªã©ã¯ãªãããã§ãã
â»éçºçšãã«ãã§ãåãçµæã«ãªããŸãã
ãããã«
Rustãå§ããããã«rustupãã€ã³ã¹ããŒã«ããŠãåããŠã®Rustã®ã³ãŒããæžãããããŠã¿ãŸããã
lsp-modeã®èšå®ãè¡ã£ãã®ã§ãããããå°ããã€æ £ããŠããããšæããŸãã