[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-rust-variables-mutability-guide-all--*":3,"academy-blog-translations-ypjquqgb7zjvn1v":91},{"data":4,"page":90,"perPage":90,"totalItems":90,"totalPages":90},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":83,"keywords":84,"locale":59,"published_at":85,"scheduled_at":13,"school_blog":81,"short_description":86,"slug":87,"status":79,"title":88,"updated":89,"updated_by":13,"views":82},"Rust programming code snippet showing variable declaration","sclblg987654321","school_blog_translations","\u003Cp>After getting to know Cargo and creating our first project in EP.3, it’s finally time to start writing actual code! The most fundamental starting point for any programming language is \u003Cstrong>\"Variables.\"\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>However, in Rust, variables are handled with a level of \"strictness\" that might surprise you if you're coming from other languages. Let’s dive in.\u003C\u002Fp>\u003Ch2>1. Immutable by Default: The Safety First Approach\u003C\u002Fh2>\u003Cp>In most programming languages, once you declare a variable, you can change its value whenever you like. But in Rust, \u003Cstrong>\"all variables are immutable (unchangeable) by default.\"\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Take a look at this code:\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let x = 5;\n    println!(\"The value of x is: {x}\");\n    \n    x = 6; \u002F\u002F ❌ This line will cause a Compiler Error!\n    println!(\"The value of x is: {x}\");\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>Why did Rust designers choose this?\u003C\u002Fh3>\u003Cp>The primary reasons are \u003Cstrong>Safety\u003C\u002Fstrong> and \u003Cstrong>Concurrency\u003C\u002Fstrong>:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Fewer Bugs:\u003C\u002Fstrong> If you assume a value stays the same but it changes elsewhere in your program, it leads to hard-to-track bugs. Rust prevents this at compile time.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Readability:\u003C\u002Fstrong> When you see \u003Ccode>let x = ...\u003C\u002Fcode>, you can be certain that \u003Ccode>x\u003C\u002Fcode> will remain constant throughout its scope.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Performance:\u003C\u002Fstrong> The compiler can optimize the code more effectively when it knows a value will not change.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>2. Using \u003Ccode>mut\u003C\u002Fcode> to Allow Changes\u003C\u002Fh2>\u003Cp>Of course, in real-world applications, we often need variables that can change (such as loop counters or application states). Rust provides the \u003Ccode>mut\u003C\u002Fcode> keyword (short for mutable) for this purpose.\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let mut x = 5; \u002F\u002F ✅ Adding 'mut' makes it mutable\n    println!(\"The value of x is: {x}\");\n    \n    x = 6; \u002F\u002F ✅ Now we can change the value!\n    println!(\"The value of x is: {x}\");\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>Adding \u003Ccode>mut\u003C\u002Fcode> is a clear signal to both the compiler and your teammates: \u003Cstrong>\"This variable is intended to be changed.\"\u003C\u002Fstrong> This clarity significantly improves code quality.\u003C\u002Fp>\u003Ch2>3. Constants vs. Immutable Variables\u003C\u002Fh2>\u003Cp>You might be wondering, \"If immutable variables can't change, how are they different from Constants?\"\u003C\u002Fp>\u003Ctable style=\"min-width: 75px;\">\u003Ccolgroup>\u003Ccol style=\"min-width: 25px;\">\u003Ccol style=\"min-width: 25px;\">\u003Ccol style=\"min-width: 25px;\">\u003C\u002Fcolgroup>\u003Ctbody>\u003Ctr>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Feature\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Variables (Immutable)\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Constants (const)\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Use of \u003Ccode>mut\u003C\u002Fcode>\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>Can use \u003Ccode>mut\u003C\u002Fcode> to become mutable\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Never\u003C\u002Fstrong> allowed to be mutable\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Declaration\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>Uses \u003Ccode>let\u003C\u002Fcode>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>Uses \u003Ccode>const\u003C\u002Fcode> and \u003Cstrong>must\u003C\u002Fstrong> include a Type\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>Value Source\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>Can be determined at Runtime\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>Must be a constant known at Compile-time\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>\u003Cstrong>Example of a Constant:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>4. Shadowing: Re-declaring the Same Name\u003C\u002Fh2>\u003Cp>One of Rust's coolest features is \u003Cstrong>Shadowing\u003C\u002Fstrong>. This allows you to declare a new variable using the same name as a previous one.\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let x = 5;\n    let x = x + 1; \u002F\u002F Shadowing (First time)\n    \n    {\n        let x = x * 2; \u002F\u002F Shadowing within this specific scope\n        println!(\"The value of x in the inner scope is: {x}\"); \u002F\u002F Result: 12\n    }\n\n    println!(\"The value of x is: {x}\"); \u002F\u002F Result: 6\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>How is Shadowing different from \u003Ccode>mut\u003C\u002Fcode>?\u003C\u002Fh3>\u003Col>\u003Cli>\u003Cp>\u003Cstrong>New Identity:\u003C\u002Fstrong> Shadowing creates a \u003Cem>new\u003C\u002Fem> variable (the old one is hidden). \u003Ccode>mut\u003C\u002Fcode> simply updates the \u003Cem>value\u003C\u002Fem> of the existing variable.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Type Flexibility:\u003C\u002Fstrong> With shadowing, we can change the \u003Cstrong>Data Type\u003C\u002Fstrong> while keeping the same name (e.g., transforming a \u003Ccode>String\u003C\u002Fcode> into a \u003Ccode>Number\u003C\u002Fcode>).\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch2>Summary\u003C\u002Fh2>\u003Cp>In Rust, choosing between Immutable, \u003Ccode>mut\u003C\u002Fcode>, or Shadowing helps you write code that is safe, predictable, and highly efficient in memory management.\u003C\u002Fp>\u003Cp>In \u003Cstrong>EP.5\u003C\u002Fstrong>, we will explore the perfect companion to variables: \u003Cstrong>Basic Data Types\u003C\u002Fstrong>. We’ll take a deep dive into how Rust handles numbers, characters, and groups of data (Scalar and Compound types). Don't miss it!\u003C\u002Fp>\u003Cp>\u003Cstrong>Happy Coding with Rust\u003C\u002Fstrong>\u003C\u002Fp>\u003Ch2>🎯 Stay Tuned with Superdev Academy:\u003C\u002Fh2>\u003Cp>Don't miss out on deep-dive technical articles and updates. Follow us on all platforms:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>🔵 \u003Cstrong>Facebook:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">Superdev Academy Thailand\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🎬 \u003Cstrong>YouTube:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\">Superdev Academy Channel\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>📸 \u003Cstrong>Instagram:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\">@superdevacademy\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🎬 \u003Cstrong>TikTok:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">@superdevacademy\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🌐 \u003Cstrong>Website:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"http:\u002F\u002Fsuperdevacademy.com\">superdevacademy.com\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","8uh5rmfhs7l_9iqxjg84ex.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fhpc9fj23gw5fb4x\u002F8uh5rmfhs7l_9iqxjg84ex.png","2026-04-03 10:21:08.575Z","",{"keywords":15,"locale":53,"school_blog":63},[16,23,28,33,38,43,48],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:08.348Z","ux2m77tsy8v19bg","Rust Programming","2026-04-10 16:07:25.602Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-23 03:08:56.729Z","ttp78s7jayewgl4","Rust","2026-04-10 16:14:44.997Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:26:57.621Z","yz0psh771cme97h","Variables","2026-04-10 16:07:36.688Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-04-03 10:18:18.197Z","pgvg42yxe6j3ovv","Mutability","2026-04-10 16:14:46.623Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-04-03 10:20:41.867Z","py0zemi2rikoayq","Immutable by default","2026-04-10 16:14:46.915Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-23 03:26:43.307Z","pf009wnev852vvt","Rust tutorial","2026-04-10 16:14:45.235Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:47:43.833Z","1wvn4ycus88oilx","Software Development","2026-04-10 16:13:33.058Z",{"code":54,"collectionId":55,"collectionName":56,"created":57,"flag":58,"id":59,"is_default":60,"label":61,"updated":62},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":64,"collectionId":65,"collectionName":66,"expand":67,"id":81,"views":82},"mfqe235iv6x1or8","pbc_2105096300","school_blogs",{"category":68},{"blogIds":69,"collectionId":70,"collectionName":71,"created":72,"created_by":13,"id":64,"image":73,"image_alt":13,"image_path":74,"label":75,"name":76,"priority":77,"publish_at":78,"scheduled_at":13,"status":79,"updated":80,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-16 04:40:07.104Z","47vt5s1gglj_tz0b90vnge.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fmfqe235iv6x1or8\u002F47vt5s1gglj_tz0b90vnge.png",{"en":76,"th":76},"Rust The Series",2,"2026-03-16 04:40:09.199Z","published","2026-04-25 02:32:15.619Z","ypjquqgb7zjvn1v",109,"hpc9fj23gw5fb4x",[20,25,30,35,40,45,50],"2026-04-03 11:13:33.445Z","Learn why Rust variables are immutable by default and how to use the 'mut' keyword for safe memory management. Master the concepts of Constants and Shadowing in Rust.","rust-variables-mutability-guide","Variables & Mutability: Why Immutable by Default? | Rust The Series EP.4","2026-04-22 07:11:54.418Z",1,{"th":92,"en":87},"rust-series-ep4-variables-and-mutability"]