let c: char = 'f'; // 字符 let string_c: &str = "ace"; // 字符串切片 let string_s = String::from("hello"); // 字符串
这里的string_c为“字符串切片”
元组
定义和访问元组
1 2 3 4
let x: (i32, f64, u8) = (500, 6.4, 1); let five_hundred = x.0; let six_point_four = x.1; let one = x.2;
数组
1 2 3 4 5
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // 声明一个全0数组,数据成员个数为5 let bytes = [0; 5]; // 使用数组成员 let first = days[0];
// 赋值 let click = MouseClick { x: 100, y: 250 }; let keys = KeyPress(String::from("Ctrl+"), 'N');
let we_load = WebEvent::WELoad(true); // Set the WEClick variant to use the data in the click struct let we_click = WebEvent::WEClick(click); // Set the WEKeys variant to use the data in the keys tuple let we_key = WebEvent::WEKeys(keys);
// Access a file at a specified path // --------------------------------- // - Pass variable to `file` variable on success, or // - Return from function early if there's an error letmut file: File = match File::open(path) { // Corrected code: Pass variable to `file` variable on success Ok(file_handle) => file_handle, // Corrected code: Return from function early if there's an error Err(io_error) => returnErr(io_error), };
// Read file contents into `String` variable with `read_to_string` // --------------------------------- // Success path is already filled in // Return from the function early if it is an error match file.read_to_string(&mut string) { Ok(_) => (), // Corrected code: Return from function early if there's an error Err(io_error) => returnErr(io_error), };
// Corrected code: Return `string` variable as expected by function signature Ok(string) }
$ valgrind ./test ==4597== Memcheck, a memory error detector ==4597== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==4597== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==4597== Command: ./test ==4597== ==4597== Invalid write of size 4 ==4597== at 0x10916B: f (test.c:6) ==4597== by 0x109180: main (test.c:11) ==4597== Address 0x4a47068 is 0 bytes after a block of size 40 alloc'd ==4597== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==4597== by 0x10915E: f (test.c:5) ==4597== by 0x109180: main (test.c:11) ==4597== ==4597== ==4597== HEAP SUMMARY: ==4597== in use at exit: 40 bytes in 1 blocks ==4597== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==4597== ==4597== LEAK SUMMARY: ==4597== definitely lost: 40 bytes in 1 blocks ==4597== indirectly lost: 0 bytes in 0 blocks ==4597== possibly lost: 0 bytes in 0 blocks ==4597== still reachable: 0 bytes in 0 blocks ==4597== suppressed: 0 bytes in 0 blocks ==4597== Rerun with --leak-check=full to see details of leaked memory ==4597== ==4597== For lists of detected and suppressed errors, rerun with: -s ==4597== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
... ==179== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==179== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==179== by 0x10915E: f (test.c:5) ==179== by 0x109180: main (test.c:11) ...