🌱
Dev Compendium
  • Ethereum
    • Solidity
      • EVM
      • Architecture
      • Execution Context
      • Transactions
      • Gas
      • Calldata, Memory & Storage
      • Gas Optimisation
      • Function Declarations
      • receive() & fallback()
      • CALL vs. DELEGATE CALL
    • Yul
      • Introduction
      • Types
      • Basic Operations
      • Storage
      • Memory
        • Arrays
        • Structs
        • Tuples, revert, keccak256
        • Logs and Events
        • Gotchas
        • abi.encode
      • Calldata
        • External Calls
        • Dynamic Length Inputs
        • Transferring Value
        • Receiving Contract Calls
      • Contracts in Yul
      • Other Yul Functions
    • Foundry
    • Security
      • Common Vulnerabilities
      • Best Practices
      • Development Workflow
      • Contract Migration
    • Auditing Tools
      • Slither
      • Mythril
      • Fuzzing
    • Upgradable Contracts
      • Upgrade Patterns
      • ERC-1967 Implementation
      • Deployment
    • MEV
    • Tooling
      • Chainlink
      • IPFS
      • Radicle
    • Frontend
      • Contract Hooks
      • Wallet Connection
        • wagmi.sh
        • Rainbow Kit
      • thirdweb
    • Protocol Research
      • Uniswap v2
      • Uniswap v3
      • Curve
      • GMX
  • Starkware
    • Fundamentals
    • Account Abstraction
    • Universal Deployer
    • Cairo 1.0
    • starknet.js
    • Security Model
  • Zero Knowledge
    • Group Theory
    • ECDSA
  • Rust
    • Basic Operations
    • Set up
    • Primitives
    • Control Flow
    • Mutability & Shadowing
    • Adding Behavior
    • Lifetimes
    • Std Library
  • SUI
    • Architecture
    • Consensus Mechanism
    • Local Node Setup
    • Sui Client CLI
    • Move Contracts
      • Move
      • Move.toml
      • Move.lock
      • Accessing Time in Sui Move
      • Set up Development Framework
      • Debug & Publish
      • Package Upgrades
      • Sui Move Library
      • Difference from Core Move
    • Object Programming
      • Object Basics
      • Using Objects
      • Immutable Objects
      • Object Wrapping
      • Dynamic Fields
      • Collections
      • Unit Testing
      • Deployment with CLI
  • NEAR
    • Architecture
    • Contract Standards
      • Fungible Token (NEP-141)
      • Non-Fungible Token (NEP-171)
      • Storage Management (NEP-145)
      • Events (NEP-297)
      • Meta-Transactions
    • Rust Contracts
      • Development Workflow
      • Smart Contract Layout
      • Storage Management
      • Events & Meta-transactions
      • Method Types
      • Upgrading Contracts
      • Unit Testing
    • NEAR Libraries
    • Environment Variables
    • Serialisation
    • Security Concepts
    • Collections
    • JS SDK
Powered by GitBook
On this page
  1. Rust

Set up

PreviousBasic OperationsNextPrimitives

Last updated 1 year ago

Rust is a general-purpose, statically-typed, functionally-inspired, high-level programming language that specializes in memory safety and speed and features an algebraic type system.

  • General-purpose — Not limited to a particular application or platform, Rust can be used to build a diverse variety of solutions.

  • Statically-typed — Data types are known (or computed) and enforced at compile time.

  • Functionally-inspired — Many features are imported from, or are derivatives of features from FP-paradigm languages like Ocaml and Haskell.

  • High-level — Rust provides a large degree of abstraction from assembler code.

  • Memory safety — All safe code (that which does not use the unsafe keyword) is guaranteed by the compiler to not contain memory safety issues such as dangling pointers, null dereferencing, or accidental memory leaks.

  • Speed — Rust code compiles directly to machine code with zero or near-zero runtime overhead. It can fairly easily interoperate directly with C code. With the absence of a garbage collector, or really any runtime to speak of, memory is managed by the programmer, albeit within the strict bounds enforced by the compiler’s memory safety features, notwithstanding [unsafe blocks]().

  • Algebraic type system — While most modern programming languages include some form of , Rust also includes a fully-featured as well, in the form of a discriminated union ([enum](<https://doc.rust-lang.org/rust-by-example/custom_types/enum.html>)).

Set up

If you don’t want to install anything, you can just use the , and . Otherwise, if you want to set up your own system, just follow , as it’s not worth duplicating the information here.

Tools

If you follow the website, you’ll end up with a few different tools installed on your system:

  • rustup manages your Rust installation(s): what versions you have installed, for which targets, etc.

  • rustc is the Rust compiler. Although you can use it directly, it is cumbersome for large and complex projects.

  • cargo will probably be the most-used tool of the three mentioned here during your Rust career. It is an all-in-one project manager, dependency installer, linter, formatter, documentation builder, and build tool. (Technically it delegates out most of those commands to dedicated tools like rustc, rustdoc, rustfmt, and clippy, but Cargo brings them all together nicely.)

You can check that everything is installed properly by running cargo version, and you should see something like this:

$ cargo version cargo 1.66.1 (ad779e08b 2023-01-10)

rustup update stable

Creating a project with Cargo

Let’s get up-and-running as quickly as possible. If you’re familiar with Node.js, Cargo may feel a bit similar.

Create a new project in the directory hello-rust:

cargo new --bin hello-rust

The --bin flag means that the new project will create standalone executable binary. The other option is the --lib flag, which will create a library package, which will not compile into an executable application. If neither flag is specified, creating a binary application is the default.

Let’s quickly make sure that everything is working correctly by building and running the default generated “Hello World” project:

cd hello-rust cargo run

cargo run will implicitly run cargo build to build the application if the existing build files are out-of-date.

If everything worked according to plan, you should see something like the following:

Compiling hello-rust v0.1.0 (/.../hello-rust) Finished dev [unoptimized + debuginfo] target(s) in 0.92s Running target/debug/hello-rust Hello, world!

If everything appears to be working correctly, open up src/main.rs in your favorite editor.

You can check the latest stable version number on , and you can upgrade the version currently installed on your computer with this command:

https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
product type
sum type
Rust playground
skip this section
the guide on the Rust website
the Releases page on GitHub