hello

./code/hello/hello.rs
fn main() {
    println!("Hello world!");
}

File extension is .rs.

To build a statically linked executable, run:

rust ./hello.rst

It generates an executable hello:

(py38) fangjuns-MacBook-Pro:hello fangjun$ file hello
hello: Mach-O 64-bit executable x86_64

(py38) fangjuns-MacBook-Pro:hello fangjun$ ls -lh hello
-rwxr-xr-x  1 fangjun  staff   460K Jun 12 11:41 hello

(py38) fangjuns-MacBook-Pro:hello fangjun$ otool -L ./hello
./hello:
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.0.0)

naming convention

  • my_var

  • my_func

comments

Like C/C++, it uses /**/ and //.

hello2 using cargo

cargo new hello2

It creates a new directory hello2 inside the current directory:

(py38) fangjuns-MacBook-Pro:code fangjun$ tree hello2/
hello2/
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
./code/hello2/Cargo.toml
[package]
name = "hello2"
version = "0.1.0"
edition = "2024"

[dependencies]
./code/hello2/src/main.rs
fn main() {
    println!("Hello, world!");
}
cd hello2
cargo build
tree . > a.txt
./code/hello2/a.txt
.
├── Cargo.lock
├── Cargo.toml
├── a.txt
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    └── debug
        ├── build
        ├── deps
        │   ├── hello2-517dbf907ca40ba4
        │   ├── hello2-517dbf907ca40ba4.2ejis4jtcc22k78hwpfevmpn9.rcgu.o
        │   ├── hello2-517dbf907ca40ba4.2u847h470kx4lrmip8o7wbm3j.rcgu.o
        │   ├── hello2-517dbf907ca40ba4.5xtf1o3fzpwipxjpgh6vuq5nb.rcgu.o
        │   ├── hello2-517dbf907ca40ba4.biyzt084nbvwnn6rzalk2kv68.rcgu.o
        │   ├── hello2-517dbf907ca40ba4.d
        │   ├── hello2-517dbf907ca40ba4.d4ptlawbgayqhc0mck8d1zb7y.rcgu.o
        │   └── hello2-517dbf907ca40ba4.ebi7az3oo64vunpcvr2ytqd4k.rcgu.o
        ├── examples
        ├── hello2
        ├── hello2.d
        └── incremental
            └── hello2-21n5fdb2vj2ji
                ├── s-h889axkwsg-0ius9rz-eplmacagf6o0mvmceghnny7ip
                │   ├── 2ejis4jtcc22k78hwpfevmpn9.o
                │   ├── 2u847h470kx4lrmip8o7wbm3j.o
                │   ├── 5xtf1o3fzpwipxjpgh6vuq5nb.o
                │   ├── biyzt084nbvwnn6rzalk2kv68.o
                │   ├── d4ptlawbgayqhc0mck8d1zb7y.o
                │   ├── dep-graph.bin
                │   ├── ebi7az3oo64vunpcvr2ytqd4k.o
                │   ├── query-cache.bin
                │   └── work-products.bin
                └── s-h889axkwsg-0ius9rz.lock

9 directories, 25 files
cd hello2
cargo build --release
tree . > b.txt
./code/hello2/b.txt
.
├── Cargo.lock
├── Cargo.toml
├── a.txt
├── b.txt
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    ├── debug
    │   ├── build
    │   ├── deps
    │   │   ├── hello2-517dbf907ca40ba4
    │   │   ├── hello2-517dbf907ca40ba4.2ejis4jtcc22k78hwpfevmpn9.rcgu.o
    │   │   ├── hello2-517dbf907ca40ba4.2u847h470kx4lrmip8o7wbm3j.rcgu.o
    │   │   ├── hello2-517dbf907ca40ba4.5xtf1o3fzpwipxjpgh6vuq5nb.rcgu.o
    │   │   ├── hello2-517dbf907ca40ba4.biyzt084nbvwnn6rzalk2kv68.rcgu.o
    │   │   ├── hello2-517dbf907ca40ba4.d
    │   │   ├── hello2-517dbf907ca40ba4.d4ptlawbgayqhc0mck8d1zb7y.rcgu.o
    │   │   └── hello2-517dbf907ca40ba4.ebi7az3oo64vunpcvr2ytqd4k.rcgu.o
    │   ├── examples
    │   ├── hello2
    │   ├── hello2.d
    │   └── incremental
    │       └── hello2-21n5fdb2vj2ji
    │           ├── s-h889axkwsg-0ius9rz-eplmacagf6o0mvmceghnny7ip
    │           │   ├── 2ejis4jtcc22k78hwpfevmpn9.o
    │           │   ├── 2u847h470kx4lrmip8o7wbm3j.o
    │           │   ├── 5xtf1o3fzpwipxjpgh6vuq5nb.o
    │           │   ├── biyzt084nbvwnn6rzalk2kv68.o
    │           │   ├── d4ptlawbgayqhc0mck8d1zb7y.o
    │           │   ├── dep-graph.bin
    │           │   ├── ebi7az3oo64vunpcvr2ytqd4k.o
    │           │   ├── query-cache.bin
    │           │   └── work-products.bin
    │           └── s-h889axkwsg-0ius9rz.lock
    └── release
        ├── build
        ├── deps
        │   ├── hello2-ae384e63350693e9
        │   └── hello2-ae384e63350693e9.d
        ├── examples
        ├── hello2
        ├── hello2.d
        └── incremental

