Skip to content

Commit 726fe15

Browse files
authored
Merge pull request #24 from jean553/add_lifetimes_questions
Add lifetimes questions
2 parents 7658e6b + 3017b4d commit 726fe15

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

rust-exam/questions.json

+45
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,51 @@
312312
"c": "No, it is not allowed to shadow a variable if the borrowing reference is mutable but allowed if the borrowing reference is immutable",
313313
"d": "Yes",
314314
"answer": "d"
315+
},
316+
{
317+
"question": "Does the following code compile ?",
318+
"code": "fn foo<'a>(value: &'a u32) {}",
319+
"a": "No, a function cannot be empty",
320+
"b": "No, lifetimes cannot be part of the function <> parameters",
321+
"c": "Yes, 'a is the lifetime of the reference value",
322+
"d": "Yes, 'a is the lifetime of the foo method",
323+
"answer": "c"
324+
},
325+
{
326+
"question": "Does the following code compile ?",
327+
"code": "fn foo<'a>(value: &'a mut u8) {}",
328+
"a": "No, references with lifetimes cannot be mutable",
329+
"b": "No, lifetimes cannot be part of the function <> parameters",
330+
"c": "Yes, 'a is the lifetime of the foo method",
331+
"d": "Yes, 'a is the lifetime of the reference value",
332+
"answer": "d"
333+
},
334+
{
335+
"question": "Does the following code compile ?",
336+
"code": "struct Structure {\nreference: &u8\n}\n\nfn main() {\nlet value = 10;\nlet obj = Structure {\nreference: &value,\n};\n}",
337+
"a": "Yes",
338+
"b": "No, a lifetime is mandatory for structure variables that are references",
339+
"c": "No, a structure cannot contains references",
340+
"d": "No, a structure cannot contains only one variable as it makes no sense",
341+
"answer": "b"
342+
},
343+
{
344+
"question": "Does the following code compile ?",
345+
"code": "{\nlet x: &u8;\nlet v = 10;\nx = &v;\n}",
346+
"a": "Yes",
347+
"b": "No, the x reference must be initialized at the first line",
348+
"c": "No, the x reference is not mutable and overwritted with &v",
349+
"d": "No, v goes out of the scope when x still references it",
350+
"answer": "d"
351+
},
352+
{
353+
"question": "Does the following code compile ?",
354+
"code": "{\nlet v = 10;\nlet x: &u8;\nx = &v;\n}",
355+
"a": "Yes",
356+
"b": "No, the x reference must be initialized at the first line",
357+
"c": "No, the x reference is not mutable and overwritted with &v",
358+
"d": "No, v goes out of the scope when x still references it",
359+
"answer": "a"
315360
}
316361
]
317362
}

0 commit comments

Comments
 (0)