Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quiz: บทที่ 5 - Ownership

คำถามที่ 1

กฎ Ownership ข้อใดถูกต้อง?

A. ค่าหนึ่งมีได้หลาย owner
B. ค่าหนึ่งมีได้ owner เดียว
C. Owner ไม่สามารถ transfer ได้
D. ค่าไม่ถูก drop เมื่อ owner ออกจาก scope

ดูเฉลย

B. ค่าหนึ่งมีได้ owner เดียว

กฎ 3 ข้อ:

  1. ทุกค่ามี owner หนึ่งเดียว
  2. มีได้แค่ owner เดียว
  3. เมื่อ owner ออกจาก scope ค่าถูก drop

คำถามที่ 2

code นี้ถูกหรือผิด?

let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);

A. ถูก
B. ผิด - s1 ถูก move
C. ผิด - syntax error
D. ผิด - type mismatch

ดูเฉลย

B. ผิด - s1 ถูก move

s1 ถูก move ไป s2 จึงใช้ไม่ได้


คำถามที่ 3

& หมายถึงอะไร?

A. Pointer
B. Reference (borrow)
C. Copy
D. Move

ดูเฉลย

B. Reference (borrow)

let s = String::from("hello");
let r = &s; // borrow แบบ immutable

คำถามที่ 4

immutable reference มีได้กี่อัน?

A. 1 อัน
B. หลายอัน
C. ขึ้นอยู่กับ type
D. 0 อัน

ดูเฉลย

B. หลายอัน

let r1 = &s;
let r2 = &s; // ✅ OK

แต่ mutable reference มีได้ 1 อัน


คำถามที่ 5

Slice &s[0..5] ทำอะไร?

A. Copy ค่า
B. สร้าง reference ไปยังส่วนหนึ่ง
C. ลบข้อมูล
D. สร้าง String ใหม่

ดูเฉลย

B. สร้าง reference ไปยังส่วนหนึ่ง

Slice คือ view เข้าไปในข้อมูลเดิม ไม่ copy


👉 Quiz บทที่ 6