[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-rust-series-ep4-variables-and-mutability-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","sclblg987654321","school_blog_translations","\u003Cp>หลังจากที่เราได้ทำความรู้จักกับ Cargo และลองสร้างโปรเจกต์แรกกันไปแล้วใน EP.3 ถึงเวลาที่เราจะมาเริ่มเขียนโค้ดจริงๆ กันเสียที! และจุดเริ่มต้นที่สำคัญที่สุดของทุกภาษาโปรแกรมก็คือ \u003Cstrong>\"ตัวแปร\" (Variables)\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>แต่สำหรับ Rust นั้น เรื่องของตัวแปรมีความพิเศษและ \"เข้มงวด\" กว่าภาษาอื่นที่คุณอาจเคยเจอมาครับ\u003C\u002Fp>\u003Ch2>1. Immutable by Default: ค่าเริ่มต้นที่เปลี่ยนไม่ได้\u003C\u002Fh2>\u003Cp>ในภาษาโปรแกรมส่วนใหญ่ เมื่อเราประกาศตัวแปร เรามักจะเปลี่ยนค่ามันได้ตลอดเวลา แต่ใน Rust \u003Cstrong>\"ตัวแปรทุกตัวจะเป็น Immutable (เปลี่ยนค่าไม่ได้) โดยค่าเริ่มต้น\"\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>ลองดูตัวอย่างโค้ดนี้ครับ:\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\r\n    let x = 5;\r\n    println!(\"The value of x is: {x}\");\r\n    \r\n    x = 6; \u002F\u002F ❌ บรรทัดนี้จะทำให้เกิด Error ทันที!\r\n    println!(\"The value of x is: {x}\");\r\n}\r\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>ทำไม Rust ถึงออกแบบมาแบบนี้?\u003C\u002Fh3>\u003Cp>เหตุผลหลักคือ \u003Cstrong>Safety (ความปลอดภัย)\u003C\u002Fstrong> และ \u003Cstrong>Concurrency (การทำงานขนาน)\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>ลด Bug:\u003C\u002Fstrong> ถ้าเรากำหนดค่าบางอย่างไว้แล้วเผลอไปเปลี่ยนมันในส่วนอื่นของโปรแกรมโดยไม่ตั้งใจ อาจทำให้เกิดข้อผิดพลาดที่หายาก\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>อ่านง่าย:\u003C\u002Fstrong> เมื่อคุณเห็น \u003Ccode>let x = ...\u003C\u002Fcode> คุณจะมั่นใจได้เลยว่าค่าของ \u003Ccode>x\u003C\u002Fcode> จะไม่เปลี่ยนไปแน่นอนตลอด Scope นั้น\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>ประสิทธิภาพ:\u003C\u002Fstrong> Compiler สามารถ Optimize โค้ดได้ดีขึ้นเพราะรู้ว่าค่านั้นคงที่\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>2. การใช้ \u003Ccode>mut\u003C\u002Fcode> เพื่อทำให้เปลี่ยนค่าได้\u003C\u002Fh2>\u003Cp>แน่นอนว่าในชีวิตจริง เราจำเป็นต้องมีตัวแปรที่เปลี่ยนค่าได้ (เช่น ตัวนับ Loop หรือสถานะของโปรแกรม) Rust จึงให้คีย์เวิร์ด \u003Ccode>mut\u003C\u002Fcode> (มาจาก mutable) มาครับ\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\r\n    let mut x = 5; \u002F\u002F ✅ เพิ่ม mut เข้าไป\r\n    println!(\"The value of x is: {x}\");\r\n    \r\n    x = 6; \u002F\u002F ✅ เปลี่ยนค่าได้แล้ว!\r\n    println!(\"The value of x is: {x}\");\r\n}\r\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>การใส่ \u003Ccode>mut\u003C\u002Fcode> เป็นการบอกทั้ง Compiler และเพื่อนร่วมทีมว่า \u003Cstrong>\"ตัวแปรนี้ตั้งใจให้เปลี่ยนค่าได้นะ\"\u003C\u002Fstrong> ซึ่งเป็นการสื่อสารที่ชัดเจนมากในเชิง Code Quality\u003C\u002Fp>\u003Ch2>3. Constants (ค่าคงที่)\u003C\u002Fh2>\u003Cp>หลายคนอาจจะสงสัยว่า \"ถ้า Immutable มันเปลี่ยนค่าไม่ได้ แล้วมันต่างจาก 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>คุณสมบัติ\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>การใช้ mut\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>ใช้ \u003Ccode>mut\u003C\u002Fcode> เพื่อเปลี่ยนค่าได้\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>ห้าม\u003C\u002Fstrong>ใช้ \u003Ccode>mut\u003C\u002Fcode> โดยเด็ดขาด\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>การประกาศ\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>ใช้ \u003Ccode>let\u003C\u002Fcode>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>ใช้ \u003Ccode>const\u003C\u002Fcode> และ\u003Cstrong>ต้องระบุ Type\u003C\u002Fstrong> เสมอ\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>\u003Cstrong>ค่าที่ใช้\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>กำหนดค่าจาก Runtime ได้\u003C\u002Fp>\u003C\u002Ftd>\u003Ctd colspan=\"1\" rowspan=\"1\">\u003Cp>ต้องเป็นค่าที่รู้ตั้งแต่ตอน Compile เท่านั้น\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>\u003Cstrong>ตัวอย่างการใช้ Const:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;\r\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>4. Shadowing: การประกาศตัวแปรทับชื่อเดิม\u003C\u002Fh2>\u003Cp>นี่คือฟีเจอร์เด็ดของ Rust ครับ เราสามารถประกาศตัวแปรใหม่โดยใช้ชื่อเดิมได้ เรียกว่า \u003Cstrong>Shadowing\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\r\n    let x = 5;\r\n    let x = x + 1; \u002F\u002F Shadowing ครั้งที่ 1\r\n    \r\n    {\r\n        let x = x * 2; \u002F\u002F Shadowing เฉพาะใน Scope นี้\r\n        println!(\"The value of x in the inner scope is: {x}\"); \u002F\u002F 12\r\n    }\r\n\r\n    println!(\"The value of x is: {x}\"); \u002F\u002F 6\r\n}\r\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>\u003Cstrong>Shadowing ต่างจาก \u003Ccode>mut\u003C\u002Fcode> อย่างไร?\u003C\u002Fstrong>\u003C\u002Fp>\u003Col>\u003Cli>\u003Cp>Shadowing คือการสร้างตัวแปรใหม่ (ค่าเดิมจะถูกบดบังไป) แต่ \u003Ccode>mut\u003C\u002Fcode> คือการเปลี่ยนค่าในตัวแปรเดิม\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>เราสามารถเปลี่ยน \u003Cstrong>Data Type\u003C\u002Fstrong> ได้ด้วย Shadowing เช่น เปลี่ยนจาก String เป็น Number โดยใช้ชื่อเดิมได้เลย\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch2>สรุป\u003C\u002Fh2>\u003Cp>ใน Rust การตัดสินใจเลือกระหว่าง Immutable, \u003Ccode>mut\u003C\u002Fcode> หรือ Shadowing จะช่วยให้โค้ดของคุณปลอดภัยและจัดการหน่วยความจำได้ดีเยี่ยมครับ\u003C\u002Fp>\u003Cp>ใน \u003Cstrong>EP.5\u003C\u002Fstrong> เราจะมาเจาะลึกสิ่งที่คู่กับตัวแปรเสมอ นั่นคือ \u003Cstrong>Data Types พื้นฐาน\u003C\u002Fstrong> เราจะไปดูกันว่า Rust จัดการกับตัวเลข, ตัวอักษร และข้อมูลแบบกลุ่ม (Compound types) อย่างไรบ้าง ห้ามพลาดครับ!\u003C\u002Fp>\u003Cp>\u003Cstrong>Happy Coding กับ Rust นะครับ\u003C\u002Fstrong>\u003C\u002Fp>\u003Ch2>🎯 ติดตามความรู้สาย Dev แบบสุดจัดได้ที่:\u003C\u002Fh2>\u003Cp>ไม่อยากพลาดบทความเทคนิคเชิงลึกและอัปเดตใหม่ๆ จากเรา ติดตาม \u003Cstrong>Superdev Academy\u003C\u002Fstrong> ได้ทุกช่องทางที่นี่ครับ:\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> (Tips &amp; Tricks ฉบับย่อยง่าย)\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>","7fnmzu2rf8s_crmtwh4g4f.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Ff4jigxremr1yfr7\u002F7fnmzu2rf8s_crmtwh4g4f.png","2026-04-03 10:19:09.480Z","",{"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-16 05:10:32.092Z","s8r1st4glsmm7yl","ภาษา Rust","2026-04-10 16:14:41.900Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:27:07.652Z","32ul6fb24qv6ffa","ตัวแปร","2026-04-10 16:07:38.429Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-04-03 10:18:18.197Z","pgvg42yxe6j3ovv","Mutability","2026-04-10 16:14:46.623Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-04-03 10:18:27.323Z","97ya64hf89xb4vw","Immutable","2026-04-10 16:14:46.750Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-23 03:08:18.875Z","pu1hva5rmnc4672","สอน Rust","2026-04-10 16:14:44.507Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-04-03 10:18:39.967Z","f1rjr0z8i71w4qu","Shadowing","2026-04-10 16:14:46.815Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:44:11.146Z","gms2qr4xg6qv65e","Superdev Academy","2026-04-10 16:12:41.207Z",{"code":54,"collectionId":55,"collectionName":56,"created":57,"flag":58,"id":59,"is_default":60,"label":61,"updated":62},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"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,"f4jigxremr1yfr7",[20,25,30,35,40,45,50],"2026-04-03 11:13:28.919Z","เจาะลึกหัวใจของ Rust ทำไมตัวแปรถึงเปลี่ยนค่าไม่ได้ (Immutable) โดยค่าเริ่มต้น? เรียนรู้วิธีการใช้ mut, ข้อแตกต่างของ Constants และเทคนิค Shadowing เพื่อการจัดการหน่วยความจำที่ปลอดภัย","rust-series-ep4-variables-and-mutability","ตัวแปรและการเปลี่ยนแปลงใน Rust: ทำไมต้อง Immutable? | Rust The Series EP.4","2026-04-22 07:11:54.331Z",1,{"th":87,"en":92},"rust-variables-mutability-guide"]