Skip to content

Commit 791c7bd

Browse files
committed
replace text buttons above code blocks with icons
1 parent 4f64d58 commit 791c7bd

32 files changed

Lines changed: 340 additions & 326 deletions

content/Basics/11-structs unions.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,28 @@ weight: 11
1010
- Structs and unions can be nested.
1111
- Initialization follows C rules with `{ }`.
1212
- Unlike C it's possible to reassign the value of a struct or union with `{ }` as well.
13-
{{<end11>}}
13+
{{<end>}}
1414

15+
{{<defcod>}}
16+
import std::io;
17+
18+
union Point
19+
{
20+
// Nested struct
21+
struct
22+
{
23+
int x, y;
24+
}
25+
int[2] v;
26+
}
27+
28+
fn void main()
29+
{
30+
Point p = { 2, 3 };
31+
32+
io::printfn("Point: %d, %d", p.x, p.y);
33+
// Reassign the p value using a literal and named arguments:
34+
p = { .v = { 5, -1 } };
35+
io::printfn("Point: %d, %d", p.x, p.y);
36+
}
37+
{{</defcod>}}

content/Basics/13-slices.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,16 @@ int[] foo8 = list[^2..]; // ^2 means "len - 2"
2525
int[] foo8 = list[^2..^1];
2626
int[] foo9 = list[^2:2];
2727
```
28-
{{<end13>}}
28+
{{<end>}}
2929

30+
{{<defcod>}}
31+
import std::io;
32+
33+
fn void main()
34+
{
35+
int[6] list = { 2, 3, 5, 7, 11, 13 };
36+
int[] s = list[1..4];
37+
38+
io::printfn("%s", s);
39+
}
40+
{{</defcod>}}

content/Basics/6-foreach.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,27 @@ weight: 6
1010
- `foreach_r` conversely allows iteration from back and forward.
1111
- Either iterate over the value: `foreach (v : values)` or over index + value: `foreach (index, v : values)`
1212
- Values can also be retrieved by reference by using `&` in front of the variable name: `foreach (&v : values)`, this allows mutation of the element directly.
13-
{{<end6>}}
13+
{{<end>}}
14+
15+
{{<defcod>}}
16+
import std::io;
17+
18+
fn void test(float[] values)
19+
{
20+
io::printn("Forward");
21+
foreach (index, value : values)
22+
{
23+
io::printfn("%d: %f", index, value);
24+
}
25+
io::printn("Back");
26+
foreach_r (index, value : values)
27+
{
28+
io::printfn("%d: %f", index, value);
29+
}
30+
}
31+
32+
fn void main()
33+
{
34+
test({ 3.2, 5.0, 1.5, 7.135});
35+
}
36+
{{</defcod>}}

content/Basics/7-if.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,26 @@ aliases:
66
weight: 7
77
---
88
{{<start>}}
9-
{{<end7>}}
9+
{{<end>}}
1010

11+
{{<defcod>}}
12+
import std::io;
13+
14+
fn void main()
15+
{
16+
int x = 2;
17+
18+
if (x == 1)
19+
{
20+
io::printfn("1");
21+
}
22+
else if (x == 2)
23+
{
24+
io::printfn("2");
25+
}
26+
else
27+
{
28+
io::printfn("0");
29+
}
30+
}
31+
{{</defcod>}}

content/Basics/8-switch.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,26 @@ weight: 8
77
---
88
{{<start>}}
99
- `case` statements automatically break.
10-
{{<end8>}}
10+
{{<end>}}
1111

12+
{{<defcod>}}
13+
import std::io;
14+
import std::core::env;
15+
16+
fn void main()
17+
{
18+
io::printf("It's crucial to say ");
19+
switch (env::OS_TYPE)
20+
{
21+
case WIN32:
22+
io::printf("WNU-");
23+
case LINUX:
24+
io::printf("GNU-");
25+
case MACOS:
26+
io::printf("MNU-");
27+
default:
28+
io::printf("DUNNO-");
29+
}
30+
io::printfn("Linux.");
31+
}
32+
{{</defcod>}}

content/Basics/9-nextcase.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,29 @@ weight: 9
88
{{<start>}}
99
- Use `nextcase` to fallthrough to the next statement.
1010
- Empty case statements have implicit fallthrough.
11-
{{<end9>}}
11+
{{<end>}}
1212

13+
{{<defcod>}}
14+
import std::io;
15+
16+
fn void main()
17+
{
18+
String s = "Gone--";
19+
20+
switch (s)
21+
{
22+
case "Python":
23+
io::printf("Far from ");
24+
nextcase "C3";
25+
case "OOP++":
26+
io::printf("Definitely not ");
27+
nextcase;
28+
case "C3":
29+
case "C":
30+
io::printfn("OK");
31+
default:
32+
io::printf("Most likely not ");
33+
nextcase "C3";
34+
}
35+
}
36+
{{</defcod>}}

content/More/1-asm block.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,21 @@ weight: 1
77
---
88
{{<start>}}
99
- Asm blocks uses a common grammar for all types of processors.
10-
{{<end14>}}
10+
{{<end>}}
1111

12+
{{<defcod>}}
13+
import std::io;
14+
15+
fn void main()
16+
{
17+
int x = 3;
18+
19+
io::printfn("%d", x);
20+
asm
21+
{
22+
xorl $eax,$eax;
23+
movl x,$eax;
24+
}
25+
io::printfn("%d", x);
26+
}
27+
{{</defcod>}}

content/More/13-initializers finalizers.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,23 @@ weight: 13
77
---
88
{{<start>}}
99
- It is sometimes useful to run code at startup and shutdown. Static initializers and finalizers are special functions that are run at startup and shutdown respectively.
10-
{{<end26>}}
10+
{{<end>}}
1111

12+
{{<defcod>}}
13+
import std::io;
14+
15+
fn void hello() @init
16+
{
17+
io::printf("hello");
18+
}
19+
20+
fn void world() @finalizer
21+
{
22+
io::printfn("world");
23+
}
24+
25+
fn void main()
26+
{
27+
io::printf(", ");
28+
}
29+
{{</defcod>}}

content/More/14-extended switch.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,32 @@ weight: 14
77
---
88
{{<start>}}
99
- It's possible to have an extended `switch`, which can take any type of conditionals.
10-
{{<end27>}}
10+
{{<end>}}
1111

12+
{{<defcod>}}
13+
import std::io;
14+
15+
fn bool is_odd(int x)
16+
{
17+
return x % 2 == 1;
18+
}
19+
20+
fn void main()
21+
{
22+
int x = 2;
23+
24+
switch
25+
{
26+
case x == 1:
27+
io::printfn("One");
28+
case is_odd(x):
29+
io::printfn("Odd");
30+
case x < 0:
31+
io::printfn("Negative");
32+
case x == 0:
33+
io::printfn("Zero");
34+
default:
35+
io::printfn("Number was: %d", x);
36+
}
37+
}
38+
{{</defcod>}}

content/More/17-short function syntax.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,16 @@ weight: 17
88
{{<start>}}
99
- Rather than using `{}` to define the function body, one can use `=> <expr>`.
1010
- This behaves as if the function only contained a single return, returning the expression.
11-
{{<end30>}}
11+
{{<end>}}
1212

13+
{{<defcod>}}
14+
import std::io;
15+
16+
fn int square(int x) => x * x;
17+
fn void printx(int x) => io::printfn("%d", x);
18+
19+
fn void main()
20+
{
21+
printx(square(3));
22+
}
23+
{{</defcod>}}

0 commit comments

Comments
 (0)