|
312 | 312 | "c": "No, it is not allowed to shadow a variable if the borrowing reference is mutable but allowed if the borrowing reference is immutable",
|
313 | 313 | "d": "Yes",
|
314 | 314 | "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" |
315 | 360 | }
|
316 | 361 | ]
|
317 | 362 | }
|
0 commit comments