@@ -6,60 +6,60 @@ A language made to **touch the realms of cutting-edge possibility** in safety, c
66Example:
77
88``` rs
9- enum GameError {
10- IO (IOError ),
11- Game (String )
9+ enum GameError {
10+ IO { e : IOError },
11+ Alloc { e : AllocError },
12+ RandomFailed { e : RandomNumberError },
13+ ParseFailed { e : ParsingFailedError },
14+ Game { msg : String }
15+ }
16+
17+ interface Printable {
18+ fn print (): IOError! void
19+ }
20+
21+ struct GuessGame {
22+ target : i32 ,
23+ attempts : i32 ,
24+ }
25+
26+ impl Printable for GuessGame {
27+ fn print (): IOError! void {
28+ try std . out. println (" Welcome to Guess the Number!" )
29+ }
30+ }
31+
32+ fn random_number (min : i32 , max : i32 ): RandomNumberError! i32 {
33+ return try min + (std . random. int () % (max - min + 1 ))
34+ }
35+
36+ fn play_game (game : & mut GuessGame ): GameError! void {
37+ for _ in 0 .. 5 {
38+ try std . out. print (" Enter your guess: " ) as { e -> GameError . IO { e } }
39+ input : = try std . io. read () as { e -> GameError . IO { e } }
40+ guess : = try input . parse<i32 >() as { e -> GameError . ParseFailed { e } }
41+ match guess {
42+ g if g == game . target => {
43+ try std . out. println (" Correct! You win!" ) as { e -> GameError . IO { e } }
44+ return
45+ }
46+ g if g < game . target => try std . out. println (" Too low!" ) as { e -> GameError . IO { e } },
47+ g if g > game . target => try std . out. println (" Too high!" ) as { e -> GameError . IO { e } },
48+ }
49+ game . attempts += 1
50+ }
51+ try std . out. println (" Out of attempts! Game over." ) as { e -> GameError . IO { e } }
52+ return GameError . Game (" Out of attempts!" );
1253}
13-
14- interface Printable {
15- fn print (): ! void
16- }
17-
18- struct GuessGame {
19- target : i32 ,
20- attempts : i32 ,
21- }
22-
23- impl Printable for GuessGame {
24- fn print (): ! void {
25- try std . out. println (" Welcome to Guess the Number!" )
26- }
27- }
28-
29- fn random_number (min : i32 , max : i32 ): i32 {
30- return min + (std . random. int () % (max - min + 1 ))
31- }
32-
33- fn play_game (game : * mut GuessGame ): GameError! void {
34- for _ in 0 .. 5 {
35- try std . out. print (" Enter your guess: " )
36- guess : = try std . io. read (). parse<i32 >();
37-
38- match guess {
39- g if g == game . target => {
40- try std . out. println (" Correct! You win!" )
41- return
42- }
43- g if g < game . target => try std . out. println (" Too low!" ),
44- g if g > game . target => try std . out. println (" Too high!" ),
45- }
46- game . attempts += 1
47- }
48- try std . out. println (" Out of attempts! Game over." )
49- return GameError . Game (" Out of attempts!" );
50- }
51-
52- fn main (): GameError! void {
53- // Allocate the game on the heap
54- let mut game : * mut GuessGame = try std . mem. malloc (size_of <GuessGame >())
55- game = GuessGame { target : random_number (1 , 10 ), attempts : 0 }
56-
57- game . print ()
58- try play_game (game )
59-
60- try std . out. println (" Thanks for playing!" )
61-
62- // auto drop game
54+ fn main (): GameError! void {
55+ let mut game : Box <GuessGame > = try Box . new (GuessGame { target : try random_number (1 , 10 ) as { e -> GameError . RandomFailed { e } }, attempts : 0 }) as { e -> GameError . Alloc { e } }
56+
57+ try game . print () as { e -> GameError . IO { e } }
58+ try play_game (& mut game )
59+
60+ try std . out. println (" Thanks for playing!" ) as { e -> GameError . IO { e } }
61+
62+ // auto drop game, no borrow checking, its CTRC for the main logic and separation logic theory
6363}
6464```
6565
0 commit comments