Skip to main content

rl_lang/docs/entries/tutorial/t1/
p10.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2pub static STEP_STDLIB: ConceptEntry = ConceptEntry {
3    name: "10. the standard library",
4    summary: "the standard library",
5    category: ConceptCategory::Modules,
6    prerequisites: &[],
7    descriptions: &[
8        DescriptionEntry {
9            kind: DescriptionKind::Explanation,
10            title: None,
11            description: "rl comes with a standard library of useful functions organized into modules. you import what you need with get. you have already used std::io and std::str. here are the ones most useful for your game",
12            examples: &[
13                "get rand_int_range from std::random\nget concat, format from std::str\nget arr_push, len  from std::array",
14            ],
15            expected_output: &[],
16        },
17        DescriptionEntry {
18            kind: DescriptionKind::Explanation,
19            title: None,
20            description: "std::random gives you unpredictable numbers. rand_int_range returns a random int between two values inclusive. this is how your game will pick the secret number",
21            examples: &[
22                "get rand_int_range from std::random\n\ndec int secret = rand_int_range(1, 100)\nprintln(secret) // different every run",
23            ],
24            expected_output: &[],
25        },
26        DescriptionEntry {
27            kind: DescriptionKind::Explanation,
28            title: None,
29            description: "std::str has format which works like concat but uses {} as placeholders. cleaner for building messages with multiple values",
30            examples: &[
31                "get format from std::str\n\ndec string name = \"Mohamed\"\ndec int    score = 95\nprintln(format(\"hello {}, your score is {}\", name, score))\n// hello Mohamed, your score is 95",
32            ],
33            expected_output: &[],
34        },
35        DescriptionEntry {
36            kind: DescriptionKind::Explanation,
37            title: None,
38            description: "std::array has arr_sum, arr_min, arr_max for number arrays. useful for showing stats about the player's guesses at the end",
39            examples: &[
40                "get arr_sum, arr_min, arr_max from std::array\n\ndec arr[int] guesses = [30, 60, 42]\nprintln(arr_min(guesses)) // 30\nprintln(arr_max(guesses)) // 60\nprintln(arr_sum(guesses)) // 132",
41            ],
42            expected_output: &[],
43        },
44        DescriptionEntry {
45            kind: DescriptionKind::Explanation,
46            title: None,
47            description: "exercise: replace the hardcoded secret number with rand_int_range. then at the end of the game show the player their lowest guess, highest guess, and total number of guesses using arr_min, arr_max, and len\n\nexpected output:\n  game over!\n  total guesses: 3\n  lowest guess:  30\n  highest guess: 60",
48            examples: &[
49                "get rand_int_range        from std::random\nget arr_min, arr_max, len from std::array\nget format                from std::str\n\ndec int secret = rand_int_range(1, 100)\n\n// ... game loop ...\n\nprintln(\"game over!\")\nprintln(format(\"total guesses: {}\", len(guesses)))\nprintln(format(\"lowest guess:  {}\", arr_min(guesses)))\nprintln(format(\"highest guess: {}\", arr_max(guesses)))",
50            ],
51            expected_output: &[],
52        },
53    ],
54    pitfalls: &[],
55    related: &[],
56    related_stdlib: &[],
57    since: None,
58};