| ... | ... | @@ -236,19 +236,18 @@ pub fn main() !void { |
| 236 | 236 | } |
| 237 | 237 | {#code_end#} |
| 238 | 238 | <p> |
| 239 | | Usually you don't want to write to stdout. You want to write to stderr. And you |
| 240 | | don't care if it fails. It's more like a <em>warning message</em> that you want |
| 241 | | to emit. For that you can use a simpler API: |
| 239 | Usually you don't want to write to stdout. You want to write to stderr, and you |
| 240 | don't care if it fails. For that you can use a simpler API: |
| 242 | 241 | </p> |
| 243 | 242 | {#code_begin|exe|hello#} |
| 244 | | const warn = @import("std").debug.warn; |
| 243 | const print = @import("std").debug.print; |
| 245 | 244 | |
| 246 | 245 | pub fn main() void { |
| 247 | | warn("Hello, world!\n", .{}); |
| 246 | print("Hello, world!\n", .{}); |
| 248 | 247 | } |
| 249 | 248 | {#code_end#} |
| 250 | 249 | <p> |
| 251 | | Note that you can leave off the {#syntax#}!{#endsyntax#} from the return type because {#syntax#}warn{#endsyntax#} cannot fail. |
| 250 | Note that you can leave off the {#syntax#}!{#endsyntax#} from the return type because {#syntax#}print{#endsyntax#} cannot fail. |
| 252 | 251 | </p> |
| 253 | 252 | {#see_also|Values|@import|Errors|Root Source File#} |
| 254 | 253 | {#header_close#} |
| ... | ... | @@ -307,7 +306,7 @@ const Timestamp = struct { |
| 307 | 306 | {#header_open|Values#} |
| 308 | 307 | {#code_begin|exe|values#} |
| 309 | 308 | // Top-level declarations are order-independent: |
| 310 | | const warn = std.debug.warn; |
| 309 | const print = std.debug.print; |
| 311 | 310 | const std = @import("std"); |
| 312 | 311 | const os = std.os; |
| 313 | 312 | const assert = std.debug.assert; |
| ... | ... | @@ -315,14 +314,14 @@ const assert = std.debug.assert; |
| 315 | 314 | pub fn main() void { |
| 316 | 315 | // integers |
| 317 | 316 | const one_plus_one: i32 = 1 + 1; |
| 318 | | warn("1 + 1 = {}\n", .{one_plus_one}); |
| 317 | print("1 + 1 = {}\n", .{one_plus_one}); |
| 319 | 318 | |
| 320 | 319 | // floats |
| 321 | 320 | const seven_div_three: f32 = 7.0 / 3.0; |
| 322 | | warn("7.0 / 3.0 = {}\n", .{seven_div_three}); |
| 321 | print("7.0 / 3.0 = {}\n", .{seven_div_three}); |
| 323 | 322 | |
| 324 | 323 | // boolean |
| 325 | | warn("{}\n{}\n{}\n", .{ |
| 324 | print("{}\n{}\n{}\n", .{ |
| 326 | 325 | true and false, |
| 327 | 326 | true or false, |
| 328 | 327 | !true, |
| ... | ... | @@ -332,7 +331,7 @@ pub fn main() void { |
| 332 | 331 | var optional_value: ?[]const u8 = null; |
| 333 | 332 | assert(optional_value == null); |
| 334 | 333 | |
| 335 | | warn("\noptional 1\ntype: {}\nvalue: {}\n", .{ |
| 334 | print("\noptional 1\ntype: {}\nvalue: {}\n", .{ |
| 336 | 335 | @typeName(@TypeOf(optional_value)), |
| 337 | 336 | optional_value, |
| 338 | 337 | }); |
| ... | ... | @@ -340,7 +339,7 @@ pub fn main() void { |
| 340 | 339 | optional_value = "hi"; |
| 341 | 340 | assert(optional_value != null); |
| 342 | 341 | |
| 343 | | warn("\noptional 2\ntype: {}\nvalue: {}\n", .{ |
| 342 | print("\noptional 2\ntype: {}\nvalue: {}\n", .{ |
| 344 | 343 | @typeName(@TypeOf(optional_value)), |
| 345 | 344 | optional_value, |
| 346 | 345 | }); |
| ... | ... | @@ -348,14 +347,14 @@ pub fn main() void { |
| 348 | 347 | // error union |
| 349 | 348 | var number_or_error: anyerror!i32 = error.ArgNotFound; |
| 350 | 349 | |
| 351 | | warn("\nerror union 1\ntype: {}\nvalue: {}\n", .{ |
| 350 | print("\nerror union 1\ntype: {}\nvalue: {}\n", .{ |
| 352 | 351 | @typeName(@TypeOf(number_or_error)), |
| 353 | 352 | number_or_error, |
| 354 | 353 | }); |
| 355 | 354 | |
| 356 | 355 | number_or_error = 1234; |
| 357 | 356 | |
| 358 | | warn("\nerror union 2\ntype: {}\nvalue: {}\n", .{ |
| 357 | print("\nerror union 2\ntype: {}\nvalue: {}\n", .{ |
| 359 | 358 | @typeName(@TypeOf(number_or_error)), |
| 360 | 359 | number_or_error, |
| 361 | 360 | }); |
| ... | ... | @@ -994,15 +993,15 @@ export fn foo_optimized(x: f64) f64 { |
| 994 | 993 | which operates in strict mode.</p> |
| 995 | 994 | {#code_begin|exe|float_mode#} |
| 996 | 995 | {#code_link_object|foo#} |
| 997 | | const warn = @import("std").debug.warn; |
| 996 | const print = @import("std").debug.print; |
| 998 | 997 | |
| 999 | 998 | extern fn foo_strict(x: f64) f64; |
| 1000 | 999 | extern fn foo_optimized(x: f64) f64; |
| 1001 | 1000 | |
| 1002 | 1001 | pub fn main() void { |
| 1003 | 1002 | const x = 0.001; |
| 1004 | | warn("optimized = {}\n", .{foo_optimized(x)}); |
| 1005 | | warn("strict = {}\n", .{foo_strict(x)}); |
| 1003 | print("optimized = {}\n", .{foo_optimized(x)}); |
| 1004 | print("strict = {}\n", .{foo_strict(x)}); |
| 1006 | 1005 | } |
| 1007 | 1006 | {#code_end#} |
| 1008 | 1007 | {#see_also|@setFloatMode|Division by Zero#} |
| ... | ... | @@ -2668,9 +2667,9 @@ const std = @import("std"); |
| 2668 | 2667 | |
| 2669 | 2668 | pub fn main() void { |
| 2670 | 2669 | const Foo = struct {}; |
| 2671 | | std.debug.warn("variable: {}\n", .{@typeName(Foo)}); |
| 2672 | | std.debug.warn("anonymous: {}\n", .{@typeName(struct {})}); |
| 2673 | | std.debug.warn("function: {}\n", .{@typeName(List(i32))}); |
| 2670 | std.debug.print("variable: {}\n", .{@typeName(Foo)}); |
| 2671 | std.debug.print("anonymous: {}\n", .{@typeName(struct {})}); |
| 2672 | std.debug.print("function: {}\n", .{@typeName(List(i32))}); |
| 2674 | 2673 | } |
| 2675 | 2674 | |
| 2676 | 2675 | fn List(comptime T: type) type { |
| ... | ... | @@ -3869,7 +3868,7 @@ test "if error union" { |
| 3869 | 3868 | {#code_begin|test|defer#} |
| 3870 | 3869 | const std = @import("std"); |
| 3871 | 3870 | const assert = std.debug.assert; |
| 3872 | | const warn = std.debug.warn; |
| 3871 | const print = std.debug.print; |
| 3873 | 3872 | |
| 3874 | 3873 | // defer will execute an expression at the end of the current scope. |
| 3875 | 3874 | fn deferExample() usize { |
| ... | ... | @@ -3892,18 +3891,18 @@ test "defer basics" { |
| 3892 | 3891 | // If multiple defer statements are specified, they will be executed in |
| 3893 | 3892 | // the reverse order they were run. |
| 3894 | 3893 | fn deferUnwindExample() void { |
| 3895 | | warn("\n", .{}); |
| 3894 | print("\n", .{}); |
| 3896 | 3895 | |
| 3897 | 3896 | defer { |
| 3898 | | warn("1 ", .{}); |
| 3897 | print("1 ", .{}); |
| 3899 | 3898 | } |
| 3900 | 3899 | defer { |
| 3901 | | warn("2 ", .{}); |
| 3900 | print("2 ", .{}); |
| 3902 | 3901 | } |
| 3903 | 3902 | if (false) { |
| 3904 | 3903 | // defers are not run if they are never executed. |
| 3905 | 3904 | defer { |
| 3906 | | warn("3 ", .{}); |
| 3905 | print("3 ", .{}); |
| 3907 | 3906 | } |
| 3908 | 3907 | } |
| 3909 | 3908 | } |
| ... | ... | @@ -3918,15 +3917,15 @@ test "defer unwinding" { |
| 3918 | 3917 | // This is especially useful in allowing a function to clean up properly |
| 3919 | 3918 | // on error, and replaces goto error handling tactics as seen in c. |
| 3920 | 3919 | fn deferErrorExample(is_error: bool) !void { |
| 3921 | | warn("\nstart of function\n", .{}); |
| 3920 | print("\nstart of function\n", .{}); |
| 3922 | 3921 | |
| 3923 | 3922 | // This will always be executed on exit |
| 3924 | 3923 | defer { |
| 3925 | | warn("end of function\n", .{}); |
| 3924 | print("end of function\n", .{}); |
| 3926 | 3925 | } |
| 3927 | 3926 | |
| 3928 | 3927 | errdefer { |
| 3929 | | warn("encountered an error!\n", .{}); |
| 3928 | print("encountered an error!\n", .{}); |
| 3930 | 3929 | } |
| 3931 | 3930 | |
| 3932 | 3931 | if (is_error) { |
| ... | ... | @@ -5925,13 +5924,13 @@ const Node = struct { |
| 5925 | 5924 | Putting all of this together, let's see how {#syntax#}printf{#endsyntax#} works in Zig. |
| 5926 | 5925 | </p> |
| 5927 | 5926 | {#code_begin|exe|printf#} |
| 5928 | | const warn = @import("std").debug.warn; |
| 5927 | const print = @import("std").debug.print; |
| 5929 | 5928 | |
| 5930 | 5929 | const a_number: i32 = 1234; |
| 5931 | 5930 | const a_string = "foobar"; |
| 5932 | 5931 | |
| 5933 | 5932 | pub fn main() void { |
| 5934 | | warn("here is a string: '{}' here is a number: {}\n", .{a_string, a_number}); |
| 5933 | print("here is a string: '{}' here is a number: {}\n", .{a_string, a_number}); |
| 5935 | 5934 | } |
| 5936 | 5935 | {#code_end#} |
| 5937 | 5936 | |
| ... | ... | @@ -6045,13 +6044,13 @@ pub fn printValue(self: *OutStream, value: var) !void { |
| 6045 | 6044 | And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}? |
| 6046 | 6045 | </p> |
| 6047 | 6046 | {#code_begin|test_err|Unused arguments#} |
| 6048 | | const warn = @import("std").debug.warn; |
| 6047 | const print = @import("std").debug.print; |
| 6049 | 6048 | |
| 6050 | 6049 | const a_number: i32 = 1234; |
| 6051 | 6050 | const a_string = "foobar"; |
| 6052 | 6051 | |
| 6053 | 6052 | test "printf too many arguments" { |
| 6054 | | warn("here is a string: '{}' here is a number: {}\n", .{ |
| 6053 | print("here is a string: '{}' here is a number: {}\n", .{ |
| 6055 | 6054 | a_string, |
| 6056 | 6055 | a_number, |
| 6057 | 6056 | a_number, |
| ... | ... | @@ -6066,14 +6065,14 @@ test "printf too many arguments" { |
| 6066 | 6065 | only that it is a compile-time known value that can be coerced to a {#syntax#}[]const u8{#endsyntax#}: |
| 6067 | 6066 | </p> |
| 6068 | 6067 | {#code_begin|exe|printf#} |
| 6069 | | const warn = @import("std").debug.warn; |
| 6068 | const print = @import("std").debug.print; |
| 6070 | 6069 | |
| 6071 | 6070 | const a_number: i32 = 1234; |
| 6072 | 6071 | const a_string = "foobar"; |
| 6073 | 6072 | const fmt = "here is a string: '{}' here is a number: {}\n"; |
| 6074 | 6073 | |
| 6075 | 6074 | pub fn main() void { |
| 6076 | | warn(fmt, .{a_string, a_number}); |
| 6075 | print(fmt, .{a_string, a_number}); |
| 6077 | 6076 | } |
| 6078 | 6077 | {#code_end#} |
| 6079 | 6078 | <p> |
| ... | ... | @@ -6511,7 +6510,7 @@ pub fn main() void { |
| 6511 | 6510 | |
| 6512 | 6511 | fn amainWrap() void { |
| 6513 | 6512 | amain() catch |e| { |
| 6514 | | std.debug.warn("{}\n", .{e}); |
| 6513 | std.debug.print("{}\n", .{e}); |
| 6515 | 6514 | if (@errorReturnTrace()) |trace| { |
| 6516 | 6515 | std.debug.dumpStackTrace(trace.*); |
| 6517 | 6516 | } |
| ... | ... | @@ -6541,8 +6540,8 @@ fn amain() !void { |
| 6541 | 6540 | const download_text = try await download_frame; |
| 6542 | 6541 | defer allocator.free(download_text); |
| 6543 | 6542 | |
| 6544 | | std.debug.warn("download_text: {}\n", .{download_text}); |
| 6545 | | std.debug.warn("file_text: {}\n", .{file_text}); |
| 6543 | std.debug.print("download_text: {}\n", .{download_text}); |
| 6544 | std.debug.print("file_text: {}\n", .{file_text}); |
| 6546 | 6545 | } |
| 6547 | 6546 | |
| 6548 | 6547 | var global_download_frame: anyframe = undefined; |
| ... | ... | @@ -6552,7 +6551,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 { |
| 6552 | 6551 | suspend { |
| 6553 | 6552 | global_download_frame = @frame(); |
| 6554 | 6553 | } |
| 6555 | | std.debug.warn("fetchUrl returning\n", .{}); |
| 6554 | std.debug.print("fetchUrl returning\n", .{}); |
| 6556 | 6555 | return result; |
| 6557 | 6556 | } |
| 6558 | 6557 | |
| ... | ... | @@ -6563,7 +6562,7 @@ fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 { |
| 6563 | 6562 | suspend { |
| 6564 | 6563 | global_file_frame = @frame(); |
| 6565 | 6564 | } |
| 6566 | | std.debug.warn("readFile returning\n", .{}); |
| 6565 | std.debug.print("readFile returning\n", .{}); |
| 6567 | 6566 | return result; |
| 6568 | 6567 | } |
| 6569 | 6568 | {#code_end#} |
| ... | ... | @@ -6581,7 +6580,7 @@ pub fn main() void { |
| 6581 | 6580 | |
| 6582 | 6581 | fn amainWrap() void { |
| 6583 | 6582 | amain() catch |e| { |
| 6584 | | std.debug.warn("{}\n", .{e}); |
| 6583 | std.debug.print("{}\n", .{e}); |
| 6585 | 6584 | if (@errorReturnTrace()) |trace| { |
| 6586 | 6585 | std.debug.dumpStackTrace(trace.*); |
| 6587 | 6586 | } |
| ... | ... | @@ -6611,21 +6610,21 @@ fn amain() !void { |
| 6611 | 6610 | const download_text = try await download_frame; |
| 6612 | 6611 | defer allocator.free(download_text); |
| 6613 | 6612 | |
| 6614 | | std.debug.warn("download_text: {}\n", .{download_text}); |
| 6615 | | std.debug.warn("file_text: {}\n", .{file_text}); |
| 6613 | std.debug.print("download_text: {}\n", .{download_text}); |
| 6614 | std.debug.print("file_text: {}\n", .{file_text}); |
| 6616 | 6615 | } |
| 6617 | 6616 | |
| 6618 | 6617 | fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 { |
| 6619 | 6618 | const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents"); |
| 6620 | 6619 | errdefer allocator.free(result); |
| 6621 | | std.debug.warn("fetchUrl returning\n", .{}); |
| 6620 | std.debug.print("fetchUrl returning\n", .{}); |
| 6622 | 6621 | return result; |
| 6623 | 6622 | } |
| 6624 | 6623 | |
| 6625 | 6624 | fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 { |
| 6626 | 6625 | const result = try std.mem.dupe(allocator, u8, "this is the file contents"); |
| 6627 | 6626 | errdefer allocator.free(result); |
| 6628 | | std.debug.warn("readFile returning\n", .{}); |
| 6627 | std.debug.print("readFile returning\n", .{}); |
| 6629 | 6628 | return result; |
| 6630 | 6629 | } |
| 6631 | 6630 | {#code_end#} |
| ... | ... | @@ -7121,7 +7120,7 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val |
| 7121 | 7120 | compile-time executing code. |
| 7122 | 7121 | </p> |
| 7123 | 7122 | {#code_begin|test_err|found compile log statement#} |
| 7124 | | const warn = @import("std").debug.warn; |
| 7123 | const print = @import("std").debug.print; |
| 7125 | 7124 | |
| 7126 | 7125 | const num1 = blk: { |
| 7127 | 7126 | var val1: i32 = 99; |
| ... | ... | @@ -7133,7 +7132,7 @@ const num1 = blk: { |
| 7133 | 7132 | test "main" { |
| 7134 | 7133 | @compileLog("comptime in main"); |
| 7135 | 7134 | |
| 7136 | | warn("Runtime in main, num1 = {}.\n", .{num1}); |
| 7135 | print("Runtime in main, num1 = {}.\n", .{num1}); |
| 7137 | 7136 | } |
| 7138 | 7137 | {#code_end#} |
| 7139 | 7138 | <p> |
| ... | ... | @@ -7145,7 +7144,7 @@ test "main" { |
| 7145 | 7144 | program compiles successfully and the generated executable prints: |
| 7146 | 7145 | </p> |
| 7147 | 7146 | {#code_begin|test#} |
| 7148 | | const warn = @import("std").debug.warn; |
| 7147 | const print = @import("std").debug.print; |
| 7149 | 7148 | |
| 7150 | 7149 | const num1 = blk: { |
| 7151 | 7150 | var val1: i32 = 99; |
| ... | ... | @@ -7154,7 +7153,7 @@ const num1 = blk: { |
| 7154 | 7153 | }; |
| 7155 | 7154 | |
| 7156 | 7155 | test "main" { |
| 7157 | | warn("Runtime in main, num1 = {}.\n", .{num1}); |
| 7156 | print("Runtime in main, num1 = {}.\n", .{num1}); |
| 7158 | 7157 | } |
| 7159 | 7158 | {#code_end#} |
| 7160 | 7159 | {#header_close#} |
| ... | ... | @@ -8555,7 +8554,7 @@ const std = @import("std"); |
| 8555 | 8554 | pub fn main() void { |
| 8556 | 8555 | var value: i32 = -1; |
| 8557 | 8556 | var unsigned = @intCast(u32, value); |
| 8558 | | std.debug.warn("value: {}\n", .{unsigned}); |
| 8557 | std.debug.print("value: {}\n", .{unsigned}); |
| 8559 | 8558 | } |
| 8560 | 8559 | {#code_end#} |
| 8561 | 8560 | <p> |
| ... | ... | @@ -8577,7 +8576,7 @@ const std = @import("std"); |
| 8577 | 8576 | pub fn main() void { |
| 8578 | 8577 | var spartan_count: u16 = 300; |
| 8579 | 8578 | const byte = @intCast(u8, spartan_count); |
| 8580 | | std.debug.warn("value: {}\n", .{byte}); |
| 8579 | std.debug.print("value: {}\n", .{byte}); |
| 8581 | 8580 | } |
| 8582 | 8581 | {#code_end#} |
| 8583 | 8582 | <p> |
| ... | ... | @@ -8611,7 +8610,7 @@ const std = @import("std"); |
| 8611 | 8610 | pub fn main() void { |
| 8612 | 8611 | var byte: u8 = 255; |
| 8613 | 8612 | byte += 1; |
| 8614 | | std.debug.warn("value: {}\n", .{byte}); |
| 8613 | std.debug.print("value: {}\n", .{byte}); |
| 8615 | 8614 | } |
| 8616 | 8615 | {#code_end#} |
| 8617 | 8616 | {#header_close#} |
| ... | ... | @@ -8629,16 +8628,16 @@ pub fn main() void { |
| 8629 | 8628 | <p>Example of catching an overflow for addition:</p> |
| 8630 | 8629 | {#code_begin|exe_err#} |
| 8631 | 8630 | const math = @import("std").math; |
| 8632 | | const warn = @import("std").debug.warn; |
| 8631 | const print = @import("std").debug.print; |
| 8633 | 8632 | pub fn main() !void { |
| 8634 | 8633 | var byte: u8 = 255; |
| 8635 | 8634 | |
| 8636 | 8635 | byte = if (math.add(u8, byte, 1)) |result| result else |err| { |
| 8637 | | warn("unable to add one: {}\n", .{@errorName(err)}); |
| 8636 | print("unable to add one: {}\n", .{@errorName(err)}); |
| 8638 | 8637 | return err; |
| 8639 | 8638 | }; |
| 8640 | 8639 | |
| 8641 | | warn("result: {}\n", .{byte}); |
| 8640 | print("result: {}\n", .{byte}); |
| 8642 | 8641 | } |
| 8643 | 8642 | {#code_end#} |
| 8644 | 8643 | {#header_close#} |
| ... | ... | @@ -8657,15 +8656,15 @@ pub fn main() !void { |
| 8657 | 8656 | Example of {#link|@addWithOverflow#}: |
| 8658 | 8657 | </p> |
| 8659 | 8658 | {#code_begin|exe#} |
| 8660 | | const warn = @import("std").debug.warn; |
| 8659 | const print = @import("std").debug.print; |
| 8661 | 8660 | pub fn main() void { |
| 8662 | 8661 | var byte: u8 = 255; |
| 8663 | 8662 | |
| 8664 | 8663 | var result: u8 = undefined; |
| 8665 | 8664 | if (@addWithOverflow(u8, byte, 10, &result)) { |
| 8666 | | warn("overflowed result: {}\n", .{result}); |
| 8665 | print("overflowed result: {}\n", .{result}); |
| 8667 | 8666 | } else { |
| 8668 | | warn("result: {}\n", .{result}); |
| 8667 | print("result: {}\n", .{result}); |
| 8669 | 8668 | } |
| 8670 | 8669 | } |
| 8671 | 8670 | {#code_end#} |
| ... | ... | @@ -8710,7 +8709,7 @@ const std = @import("std"); |
| 8710 | 8709 | pub fn main() void { |
| 8711 | 8710 | var x: u8 = 0b01010101; |
| 8712 | 8711 | var y = @shlExact(x, 2); |
| 8713 | | std.debug.warn("value: {}\n", .{y}); |
| 8712 | std.debug.print("value: {}\n", .{y}); |
| 8714 | 8713 | } |
| 8715 | 8714 | {#code_end#} |
| 8716 | 8715 | {#header_close#} |
| ... | ... | @@ -8728,7 +8727,7 @@ const std = @import("std"); |
| 8728 | 8727 | pub fn main() void { |
| 8729 | 8728 | var x: u8 = 0b10101010; |
| 8730 | 8729 | var y = @shrExact(x, 2); |
| 8731 | | std.debug.warn("value: {}\n", .{y}); |
| 8730 | std.debug.print("value: {}\n", .{y}); |
| 8732 | 8731 | } |
| 8733 | 8732 | {#code_end#} |
| 8734 | 8733 | {#header_close#} |
| ... | ... | @@ -8749,7 +8748,7 @@ pub fn main() void { |
| 8749 | 8748 | var a: u32 = 1; |
| 8750 | 8749 | var b: u32 = 0; |
| 8751 | 8750 | var c = a / b; |
| 8752 | | std.debug.warn("value: {}\n", .{c}); |
| 8751 | std.debug.print("value: {}\n", .{c}); |
| 8753 | 8752 | } |
| 8754 | 8753 | {#code_end#} |
| 8755 | 8754 | {#header_close#} |
| ... | ... | @@ -8770,7 +8769,7 @@ pub fn main() void { |
| 8770 | 8769 | var a: u32 = 10; |
| 8771 | 8770 | var b: u32 = 0; |
| 8772 | 8771 | var c = a % b; |
| 8773 | | std.debug.warn("value: {}\n", .{c}); |
| 8772 | std.debug.print("value: {}\n", .{c}); |
| 8774 | 8773 | } |
| 8775 | 8774 | {#code_end#} |
| 8776 | 8775 | {#header_close#} |
| ... | ... | @@ -8791,7 +8790,7 @@ pub fn main() void { |
| 8791 | 8790 | var a: u32 = 10; |
| 8792 | 8791 | var b: u32 = 3; |
| 8793 | 8792 | var c = @divExact(a, b); |
| 8794 | | std.debug.warn("value: {}\n", .{c}); |
| 8793 | std.debug.print("value: {}\n", .{c}); |
| 8795 | 8794 | } |
| 8796 | 8795 | {#code_end#} |
| 8797 | 8796 | {#header_close#} |
| ... | ... | @@ -8810,20 +8809,20 @@ const std = @import("std"); |
| 8810 | 8809 | pub fn main() void { |
| 8811 | 8810 | var optional_number: ?i32 = null; |
| 8812 | 8811 | var number = optional_number.?; |
| 8813 | | std.debug.warn("value: {}\n", .{number}); |
| 8812 | std.debug.print("value: {}\n", .{number}); |
| 8814 | 8813 | } |
| 8815 | 8814 | {#code_end#} |
| 8816 | 8815 | <p>One way to avoid this crash is to test for null instead of assuming non-null, with |
| 8817 | 8816 | the {#syntax#}if{#endsyntax#} expression:</p> |
| 8818 | 8817 | {#code_begin|exe|test#} |
| 8819 | | const warn = @import("std").debug.warn; |
| 8818 | const print = @import("std").debug.print; |
| 8820 | 8819 | pub fn main() void { |
| 8821 | 8820 | const optional_number: ?i32 = null; |
| 8822 | 8821 | |
| 8823 | 8822 | if (optional_number) |number| { |
| 8824 | | warn("got number: {}\n", .{number}); |
| 8823 | print("got number: {}\n", .{number}); |
| 8825 | 8824 | } else { |
| 8826 | | warn("it's null\n", .{}); |
| 8825 | print("it's null\n", .{}); |
| 8827 | 8826 | } |
| 8828 | 8827 | } |
| 8829 | 8828 | {#code_end#} |
| ... | ... | @@ -8846,7 +8845,7 @@ const std = @import("std"); |
| 8846 | 8845 | |
| 8847 | 8846 | pub fn main() void { |
| 8848 | 8847 | const number = getNumberOrFail() catch unreachable; |
| 8849 | | std.debug.warn("value: {}\n", .{number}); |
| 8848 | std.debug.print("value: {}\n", .{number}); |
| 8850 | 8849 | } |
| 8851 | 8850 | |
| 8852 | 8851 | fn getNumberOrFail() !i32 { |
| ... | ... | @@ -8856,15 +8855,15 @@ fn getNumberOrFail() !i32 { |
| 8856 | 8855 | <p>One way to avoid this crash is to test for an error instead of assuming a successful result, with |
| 8857 | 8856 | the {#syntax#}if{#endsyntax#} expression:</p> |
| 8858 | 8857 | {#code_begin|exe#} |
| 8859 | | const warn = @import("std").debug.warn; |
| 8858 | const print = @import("std").debug.print; |
| 8860 | 8859 | |
| 8861 | 8860 | pub fn main() void { |
| 8862 | 8861 | const result = getNumberOrFail(); |
| 8863 | 8862 | |
| 8864 | 8863 | if (result) |number| { |
| 8865 | | warn("got number: {}\n", .{number}); |
| 8864 | print("got number: {}\n", .{number}); |
| 8866 | 8865 | } else |err| { |
| 8867 | | warn("got error: {}\n", .{@errorName(err)}); |
| 8866 | print("got error: {}\n", .{@errorName(err)}); |
| 8868 | 8867 | } |
| 8869 | 8868 | } |
| 8870 | 8869 | |
| ... | ... | @@ -8891,7 +8890,7 @@ pub fn main() void { |
| 8891 | 8890 | var err = error.AnError; |
| 8892 | 8891 | var number = @errorToInt(err) + 500; |
| 8893 | 8892 | var invalid_err = @intToError(number); |
| 8894 | | std.debug.warn("value: {}\n", .{number}); |
| 8893 | std.debug.print("value: {}\n", .{number}); |
| 8895 | 8894 | } |
| 8896 | 8895 | {#code_end#} |
| 8897 | 8896 | {#header_close#} |
| ... | ... | @@ -8921,7 +8920,7 @@ const Foo = enum { |
| 8921 | 8920 | pub fn main() void { |
| 8922 | 8921 | var a: u2 = 3; |
| 8923 | 8922 | var b = @intToEnum(Foo, a); |
| 8924 | | std.debug.warn("value: {}\n", .{@tagName(b)}); |
| 8923 | std.debug.print("value: {}\n", .{@tagName(b)}); |
| 8925 | 8924 | } |
| 8926 | 8925 | {#code_end#} |
| 8927 | 8926 | {#header_close#} |
| ... | ... | @@ -8958,7 +8957,7 @@ pub fn main() void { |
| 8958 | 8957 | } |
| 8959 | 8958 | fn foo(set1: Set1) void { |
| 8960 | 8959 | const x = @errSetCast(Set2, set1); |
| 8961 | | std.debug.warn("value: {}\n", .{x}); |
| 8960 | std.debug.print("value: {}\n", .{x}); |
| 8962 | 8961 | } |
| 8963 | 8962 | {#code_end#} |
| 8964 | 8963 | {#header_close#} |
| ... | ... | @@ -9015,7 +9014,7 @@ pub fn main() void { |
| 9015 | 9014 | |
| 9016 | 9015 | fn bar(f: *Foo) void { |
| 9017 | 9016 | f.float = 12.34; |
| 9018 | | std.debug.warn("value: {}\n", .{f.float}); |
| 9017 | std.debug.print("value: {}\n", .{f.float}); |
| 9019 | 9018 | } |
| 9020 | 9019 | {#code_end#} |
| 9021 | 9020 | <p> |
| ... | ... | @@ -9039,7 +9038,7 @@ pub fn main() void { |
| 9039 | 9038 | |
| 9040 | 9039 | fn bar(f: *Foo) void { |
| 9041 | 9040 | f.* = Foo{ .float = 12.34 }; |
| 9042 | | std.debug.warn("value: {}\n", .{f.float}); |
| 9041 | std.debug.print("value: {}\n", .{f.float}); |
| 9043 | 9042 | } |
| 9044 | 9043 | {#code_end#} |
| 9045 | 9044 | <p> |
| ... | ... | @@ -9058,7 +9057,7 @@ pub fn main() void { |
| 9058 | 9057 | var f = Foo{ .int = 42 }; |
| 9059 | 9058 | f = Foo{ .float = undefined }; |
| 9060 | 9059 | bar(&f); |
| 9061 | | std.debug.warn("value: {}\n", .{f.float}); |
| 9060 | std.debug.print("value: {}\n", .{f.float}); |
| 9062 | 9061 | } |
| 9063 | 9062 | |
| 9064 | 9063 | fn bar(f: *Foo) void { |
| ... | ... | @@ -9178,7 +9177,7 @@ pub fn main() !void { |
| 9178 | 9177 | const allocator = &arena.allocator; |
| 9179 | 9178 | |
| 9180 | 9179 | const ptr = try allocator.create(i32); |
| 9181 | | std.debug.warn("ptr={*}\n", .{ptr}); |
| 9180 | std.debug.print("ptr={*}\n", .{ptr}); |
| 9182 | 9181 | } |
| 9183 | 9182 | {#code_end#} |
| 9184 | 9183 | When using this kind of allocator, there is no need to free anything manually. Everything |
| ... | ... | @@ -9712,7 +9711,7 @@ pub fn main() !void { |
| 9712 | 9711 | defer std.process.argsFree(std.heap.page_allocator, args); |
| 9713 | 9712 | |
| 9714 | 9713 | for (args) |arg, i| { |
| 9715 | | std.debug.warn("{}: {}\n", .{i, arg}); |
| 9714 | std.debug.print("{}: {}\n", .{i, arg}); |
| 9716 | 9715 | } |
| 9717 | 9716 | } |
| 9718 | 9717 | {#code_end#} |
| ... | ... | @@ -9734,7 +9733,7 @@ pub fn main() !void { |
| 9734 | 9733 | try preopens.populate(); |
| 9735 | 9734 | |
| 9736 | 9735 | for (preopens.asSlice()) |preopen, i| { |
| 9737 | | std.debug.warn("{}: {}\n", .{ i, preopen }); |
| 9736 | std.debug.print("{}: {}\n", .{ i, preopen }); |
| 9738 | 9737 | } |
| 9739 | 9738 | } |
| 9740 | 9739 | {#code_end#} |