Literals and operators in Rust

CK CK 3788 
Created at  
300 0 0

Integers 1, floats 1.2, characters 'a', strings "abc", booleans true and the unit type () can be expressed using literals.

Integers can, alternatively, be expressed using hexadecimal, octal or binary notation using these prefixes respectively: 0x, 0o or 0b.

Underscores can be inserted in numeric literals to improve readability, e.g. 1_000 is the same as 1000, and 0.000_001 is the same as 0.000001.

Rust also supports scientific E-notation, e.g. 1e6, 7.6e-4. The associated type is f64.

We need to tell the compiler the type of the literals we use. For now, we'll use the u32 suffix to indicate that the literal is an unsigned 32-bit integer, and the i32 suffix to indicate that it's a signed 32-bit integer.

The operators available and their precedence in Rust are similar to other C-like languages.

fn main() {
    // Integer addition
    println!("1 + 2 = {}", 1u32 + 2);

    // Integer subtraction
    println!("1 - 2 = {}", 1i32 - 2);
    // TODO ^ Try changing `1i32` to `1u32` to see why the type is important

    // Scientific notation
    println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3);

    // Short-circuiting boolean logic
    println!("true AND false is {}", true && false);
    println!("true OR false is {}", true || false);
    println!("NOT true is {}", !true);

    // Bitwise operations
    println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
    println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
    println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
    println!("1 << 5 is {}", 1u32 << 5);
    println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);

    // Use underscores to improve readability!
    println!("One million is written as {}", 1_000_000u32);
}
Tags Rust Data Types Rust Literals Rust Operators Facebook X
Comments 0
Similar posts
  1. Tuples in Rust - a collection of values of different types
    298
  2. Rust Data Type - What is mutable?
    256
  3. Arrays and Slices in Rust
    341
  4. Rust Custom Data Types - struct, enum, use, constants
    299
  5. Rust Data Type Operations - Casting, Literals, Inference, Aliasing
    326
  6. Question(?) Operator in Rust
    572
  1. Rust Error Handling
    349
  2. Rust Macro Rules
    1,521
  3. Rust mechanism for defining shared behavior across different types - Traits
    1,771
  4. Rust Scoping Rules - only accessible within the block of code
    1,073
  5. Rust Generics - Generalizing types and functionalities
    1,356
  6. Rust Attributes for conditional compilation, crate, lints, mark functions, macros
    317
  7. Rust Package Management Tool - cargo
    307
  8. Rust compilation unit - Crates
    254
  9. Rust Modules - Visivility, Struct Visibility, Use Declaration, super and self, File Hierarchy
    1,908
  10. Rust functions - Methods, Closures, Capturing
    1,210
  11. Rust Flow of Control - if/else, loop. while, for and range, match, if let, let-else, while let
    339
  12. Rust Expressions
    933
  13. Rust Conversion - From, Into, TryFrom, TryInto
    1,320
  14. gRPC library for Rust
    345
  15. Variable Bindings in Rust
    975
  16. Rust Code Testing Environment without installing anything on your computer
    327
  17. Rust Thread Example
    346
  18. How do I setup Rust development environment?
    369
  19. Rust - System Programming Language invented by Graydon Hoare
    281
  20. Why America's Biggest Tech Companies Are Betting on Rust?
    290
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    29
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  4. How do I create animated background for Google Meet?
    22,861
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  8. Array of arguments passed to script in PHP
    97
  9. Invalid Auction Storms in Ad Tech
    106
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  11. The Two Clocks of Ad Server Architecture
    77
  12. Frequency Cap Architecture Overview
    526
  13. Collecting Proof-of-Play from 100,000 Screens
    152
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  15. What is RedisVL?
    114
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  17. TPU & GPU Cluster Management Tools
    257
  18. The Concept of Scaffolding in AI Agents
    244
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  23. Machine Learning: Training vs. Ranking
    229
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    671
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    200
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232