เรียน Rust ผ่าน Rustlings วันที่ 3
สวัสดีครับ ต่อจากบล็อกที่แล้ว กับการเรียน Rust ผ่าน Rustlings ตอนที่ 3 นะครับ

Exercises ของ Rustlings วันนี้คือ
- Primitive Types จะตรงกับ Data Types และ The Slice Type
- Vectors จะตรงกับ Storing Lists of Values with Vectors , iter_mut และ map
สรุปจาก Rustlings แต่ละ Exercises และการอ่านเพิ่มเติมจาก Rust Book
Primitive Types
หลักๆ แบ่งออกเป็น 2 แบบคือ
- Scalar Type คือพวก integers, float, number, boolean และก็ char
- Compound Type คือ Tuple กับ Array
Scalar Type
Integer จะมี signed และ unsigned เช่น i32
= signed 32 bit , u32
= unsigned 32-bit
let d = 98_222 // decimal
let h = 0xff // hex
let o = 0o77 // octal
let b = 0b1111_0000 // binary
let bb = b'A' // bytes
Floating-point จะมี f32
และ f64
fn main() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
}
Boolean type ( bool
) มีค่าได้ true
กับ false
fn main() {
let t = true;
let f: bool = false; // with explicit type annotation
}
Character type ใช้ single quote ถ้า String จะใช้ double quote
fn main() {
let c = 'z';
let z: char = 'ℤ'; // with explicit type annotation
let heart_eyed_cat = '😻';
}
Compound Type
Tuple คือกลุ่มข้อมูล คล้ายๆ array แต่ว่าในกลุ่มเป็นคนละ type กันได้
- fixed length - เมื่อ declare แล้ว ไม่สามารถ เพิ่ม ลดได้
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
- destructure tuple ทำคล้ายๆ JavaScript (เปลี่ยนจาก
{}
เป็น()
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
}
- เข้าถึง element ของ tuple ผ่าน index ได้ด้วยการใช้
.
(อย่าง JS ใช้[index]
- index แรก คือ 0
- tuple ที่ไม่มีค่า เราเรียกว่า unit หรือก็คือ
()
นั่นเอง (function ที่ไม่ reutrn อะไร)
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
Array คล้ายๆกับภาษาอื่นๆ เป็นกลุ่มข้อมูล ในภาษา Rust ข้อมูลใน array ต้องเป็น type เดียวกัน และก็ fixed length
fn main() {
let a = [1, 2, 3, 4, 5];
}
- Array มีประโยชน์ ถ้าเรารู้จำนวน element (ไม่งั้นไปใช้ Vector)
- ตัวอย่างการกำหนด array ขนาด 5 และมี type เป็น
i32
let a: [i32; 5] = [1, 2, 3, 4, 5];
// initial array ด้วย value เดียวกัน จำนวน 5 ครั้ง
let a = [3; 5];
- เข้าถึง element ของ array ด้วย index (start index = 0)
fn main() {
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
}
Vectors
Vector เหมือนกับ Array เพียงแต่ว่า มันสามารถ เพิ่ม ลด ขนาดได้
- สร้าง empty vector (ต้องใส่ type ให้ด้วย)
let v: Vec<i32> = Vec::new();
- Vector รองรับ Generic Type ด้วย
- สามารถ initial vector ด้วย macro
vec!
ตัว Rust จะทำการ infer ตามค่าให้
let v = vec![1, 2, 3];
// Rust รู้ว่า v = Vec<i32>
- ใช้
push()
เพื่อเพิ่มค่าใน vector (ต้องกำหนดmut
ให้กับ vector เพราะเรากำลังจะแก้ไขข้อมูล)
let mut v = Vec::new();
v.push(5);
v.push(6);
- เข้าถึง element ของ Vector ได้ 2 วิธีคือ index (start index 0) หรือ
get()
method
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {third}");
let third: Option<&i32> = v.get(2);
match third {
Some(third) => println!("The third element is {third}"),
None => println!("There is no third element."),
}
- แบบ index เราใช้
&v
หมายถึง reference ถึงตัวแปร v - แบบ
v.get(2)
จะ return ค่าเป็นOption<&i32>
- การ iterate element ใน vector
let v = vec![100, 32, 57];
for i in &v {
println!("{i}");
}
- iterate แล้วเปลี่ยนค่า (กำหนด
mut
)
let mut v = vec![100, 32, 57];
for i in &mut v {
*i += 50;
}
- หรือใช้
iter_mut
let x = &mut [1, 2, 4];
for elem in x.iter_mut() {
*elem += 2;
}
assert_eq!(x, &[3, 4, 6]);
*
คือ dereference operator
จบวันที่ 3 🎉