rl_lang/docs/entries/tutorial/t1/
p12.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2pub static STEP_NULL: ConceptEntry = ConceptEntry {
3 name: "12. null",
4 summary: "null",
5 category: ConceptCategory::Types,
6 prerequisites: &[],
7 descriptions: &[
8 DescriptionEntry {
9 kind: DescriptionKind::Explanation,
10 title: None,
11 description: "null means the absence of a value. a variable can hold null regardless of its declared type. functions that do not explicitly return anything return null implicitly",
12 examples: &[
13 "dec int x = null\nprintln(x) // null\n\nfn do_nothing() {\n // returns null implicitly\n}",
14 ],
15 expected_output: &[],
16 },
17 DescriptionEntry {
18 kind: DescriptionKind::Explanation,
19 title: None,
20 description: "use is_null from std::types to check if a value is null before using it. this avoids surprises when a function might return nothing",
21 examples: &[
22 "get is_null from std::types\n\nfn find_even(arr[int] nums) -> int {\n for n in nums {\n if (n > 10) { return n }\n }\n return null // nothing matched\n}\n\ndec int res = find_even([1, 2, 3])\n\nif (is_null(res)) {\n println(\"nothing found\")\n} else {\n println(res)\n}",
23 ],
24 expected_output: &[],
25 },
26 DescriptionEntry {
27 kind: DescriptionKind::Explanation,
28 title: None,
29 description: "exercise: write a function find_first_correct that takes the guesses array and the secret number and returns the index of the first correct guess, or null if the player never guessed correctly\n\nhint: use a for loop with a counter variable alongside it",
30 examples: &[
31 "get is_null from std::types\n\nfn find_first_correct(arr[int] guesses, int secret) -> int {\n dec int i = 0\n for g in guesses {\n if (g == secret) { return i }\n i += 1\n }\n return null\n}\n\ndec int idx = find_first_correct(guesses, secret)\n\nif (is_null(idx)) {\n println(\"you never guessed correctly\")\n} else {\n println(format(\"you got it on guess number {}\", idx + 1))\n}",
32 ],
33 expected_output: &[],
34 },
35 ],
36 pitfalls: &[],
37 related: &[],
38 related_stdlib: &[],
39 since: None,
40};