Rust Data Type - What is mutable?

CK CK 3788 
Created at
Updated at  
256 0 0

Rust provides access to a wide variety of primitives. A sample includes:

Scalar Types

  • Signed integers: i8, i16, i32, i64, i128 and isize (pointer size)
  • Unsigned integers: u8, u16, u32, u64, u128 and usize (pointer size)
  • Floating point: f32, f64
  • char Unicode scalar values like 'a', 'α' and '∞' (4 bytes each)
  • bool either true or false
  • The unit type (), whose only possible value is an empty tuple: ()

Despite the value of a unit type being a tuple, it is not considered a compound type because it does not contain multiple values.

Compound Types

  • Arrays like [1, 2, 3]
  • Tuples like (1, true)

Variables can always be type annotated. Numbers may additionally be annotated via a suffix or by default. Integers default to i32 and floats to f64. Note that Rust can also infer types from context.

fn main() {
    // Variables can be type annotated.
    let logical: bool = true;

    let a_float: f64 = 1.0;  // Regular annotation
    let an_integer   = 5i32; // Suffix annotation

    // Or a default will be used.
    let default_float   = 3.0; // `f64`
    let default_integer = 7;   // `i32`

    // A type can also be inferred from context.
    let mut inferred_type = 12; // Type i64 is inferred from another line.
    inferred_type = 4294967296i64;

    // A mutable variable's value can be changed.
    let mut mutable = 12; // Mutable `i32`
    mutable = 21;

    // Error! The type of a variable can't be changed.
    mutable = true;

    // Variables can be overwritten with shadowing.
    let mutable = true;

    /* Compound types - Array and Tuple */

    // Array signature consists of Type T and length as [T; length].
    let my_array: [i32; 5] = [1, 2, 3, 4, 5];

    // Tuple is a collection of values of different types 
    // and is constructed using parentheses ().
    let my_tuple = (5u32, 1u8, true, -5.04f32);
}


In Rust, mut is a keyword used to declare a variable as mutable, meaning its value can be changed after it's initialized. Without mut, a variable is immutable by default, meaning its value remains constant throughout its lifetime. Using mut allows modification of the variable's value, but this must be explicitly stated to ensure Rust's strong focus on memory safety and preventing data races.
 

Tags Compound Types Rust Data Types Rust Primitive Data Types Scalar Types mut Facebook X
Comments 0
Similar posts
  1. Literals and operators in Rust
    293
  2. Tuples in Rust - a collection of values of different types
    296
  3. Arrays and Slices in Rust
    333
  4. Rust Custom Data Types - struct, enum, use, constants
    296
  5. Rust Data Type Operations - Casting, Literals, Inference, Aliasing
    325
  1. Rust Attributes for conditional compilation, crate, lints, mark functions, macros
    309
  2. Rust Package Management Tool - cargo
    305
  3. Rust compilation unit - Crates
    249
  4. Rust Modules - Visivility, Struct Visibility, Use Declaration, super and self, File Hierarchy
    1,881
  5. Rust functions - Methods, Closures, Capturing
    1,191
  6. Rust Flow of Control - if/else, loop. while, for and range, match, if let, let-else, while let
    331
  7. Rust Expressions
    918
  8. Rust Conversion - From, Into, TryFrom, TryInto
    1,296
  9. gRPC library for Rust
    334
  10. Rust Data Type Operations - Casting, Literals, Inference, Aliasing
    325
  11. Variable Bindings in Rust
    957
  12. Rust Custom Data Types - struct, enum, use, constants
    296
  13. Arrays and Slices in Rust
    333
  14. Tuples in Rust - a collection of values of different types
    296
  15. Literals and operators in Rust
    293
  16. Rust Code Testing Environment without installing anything on your computer
    321
  17. Rust Thread Example
    344
  18. How do I setup Rust development environment?
    366
  19. Rust - System Programming Language invented by Graydon Hoare
    277
  20. Why America's Biggest Tech Companies Are Betting on Rust?
    284
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,703
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    197
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    508
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    575
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    252
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    326
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252