use std::os::raw::c_int; // 32位 use std::os::raw::c_double; // 64位 // 从标准库 libc 中引入三个函数。 // 此处是 Rust 对三个 C 函数的声明: extern "C" { fn abs(num: c_int) -> c_int; fn sqrt(num: c_double) -> c_double; fn pow(num: c_double, power: c_dou...
std::string 和std::wstring。不要使用 std::wstring 如果您关心可移植性( wchar_t 在Windows 上只有 16 位);使用 std::u32string 代替(又名 std::basic_string<char32_t>)。 内存表示( std::string 或std::wstring )独立于磁盘表示(UTF-8、UTF-16 或 UTF-32),因此请做好准备在边界处转换(读取和...
const std = @import("std");fn fibonacci(index: u32) u32 { if (index < 2) return index; return fibonacci(index - 1) + fibonacci(index - 2);}pub fn main() void { const foo = comptime fibonacci(7); std.debug.print("{}", .{ foo });}1.2.3.4.5.6.7.8.9.10...
; char dst[10][80]; // 以空格进行切割字符串并将结果存入dst int cnt = split(dst, src_string, " "); for (int i = 0; i < cnt; i++) { std::cout << "切割后: " << dst[i] << std::endl; } return 0; } 字符串分块: 循环将传入的字符串按照指定长度切片处理,切片后返回到...
; char dst[10][80]; // 以空格进行切割字符串并将结果存入dst int cnt = split(dst, src_string, " "); for (int i = 0; i < cnt; i++) { std::cout << "切割后: " << dst[i] << std::endl; } return 0; } 字符串分块: 循环将传入的字符串按照指定长度切片处理,切片后返回到...
std::string sItemName; // The structure has std::string object inside int nItemValue; }; // Init the structure ItemInfo item; // Do not use memset()! It can corrupt the structure // memset(&item, 0, sizeof(ItemInfo));
std.debug.print("{s}\n", .{"Zig" ++ "Lang"}); // ZigLang (concatenation)std.debug.print("{s}\n", .{"Zig" ** 5}); // ZigZigZigZigZig (repetition)std.debug.print("{}\n", .{@TypeOf("string")}); // *const [6:0]u8 (a pointer to an unsigned byte array)1...
避免内存泄漏的安全工具,例如 std.heap.GeneralPurposeAllocator Zig 不会像 Rust 那样限制你的编码方式,帮助你保持安全,避免泄漏,但仍然让你可以像在 C 中那样随心所欲。我个人认为这可能是一个方便的折衷。conststd = @import("std");test "detect leak" { var list = std.ArrayList(u21).init(std....
const string = std.fmt.bufPrint(&buf, "{s} {s}", .{ firstName, lastName }) catch unreachable; break :blk string; } if (firstName != null) break :blk firstName; if (lastName != null) break :blk lastName; break :blk "(no name)"; ...
usestd::os::raw::c_double;// 64位 // 从标准库 libc 中引入三个函数。 // 此处是 Rust 对三个 C 函数的声明: extern"C"{ fn abs(num:c_int)->c_int; fn sqrt(num:c_double)->c_double; fn pow(num:c_double,power:c_double)->c_double; ...