61-72
This commit is contained in:
parent
28dc209134
commit
723822e800
12 changed files with 23 additions and 23 deletions
|
|
@ -67,7 +67,7 @@ const print = @import("std").debug.print;
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
var letter: u8 = 'A';
|
var letter: u8 = 'A';
|
||||||
|
|
||||||
const my_letter: ??? = &letter;
|
const my_letter: ?*[1]u8 = &letter;
|
||||||
// ^^^^^^^
|
// ^^^^^^^
|
||||||
// Your type here.
|
// Your type here.
|
||||||
// Must coerce from &letter (which is a *u8).
|
// Must coerce from &letter (which is a *u8).
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ pub fn main() void {
|
||||||
// return it from the for loop.
|
// return it from the for loop.
|
||||||
const current_lang: ?[]const u8 = for (langs) |lang| {
|
const current_lang: ?[]const u8 = for (langs) |lang| {
|
||||||
if (lang.len == 3) break lang;
|
if (lang.len == 3) break lang;
|
||||||
};
|
} else null;
|
||||||
|
|
||||||
if (current_lang) |cl| {
|
if (current_lang) |cl| {
|
||||||
print("Current language: {s}\n", .{cl});
|
print("Current language: {s}\n", .{cl});
|
||||||
|
|
|
||||||
|
|
@ -128,8 +128,8 @@ pub fn main() void {
|
||||||
// wanted for this Food.
|
// wanted for this Food.
|
||||||
//
|
//
|
||||||
// Please return this Food from the loop.
|
// Please return this Food from the loop.
|
||||||
break;
|
break food;
|
||||||
};
|
} else menu[0];
|
||||||
// ^ Oops! We forgot to return Mac & Cheese as the default
|
// ^ Oops! We forgot to return Mac & Cheese as the default
|
||||||
// Food when the requested ingredients aren't found.
|
// Food when the requested ingredients aren't found.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ pub fn main() void {
|
||||||
//
|
//
|
||||||
// If there was no overflow at all while adding 5 to a, what value would
|
// If there was no overflow at all while adding 5 to a, what value would
|
||||||
// 'my_result' hold? Write the answer in into 'expected_result'.
|
// 'my_result' hold? Write the answer in into 'expected_result'.
|
||||||
const expected_result: u8 = ???;
|
const expected_result: u8 = 18;
|
||||||
print(". Without overflow: {b:0>8}. ", .{expected_result});
|
print(". Without overflow: {b:0>8}. ", .{expected_result});
|
||||||
|
|
||||||
print("Furthermore, ", .{});
|
print("Furthermore, ", .{});
|
||||||
|
|
@ -78,6 +78,6 @@ pub fn main() void {
|
||||||
// Now it's your turn. See if you can fix this attempt to use
|
// Now it's your turn. See if you can fix this attempt to use
|
||||||
// this builtin to reverse the bits of a u8 integer.
|
// this builtin to reverse the bits of a u8 integer.
|
||||||
const input: u8 = 0b11110000;
|
const input: u8 = 0b11110000;
|
||||||
const tupni: u8 = @bitReverse(input, tupni);
|
const tupni: u8 = @bitReverse(input);
|
||||||
print("{b:0>8} backwards is {b:0>8}.\n", .{ input, tupni });
|
print("{b:0>8} backwards is {b:0>8}.\n", .{ input, tupni });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ pub fn main() void {
|
||||||
// Oops! We cannot leave the 'me' and 'myself' fields
|
// Oops! We cannot leave the 'me' and 'myself' fields
|
||||||
// undefined. Please set them here:
|
// undefined. Please set them here:
|
||||||
narcissus.me = &narcissus;
|
narcissus.me = &narcissus;
|
||||||
narcissus.??? = ???;
|
narcissus.myself = &narcissus;
|
||||||
|
|
||||||
// This determines a "peer type" from three separate
|
// This determines a "peer type" from three separate
|
||||||
// references (they just happen to all be the same object).
|
// references (they just happen to all be the same object).
|
||||||
|
|
@ -70,7 +70,7 @@ pub fn main() void {
|
||||||
//
|
//
|
||||||
// The fix for this is very subtle, but it makes a big
|
// The fix for this is very subtle, but it makes a big
|
||||||
// difference!
|
// difference!
|
||||||
const Type2 = narcissus.fetchTheMostBeautifulType();
|
const Type2 = Narcissus.fetchTheMostBeautifulType();
|
||||||
|
|
||||||
// Now we print a pithy statement about Narcissus.
|
// Now we print a pithy statement about Narcissus.
|
||||||
print("A {s} loves all {s}es. ", .{
|
print("A {s} loves all {s}es. ", .{
|
||||||
|
|
@ -109,15 +109,15 @@ pub fn main() void {
|
||||||
// Please complete these 'if' statements so that the field
|
// Please complete these 'if' statements so that the field
|
||||||
// name will not be printed if the field is of type 'void'
|
// name will not be printed if the field is of type 'void'
|
||||||
// (which is a zero-bit type that takes up no space at all!):
|
// (which is a zero-bit type that takes up no space at all!):
|
||||||
if (fields[0].??? != void) {
|
if (fields[0].type != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[0].name});
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[0].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields[1].??? != void) {
|
if (fields[1].type != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[1].name});
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[1].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields[2].??? != void) {
|
if (fields[2].type != void) {
|
||||||
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[2].name});
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[2].name});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,8 +62,8 @@ pub fn main() void {
|
||||||
// types with specific sizes. The comptime numbers will be
|
// types with specific sizes. The comptime numbers will be
|
||||||
// coerced (if they'll fit!) into your chosen runtime types.
|
// coerced (if they'll fit!) into your chosen runtime types.
|
||||||
// For this it is necessary to specify a size, e.g. 32 bit.
|
// For this it is necessary to specify a size, e.g. 32 bit.
|
||||||
var var_int = 12345;
|
var var_int: u32 = 12345;
|
||||||
var var_float = 987.654;
|
var var_float: f32 = 987.654;
|
||||||
|
|
||||||
// We can change what is stored at the areas set aside for
|
// We can change what is stored at the areas set aside for
|
||||||
// "var_int" and "var_float" in the running compiled program.
|
// "var_int" and "var_float" in the running compiled program.
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ pub fn main() void {
|
||||||
// In this contrived example, we've decided to allocate some
|
// In this contrived example, we've decided to allocate some
|
||||||
// arrays using a variable count! But something's missing...
|
// arrays using a variable count! But something's missing...
|
||||||
//
|
//
|
||||||
var count = 0;
|
comptime var count = 0;
|
||||||
|
|
||||||
count += 1;
|
count += 1;
|
||||||
const a1: [count]u8 = .{'A'} ** count;
|
const a1: [count]u8 = .{'A'} ** count;
|
||||||
|
|
@ -60,5 +60,5 @@ pub fn main() void {
|
||||||
//
|
//
|
||||||
// Try uncommenting this line and playing around with it
|
// Try uncommenting this line and playing around with it
|
||||||
// (copy it, move it) to see what it does:
|
// (copy it, move it) to see what it does:
|
||||||
//@compileLog("Count at compile time: ", count);
|
// @compileLog("Count at compile time: ", count);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ const Schooner = struct {
|
||||||
//
|
//
|
||||||
// Please change this so that it sets a 0 scale to 1
|
// Please change this so that it sets a 0 scale to 1
|
||||||
// instead.
|
// instead.
|
||||||
if (my_scale == 0) @compileError("Scale 1:0 is not valid!");
|
if (my_scale == 0) my_scale = 1;
|
||||||
|
|
||||||
self.scale = my_scale;
|
self.scale = my_scale;
|
||||||
self.hull_length /= my_scale;
|
self.hull_length /= my_scale;
|
||||||
|
|
@ -69,7 +69,7 @@ pub fn main() void {
|
||||||
// Hey, we can't just pass this runtime variable as an
|
// Hey, we can't just pass this runtime variable as an
|
||||||
// argument to the scaleMe() method. What would let us do
|
// argument to the scaleMe() method. What would let us do
|
||||||
// that?
|
// that?
|
||||||
var scale: u32 = undefined;
|
comptime var scale: u32 = undefined;
|
||||||
|
|
||||||
scale = 32; // 1:32 scale
|
scale = 32; // 1:32 scale
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,8 @@ pub fn main() void {
|
||||||
// 2) Sets the size of the array of type T (which is the
|
// 2) Sets the size of the array of type T (which is the
|
||||||
// sequence we're creating and returning).
|
// sequence we're creating and returning).
|
||||||
//
|
//
|
||||||
fn makeSequence(comptime T: type, ??? size: usize) [???]T {
|
fn makeSequence(comptime T: type, comptime size: usize) [size]T {
|
||||||
var sequence: [???]T = undefined;
|
var sequence: [size]T = undefined;
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
|
|
||||||
while (i < size) : (i += 1) {
|
while (i < size) : (i += 1) {
|
||||||
|
|
|
||||||
|
|
@ -123,8 +123,8 @@ fn isADuck(possible_duck: anytype) bool {
|
||||||
// Please make sure MyType has both waddle() and quack()
|
// Please make sure MyType has both waddle() and quack()
|
||||||
// methods:
|
// methods:
|
||||||
const MyType = @TypeOf(possible_duck);
|
const MyType = @TypeOf(possible_duck);
|
||||||
const walks_like_duck = ???;
|
const walks_like_duck = @hasDecl(MyType, "waddle");
|
||||||
const quacks_like_duck = ???;
|
const quacks_like_duck = @hasDecl(MyType, "quack");
|
||||||
|
|
||||||
const is_duck = walks_like_duck and quacks_like_duck;
|
const is_duck = walks_like_duck and quacks_like_duck;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ pub fn main() void {
|
||||||
|
|
||||||
const fields = @typeInfo(Narcissus).Struct.fields;
|
const fields = @typeInfo(Narcissus).Struct.fields;
|
||||||
|
|
||||||
??? {
|
inline for (fields) |field| {
|
||||||
if (field.type != void) {
|
if (field.type != void) {
|
||||||
print(" {s}", .{field.name});
|
print(" {s}", .{field.name});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ pub fn main() void {
|
||||||
// at compile time.
|
// at compile time.
|
||||||
//
|
//
|
||||||
// Please fix this to loop once per "instruction":
|
// Please fix this to loop once per "instruction":
|
||||||
??? (i < instructions.len) : (???) {
|
inline while (i < instructions.len) : (i += 3) {
|
||||||
|
|
||||||
// This gets the digit from the "instruction". Can you
|
// This gets the digit from the "instruction". Can you
|
||||||
// figure out why we subtract '0' from it?
|
// figure out why we subtract '0' from it?
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue