Rust vs. Go

When to use Rust and when to use Go

1
2
3
> Go has a stronger focus on building web APIs and small services that can scale endlessly, especially with the power of goroutines.

> Rust works well for processing large amounts of data and other CPU-intensive operations, such as executing algorithms.

Gettting started Rust

Quickly set up a Rust development environment and write a small app!

  • Rustup
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    > the Rust installer and version management tool
    ➜ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 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/chyiyaqing/.rustup

    This can be modified with the RUSTUP_HOME environment variable.

    The Cargo home directory located at:

    /home/chyiyaqing/.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/chyiyaqing/.cargo/bin

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

    /home/chyiyaqing/.profile
    /home/chyiyaqing/.bashrc
    /home/chyiyaqing/.zshenv

    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 installation (default)
    2) Customize installation
    3) Cancel installation

    # rustup: Toolchain management
    - rustup update

    # Cargo: the Rust build tool and package manager, used to install dependencies and manage the building, testing, running, and publishing of your project.
    - cargo build : build your project
    - cargo run: run your project
    - cargo test: test your project
    - cargo doc: build documentation for your project
    - cargo publish: publish a library to crates.io

    # rustc: the Rust compiler
    > rustc is invoked indirectly by the **cargo build** or **cargo run** commands.
  • Toolchain overview
    Rust toolchain and ecosystem
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct  9 09:49 cargo          -- package manager
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 cargo-clippy
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 cargo-fmt
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 cargo-miri
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 clippy-driver
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rls
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rustc -- rust compiler
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rustdoc
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rustfmt
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rust-gdb
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rust-lldb
    -rwxr-xr-x 12 chyiyaqing chyiyaqing 15M Oct 9 09:49 rustup -- toolchain installer

    creates.io -- package repository
    docs.rs -- create documentation

Performance

  • Go

    1
    2
    > Go take advantage of the concurrency the language offers. The language provides goroutines that enable run function as subprocesses.
    > Go concurrency model allows to deploy workloads across multiple CPU cores, making it efficient language
  • Rust

    1
    > Rust is more efficient in execution algorithms and resource-intensive operations.

Concurrency

  • Go

    1
    2
    3
    > Go support concurrency.
    > For example, you're running a web server that handles API requests. use Go's goroutines to run each request as a subprocess, maximizing efficiency by offloading tasks to all available CPU cores.

  • Rust

    1

  • Channel

    1
    > A channel helps transfer a message from one thread to another.
  • Lock

    1
    > Data is only accessible when the lock is held. Rust relies on the principle of locking data instead of cod
  • Memory safety

    1

References