Rust Expressions

CK CK 3788 
Created at  
925 0 0

A Rust program is (mostly) made up of a series of statements:

fn main() {
    // statement
    // statement
    // statement
}

There are a few kinds of statements in Rust. The most common two are declaring a variable binding, and using a ; with an expression:

fn main() {
    // variable binding
    let x = 5;

    // expression;
    x;
    x + 1;
    15;
}

Blocks are expressions too, so they can be used as values in assignments. The last expression in the block will be assigned to the place expression such as a local variable. However, if the last expression of the block ends with a semicolon, the return value will be ().

fn main() {
    let x = 5u32;

    let y = {
        let x_squared = x * x;
        let x_cube = x_squared * x;

        // This expression will be assigned to `y`
        x_cube + x_squared + x
    };

    let z = {
        // The semicolon suppresses this expression and `()` is assigned to `z`
        2 * x;
    };

    println!("x is {:?}", x);
    println!("y is {:?}", y);
    println!("z is {:?}", z);
}

 

Tags Rust Expression Rust Syntax Semicolon Facebook X
Comments 0
  1. Rust Unsafe Operations
    362
  2. Rust Testing
    1,234
  3. Rust Std misc
    372
  4. Rust Std library types
    368
  5. Rust Error Handling
    342
  6. Rust Macro Rules
    1,507
  7. Rust mechanism for defining shared behavior across different types - Traits
    1,761
  8. Rust Scoping Rules - only accessible within the block of code
    1,066
  9. Rust Generics - Generalizing types and functionalities
    1,349
  10. Rust Attributes for conditional compilation, crate, lints, mark functions, macros
    313
  11. Rust Package Management Tool - cargo
    307
  12. Rust compilation unit - Crates
    251
  13. Rust Modules - Visivility, Struct Visibility, Use Declaration, super and self, File Hierarchy
    1,889
  14. Rust functions - Methods, Closures, Capturing
    1,198
  15. Rust Flow of Control - if/else, loop. while, for and range, match, if let, let-else, while let
    335
  16. Rust Conversion - From, Into, TryFrom, TryInto
    1,309
  17. gRPC library for Rust
    342
  18. Rust Data Type Operations - Casting, Literals, Inference, Aliasing
    326
  19. Variable Bindings in Rust
    966
  20. Rust Custom Data Types - struct, enum, use, constants
    298
  21. Arrays and Slices in Rust
    337
  22. Tuples in Rust - a collection of values of different types
    297
  23. Literals and operators in Rust
    296
  24. Rust Data Type - What is mutable?
    256
  25. Rust Code Testing Environment without installing anything on your computer
    324
  26. Rust Thread Example
    346
  27. How do I setup Rust development environment?
    368
  28. Rust - System Programming Language invented by Graydon Hoare
    280
  29. Why America's Biggest Tech Companies Are Betting on Rust?
    287
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,017
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    251
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    262