mirror of
				https://codeberg.org/ziglings/exercises.git
				synced 2025-11-03 20:25:37 +00:00 
			
		
		
		
	1. All exercises should print a trailing \n 2. The build script should always show you _exactly_ what it's looking for when you get it wrong. Therefore, .output should be set to the exact expected output.
		
			
				
	
	
		
			39 lines
		
	
	
		
			858 B
		
	
	
	
		
			Zig
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			858 B
		
	
	
	
		
			Zig
		
	
	
	
	
	
//
 | 
						|
// Zig has a handy "try" shortcut for this common error handling pattern:
 | 
						|
//
 | 
						|
//     canFail() catch |err| return err;
 | 
						|
//
 | 
						|
// which can be more compactly written as:
 | 
						|
//
 | 
						|
//     try canFail();
 | 
						|
//
 | 
						|
const std = @import("std");
 | 
						|
 | 
						|
const MyNumberError = error{
 | 
						|
    TooSmall,
 | 
						|
    TooBig,
 | 
						|
};
 | 
						|
 | 
						|
pub fn main() void {
 | 
						|
    var a: u32 = addFive(44) catch 0;
 | 
						|
    var b: u32 = addFive(14) catch 0;
 | 
						|
    var c: u32 = addFive(4) catch 0;
 | 
						|
 | 
						|
    std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
 | 
						|
}
 | 
						|
 | 
						|
fn addFive(n: u32) MyNumberError!u32 {
 | 
						|
    // This function needs to return any error which might come back from detect().
 | 
						|
    // Please use a "try" statement rather than a "catch".
 | 
						|
    //
 | 
						|
    var x = detect(n);
 | 
						|
 | 
						|
    return x + 5;
 | 
						|
}
 | 
						|
 | 
						|
fn detect(n: u32) MyNumberError!u32 {
 | 
						|
    if (n < 10) return MyNumberError.TooSmall;
 | 
						|
    if (n > 20) return MyNumberError.TooBig;
 | 
						|
    return n;
 | 
						|
}
 |