diff --git a/docs/32/index.html b/docs/32/index.html new file mode 120000 index 0000000..79c5d6f --- /dev/null +++ b/docs/32/index.html @@ -0,0 +1 @@ +../index.html \ No newline at end of file diff --git a/docs/questions.js b/docs/questions.js index 2a945b8..a95b38f 100644 --- a/docs/questions.js +++ b/docs/questions.js @@ -215,5 +215,12 @@ var questions = { "answer": "111222", "hint": "

During a method lookup, Rust automatically derefences and borrows the receiver\nin a well-defined order until it finds the first function with a suitable\nsignature. What is that order?

\n", "explanation": "

The Reference describes Rust's method lookup order. The relevant\nparagraph is:

\n
\n

Obtain [the candidate receiver type] by repeatedly dereferencing the receiver\nexpression's type, adding each type encountered to the list, then finally\nattempting an unsized coercion at the end, and adding the result type if that\nis successful. Then, for each candidate T, add &T and &mut T to the\nlist immediately after T.

\n
\n

Applying these rules to the given examples, we have:

\n\n" + }, + "32": { + "code": "fn check(x: i32) -> bool {\n print!(\"{}\", x);\n false\n}\n\nfn main() {\n match (1, 2) {\n (x, _) | (_, x) if check(x) => {\n print!(\"3\")\n }\n _ => print!(\"4\"),\n }\n}\n", + "difficulty": 2, + "answer": "124", + "hint": "

Either way would be confusing in different situations; there isn't a clear right\nbehavior that a hint could help identify. Guess both. :/

\n", + "explanation": "

This question covers two behaviors of match arms and guards.

\n

First, whether an if guard on a match-arm containing | applies to all\nalternatives in the match-arm or just to the one it is adjacent to. In the quiz\ncode, does check(x) execute at all for (x, _) or does it only cover the (_, x) case? We would expect 1 would get printed if and only if the latter is the\ncase. In fact 1 does get printed. A match-arm gets to have at most one if\nguard and that guard applies to all the |-separated alternatives in the arm.

\n

But second, this question also covers a kind of "backtracking" behavior of\nmatch-arms. After check(x) returns false on (x, _), does the whole match-arm\nfail to match at that point or does Rust move on to (_, x) and execute the\nguard a second time? We would expect 2 to be printed if and only if the latter\nis the case. In fact 2 does get printed; the guard is being run multiple\ntimes, once per |-separated alternative in the match-arm.

\n" } }; diff --git a/questions/000-or-pattern-guard.md b/questions/032-or-pattern-guard.md similarity index 100% rename from questions/000-or-pattern-guard.md rename to questions/032-or-pattern-guard.md diff --git a/questions/000-or-pattern-guard.rs b/questions/032-or-pattern-guard.rs similarity index 100% rename from questions/000-or-pattern-guard.rs rename to questions/032-or-pattern-guard.rs