Data Macros
Macros สำหรับ data structures และ collections
vec! Macro
Section titled “vec! Macro”fn main() { // Create Vec with values let v1 = vec![1, 2, 3, 4, 5]; println!("v1: {:?}", v1);
// Repeat value let v2 = vec![0; 5]; // [0, 0, 0, 0, 0] println!("v2: {:?}", v2);
// Complex types let strings = vec!["hello".to_string(), "world".to_string()]; println!("strings: {:?}", strings);
// Nested let matrix = vec![ vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9], ]; println!("matrix: {:?}", matrix);
// With expressions let computed = vec![1 + 1, 2 * 2, 3 * 3]; println!("computed: {:?}", computed);
// Empty Vec with type let empty: Vec<i32> = vec![]; println!("empty: {:?}", empty);
// Capacity hint (not size) let mut with_cap = Vec::with_capacity(100); with_cap.extend(vec![1, 2, 3]); println!("with_cap len: {}, cap: {}", with_cap.len(), with_cap.capacity());}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google