🦉
Programming Notes
  • My Programming Notes
  • CKA Exam Preparation
    • Certified Kubernetes Administrator
    • Setup Minikube
    • Network Design Principles
    • Role-Based Access Control (RBAC)
    • Namespace
    • Resource Quota
    • Pod
    • Deployment
    • Deployment: Rollout
    • ConfigMap
    • Service
    • Service: kubectl expose
    • Pod: Resources Management
    • Pod & Container: Quality of Service Class
    • Pod & Container: Probes
    • Limit Range
    • Scaling: Manual
    • Scaling: Horizontal Pod Autoscaler
    • Persistent Volume & Claim
    • Secret
    • Ingress: Routing
    • Ingress: TLS
    • Ingress: Rate Limit
    • Ingress: Basic Auth
    • Ingress: CRD (Custom Resource Definition)
    • Job
    • CronJob
    • Mutli-Node Cluster
  • Golang
    • Generics
    • Context
    • Goroutines and Channels in Go
    • Goroutine: Concurrency vs Parallelism
    • Goroutine: Performance & Tradeoffs
    • JSON: omitzero
  • Rust
    • Arrays & Slices
    • Closures
    • Generics & Traits
    • Iterators
    • Run Code Simultaneously
    • String vs &str
    • Tests
    • Rustlings Exercises
      • Variables
      • Functions
      • If
      • Primitive Types
      • Vectors
      • Move Semantics
      • Structs
      • Enums and Matching Pattern
      • Strings
      • Modules
      • Hashmaps
      • Options
      • Error Handling
      • Generics
      • Traits
      • Lifetimes
      • Tests
      • Iterators
      • Smart Pointers
      • Threads
      • Macros
      • Quiz 1
      • Quiz 2
      • Quiz 3
  • Software Engineering
    • CAP Theorem
    • Circuit Breaker
    • Decoupling
    • GraphQL: Query Caching
    • HMAC Signature
    • Idempotency
    • Monolith VS Microservice
    • OWASP Top 10 2021
    • PCI DSS
    • PostgreSQL: Partitioning
    • PostgreSQL: Replication
    • Protobuf & gRPC
    • Redis: Streams
    • Resource Scaling
    • Signed URL
    • SOLID
    • Stack VS Heap
    • Stateful VS Stateless
  • Site Reliability Engineering
    • Chaos Engineering
    • Distributed Tracing
    • Kubernetes (k8s)
    • SLA, SLO, and SLI Metrics
    • Site Reliability Engineer
  • Others
    • FFMPEG Cheat sheet
Powered by GitBook
On this page
  • variables1.rs
  • variables2.rs
  • variables3.rs
  • variables4.rs
  • variables5.rs
  • variables6.rs
  1. Rust
  2. Rustlings Exercises

Variables

PreviousRustlings ExercisesNextFunctions

Last updated 4 months ago

  • Define variables using let statement.

  • Variables need initialization. By default variables are immutable unless explicitly declared using mut.

  • We can declare a new variable with the same name as a previous variable, this is called shadowing.

  • Constants are values that are bound to a name and are not allowed to change.

  • Constants are always immutable.

  • The type of the value MUST be annotated.

  • Reference:

variables1.rs

fn main() {
    // Adding let
    let x = 5;

    println!("x has the value {x}");
}
  • let statement:

    A let statement introduces a new set of variables, given by a pattern. The pattern is followed optionally by a type annotation and then either ends, or is followed by an initializer expression plus an optional else block.

    Reference: .

  • In this exercise we only need to add let without needing to include any type explicitly, the compiler will infer the type.

variables2.rs

fn main() {
    // Initialize the variable x to any integer
    let x = 5;

    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}
  • Variables need initialization.

  • But you don't need to initialize it on the same line, you can do something like this too:

    let x;
    x = 5;

variables3.rs

fn main() {
    // Initialize the variable x to any integer
    let x: i32 = 10;

    println!("Number {x}");
}
  • Same as previous exercise, we just need to initialize the variable x.

variables4.rs

fn main() {
    // Add mutable
    let mut x = 3;
    println!("Number {x}");

    x = 5; // Don't change this line
    println!("Number {x}");
}
  • By default variables are immutable, so we need to explicitly declare mutability by adding mut.

variables5.rs

fn main() {
    let number = "T-H-R-E-E"; // Don't change this line
    println!("Spell a number: {}", number);

    // Redeclare the number variable below
    let number = 3;
    println!("Number plus two is: {}", number + 2);
}
  • We can declare a new variable with the same name as a previous variable

  • In Rust this is called shadowing.

variables6.rs

// constant need type
const NUMBER: i32 = 3;

fn main() {
    println!("Number: {NUMBER}");
}
  • Constants are values that are bound to a name and are not allowed to change.

  • Constants are always immutable.

  • The type of the value MUST be annotated, so adding i32 will fix the code.

Reference: .

You can read more about this topic here: .

Reference:

Variables and Mutability
https://doc.rust-lang.org/reference/statements.html#let-statements
https://doc.rust-lang.org/reference/variables.html
https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#constants