Open9

Rust

PINTOPINTO
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ cargo --version
cargo 1.62.1 (a748cf5a3 2022-06-08)

$ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.62.1 (e092d0b6b 2022-07-16)

info: cleaning up downloads & tmp directories
PINTOPINTO
$ cargo new hello-rust
     Created binary (application) `hello-rust` package

$ cd hello-rust
$ sudo apt install tree -y
$ tree
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
PINTOPINTO
$ cargo run
   Compiling hello-rust v0.1.0 (/home/xxxx/work/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.22s
     Running `target/debug/hello-rust`
Hello, world!
PINTOPINTO
  • From
Cargo.toml
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
  • To
Cargo.toml
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ferris-says = "0.2"
PINTOPINTO
$ cargo build
    Updating crates.io index
  Downloaded smawk v0.3.1
  Downloaded textwrap v0.13.4
  Downloaded smallvec v0.4.5
  Downloaded ferris-says v0.2.1
  Downloaded unicode-width v0.1.9
  Downloaded 5 crates (97.7 KB) in 0.74s
   Compiling unicode-width v0.1.9
   Compiling smawk v0.3.1
   Compiling smallvec v0.4.5
   Compiling textwrap v0.13.4
   Compiling ferris-says v0.2.1
   Compiling hello-rust v0.1.0 (/home/b920405/work/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 15.22s

PINTOPINTO
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "ferris-says"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9515ec2dd9606ec230f6b2d1f25fd9e808a2f2af600143f7efe7e5865505b7aa"
dependencies = [
 "smallvec",
 "textwrap",
 "unicode-width",
]

[[package]]
name = "hello-rust"
version = "0.1.0"
dependencies = [
 "ferris-says",
]

[[package]]
name = "smallvec"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90c5e5fe535e48807ab94fc611d323935f39d4660c52b26b96446a7b33aef10"

[[package]]
name = "smawk"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043"

[[package]]
name = "textwrap"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd05616119e612a8041ef58f2b578906cc2531a6069047ae092cfb86a325d835"
dependencies = [
 "smawk",
 "unicode-width",
]

[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
PINTOPINTO
main.rs
use ferris_says::say;
use std::io::{stdout, BufWriter};

fn main() {
    let stdout = stdout();
    let message = String::from("Hello fellow Rustaceans!");
    let width = message.chars().count();

    let mut writer = BufWriter::new(stdout.lock());
    say(message.as_bytes(), width, &mut writer).unwrap();
}
PINTOPINTO
$ cargo run

   Compiling hello-rust v0.1.0 (/home/xxxx/work/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/hello-rust`
 __________________________
< Hello fellow Rustaceans! >
 --------------------------
        \
         \
            _~^~^~_
        \) /  o o  \ (/
          '_   -   _'
          / '-----' \