14 directories, 30 files
(py38) fangjuns-MacBook-Pro:hello2 fangjun$ ls -lh target/debug/hello2 target/release/hello2
-rwxr-xr-x  1 fangjun  staff   463K Jun 12 21:35 target/debug/hello2
-rwxr-xr-x  1 fangjun  staff   388K Jun 12 21:37 target/release/hello2

(py38) fangjuns-MacBook-Pro:hello2 fangjun$ strip target/debug/hello2 target/release/hello2

(py38) fangjuns-MacBook-Pro:hello2 fangjun$ ls -lh target/debug/hello2 target/release/hello2
-rwxr-xr-x  1 fangjun  staff   325K Jun 12 21:39 target/debug/hello2
-rwxr-xr-x  1 fangjun  staff   325K Jun 12 21:39 target/release/hello2
(py38) fangjuns-MacBook-Pro:hello2 fangjun$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello2`
Hello, world!

hello3

cd code
cargo new --lib hello3
(py38) fangjuns-MacBook-Pro:code fangjun$ cargo new --lib hello3
    Creating library `hello3` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

(py38) fangjuns-MacBook-Pro:code fangjun$ tree hello3
hello3
├── Cargo.toml
└── src
    └── lib.rs

1 directory, 2 files
./code/hello3/Cargo.toml
[package]
name = "hello3"
version = "0.1.0"
edition = "2024"

[dependencies]
./code/hello3/src/lib.rs
pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

Pay attention to the function signature.

(py38) fangjuns-MacBook-Pro:hello3 fangjun$ cargo test
   Compiling hello3 v0.1.0 (/Users/fangjun/open-source/notes/docs/source/rust/code/hello3)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.81s
     Running unittests src/lib.rs (target/debug/deps/hello3-b178d4679cda4da3)

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests hello3

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

use_hello3

cd code
cargo new use_hello3
(py38) fangjuns-MacBook-Pro:use_hello3 fangjun$ vim Cargo.toml
(py38) fangjuns-MacBook-Pro:use_hello3 fangjun$ cargo run
     Locking 1 package to latest Rust 1.87.0 compatible version
   Compiling hello3 v0.1.0 (/Users/fangjun/open-source/notes/docs/source/rust/code/hello3)
   Compiling use_hello3 v0.1.0 (/Users/fangjun/open-source/notes/docs/source/rust/code/use_hello3)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.71s
     Running `target/debug/use_hello3`
Hello, world! 5
./code/use_hello3/Cargo.toml
[package]
name = "use_hello3"
version = "0.1.0"
edition = "2024"

[dependencies]
hello3={path = "../hello3"}
./code/use_hello3/src/main.rs
fn main() {
    let result = hello3::add(2, 3);
    println!("Hello, world! {}", result);
}

hello_comments

cd code
cargo new hello_comment
./code/hello_comment/Cargo.toml
[package]
name = "hello_comment"
version = "0.1.0"
edition = "2024"

[dependencies]
./code/hello_comment/src/main.rs
//! <b> hello comment </b>
//! <p> Author: my name </p>
//! <p> Apache 2.0 License</p>

/// add two numbers
pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

/// this is the main function
fn main() {
    println!("Hello, world!");
}
(py38) fangjuns-MacBook-Pro:hello_comment fangjun$ cargo doc
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
   Generated /Users/fangjun/open-source/notes/docs/source/rust/code/hello_comment/target/doc/hello_comment/index.html