[{"data":1,"prerenderedAt":3925},["ShallowReactive",2],{"page-\u002Frust\u002F05-functions":3},{"id":4,"title":5,"body":6,"description":30,"extension":3919,"meta":3920,"navigation":91,"path":3921,"seo":3922,"stem":3923,"__hash__":3924},"content\u002Frust\u002F05-functions.md","05 — Functions",{"type":7,"value":8,"toc":3883},"minimark",[9,13,192,352,525,690,911,1087,1302,1469,1605,1825,2037,2286,2531,2796,3132,3230,3465,3547,3773,3879],[10,11,5],"h1",{"id":12},"_05-functions",[14,15,17,22,78,140],"question-wrapper",{"language":16},"rust",[18,19,21],"h3",{"id":20},"q1-what-happens-when-you-try-to-compile-this-function","Q1. What happens when you try to compile this function?",[23,24,25],"code-wrapper",{"language":16},[26,27,31],"pre",{"className":28,"code":29,"language":16,"meta":30,"style":30},"language-rust shiki shiki-themes github-light github-dark","fn add(a, b) -> i32 {\n    a + b\n}\n","",[32,33,34,60,72],"code",{"__ignoreMap":30},[35,36,39,43,47,51,54,57],"span",{"class":37,"line":38},"line",1,[35,40,42],{"class":41},"svdQ7","fn",[35,44,46],{"class":45},"sIsaT"," add",[35,48,50],{"class":49},"ssxIu","(a, b) ",[35,52,53],{"class":41},"->",[35,55,56],{"class":45}," i32",[35,58,59],{"class":49}," {\n",[35,61,63,66,69],{"class":37,"line":62},2,[35,64,65],{"class":49},"    a ",[35,67,68],{"class":41},"+",[35,70,71],{"class":49}," b\n",[35,73,75],{"class":37,"line":74},3,[35,76,77],{"class":49},"}\n",[79,80,83,106,112,125],"ul",{"className":81},[82],"contains-task-list",[84,85,88,93,94,97,98,101,102,105],"li",{"className":86},[87],"task-list-item",[89,90],"input",{"disabled":91,"type":92},true,"checkbox"," It compiles, but ",[32,95,96],{},"a"," and ",[32,99,100],{},"b"," are inferred as generic ",[32,103,104],{},"T: Add"," parameters.",[84,107,109,111],{"className":108},[87],[89,110],{"disabled":91,"type":92}," It compiles only in debug builds; release builds require explicit types.",[84,113,115,117,118,121,122,124],{"className":114},[87],[89,116],{"disabled":91,"type":92}," It fails to compile — Rust requires every function parameter to have an explicit type annotation; type inference like this only applies to closures and ",[32,119,120],{},"let"," bindings, never to ",[32,123,42],{}," parameter lists.",[84,126,128,130,131,97,133,135,136,139],{"className":127},[87],[89,129],{"disabled":91,"type":92}," It compiles fine — Rust infers ",[32,132,96],{},[32,134,100],{}," as ",[32,137,138],{},"i32"," from the return type and usage, just like closures do.",[141,142,143,147,155],"details",{},[144,145,146],"summary",{},"Show Answer",[148,149,150,154],"p",{},[151,152,153],"strong",{},"Answer:"," C — It fails to compile — Rust requires every function parameter to have an explicit type annotation.",[148,156,157,160,161,164,165,167,168,171,172,175,176,178,179,181,182,184,185,187,188,191],{},[151,158,159],{},"Explanation:"," ",[151,162,163],{},"Idiom:"," the parser itself rejects this before type-checking even begins — every ",[32,166,42],{}," parameter must be written as ",[32,169,170],{},"name: Type",", full stop. This surprises people who've just seen closures like ",[32,173,174],{},"|a, b| a + b"," compile fine with types inferred from context: closures and ",[32,177,120],{}," bindings support inference, but plain ",[32,180,42],{}," items never do, by design, since a function's signature is part of its public contract and shouldn't silently shift based on how it's called. Rules out D (no such inference exists for ",[32,183,42],{},") and B (nothing in ",[32,186,42],{}," syntax implies generics without explicit ",[32,189,190],{},"\u003CT>"," bounds); A's debug\u002Frelease split is invented — this is a hard parse error present in every build profile.",[14,193,194,201,250,297],{"language":16},[18,195,197,198,200],{"id":196},"q2-this-function-is-declared-to-return-i32-what-happens-when-you-try-to-compile-it","Q2. This function is declared to return ",[32,199,138],{},". What happens when you try to compile it?",[23,202,203],{"language":16},[26,204,206],{"className":28,"code":205,"language":16,"meta":30,"style":30},"fn plus_one(x: i32) -> i32 {\n    x + 1;\n}\n",[32,207,208,232,246],{"__ignoreMap":30},[35,209,210,212,215,218,221,223,226,228,230],{"class":37,"line":38},[35,211,42],{"class":41},[35,213,214],{"class":45}," plus_one",[35,216,217],{"class":49},"(x",[35,219,220],{"class":41},":",[35,222,56],{"class":45},[35,224,225],{"class":49},") ",[35,227,53],{"class":41},[35,229,56],{"class":45},[35,231,59],{"class":49},[35,233,234,237,239,243],{"class":37,"line":62},[35,235,236],{"class":49},"    x ",[35,238,68],{"class":41},[35,240,242],{"class":241},"snvgF"," 1",[35,244,245],{"class":49},";\n",[35,247,248],{"class":37,"line":74},[35,249,77],{"class":49},[79,251,253,273,282,291],{"className":252},[82],[84,254,256,258,259,261,262,265,266,269,270,272],{"className":255},[87],[89,257],{"disabled":91,"type":92}," It fails to compile with a type mismatch: expected ",[32,260,138],{},", found ",[32,263,264],{},"()"," — the trailing semicolon turns ",[32,267,268],{},"x + 1"," into a statement, so the block's value becomes the unit type instead of the computed ",[32,271,138],{},".",[84,274,276,278,279,281],{"className":275},[87],[89,277],{"disabled":91,"type":92}," It compiles and returns ",[32,280,268],{}," as expected.",[84,283,285,287,288,272],{"className":284},[87],[89,286],{"disabled":91,"type":92}," It compiles and always returns ",[32,289,290],{},"0",[84,292,294,296],{"className":293},[87],[89,295],{"disabled":91,"type":92}," It compiles with a warning but panics at runtime when called.",[141,298,299,301,310],{},[144,300,146],{},[148,302,303,305,306,261,308,272],{},[151,304,153],{}," A — It fails to compile with a type mismatch: expected ",[32,307,138],{},[32,309,264],{},[148,311,312,160,314,317,318,322,323,326,327,329,330,332,333,335,336,339,340,343,344,347,348,351],{},[151,313,159],{},[151,315,316],{},"Debug:"," this is the single most common first-week Rust trap. A function body's value is determined by its final expression ",[319,320,321],"em",{},"if and only if"," that expression has no trailing semicolon; adding ",[32,324,325],{},";"," after ",[32,328,268],{}," converts it into a statement, and statements always evaluate to ",[32,331,264],{},". The block's overall value is then ",[32,334,264],{},", which doesn't match the declared ",[32,337,338],{},"-> i32",", so ",[32,341,342],{},"rustc"," rejects it with ",[32,345,346],{},"mismatched types: expected i32, found ()",". It's not a runtime issue (rules out D — this is caught entirely at compile time) and there's no fallback to a default value (rules out C). The fix is either deleting the semicolon or writing ",[32,349,350],{},"return x + 1;"," explicitly.",[14,353,354,362,447,490],{"language":16},[18,355,357,358,361],{"id":356},"q3-is-this-function-valid-and-what-does-it-demonstrate-about-return","Q3. Is this function valid, and what does it demonstrate about ",[32,359,360],{},"return","?",[23,363,364],{"language":16},[26,365,367],{"className":28,"code":366,"language":16,"meta":30,"style":30},"fn abs_diff(a: i32, b: i32) -> i32 {\n    if a > b {\n        return a - b;\n    }\n    b - a\n}\n",[32,368,369,398,412,425,431,442],{"__ignoreMap":30},[35,370,371,373,376,379,381,383,386,388,390,392,394,396],{"class":37,"line":38},[35,372,42],{"class":41},[35,374,375],{"class":45}," abs_diff",[35,377,378],{"class":49},"(a",[35,380,220],{"class":41},[35,382,56],{"class":45},[35,384,385],{"class":49},", b",[35,387,220],{"class":41},[35,389,56],{"class":45},[35,391,225],{"class":49},[35,393,53],{"class":41},[35,395,56],{"class":45},[35,397,59],{"class":49},[35,399,400,403,406,409],{"class":37,"line":62},[35,401,402],{"class":41},"    if",[35,404,405],{"class":49}," a ",[35,407,408],{"class":41},">",[35,410,411],{"class":49}," b {\n",[35,413,414,417,419,422],{"class":37,"line":74},[35,415,416],{"class":41},"        return",[35,418,405],{"class":49},[35,420,421],{"class":41},"-",[35,423,424],{"class":49}," b;\n",[35,426,428],{"class":37,"line":427},4,[35,429,430],{"class":49},"    }\n",[35,432,434,437,439],{"class":37,"line":433},5,[35,435,436],{"class":49},"    b ",[35,438,421],{"class":41},[35,440,441],{"class":49}," a\n",[35,443,445],{"class":37,"line":444},6,[35,446,77],{"class":49},[79,448,450,459,468,478],{"className":449},[82],[84,451,453,455,456,458],{"className":452},[87],[89,454],{"disabled":91,"type":92}," It fails to compile — every code path must end in an explicit ",[32,457,360],{}," statement, not a bare tail expression.",[84,460,462,464,465,467],{"className":461},[87],[89,463],{"disabled":91,"type":92}," It fails to compile — mixing ",[32,466,360],{}," and implicit returns in the same function is not allowed.",[84,469,471,473,474,477],{"className":470},[87],[89,472],{"disabled":91,"type":92}," It compiles, but the final ",[32,475,476],{},"b - a"," line is unreachable dead code that gets a compiler warning.",[84,479,481,483,484,486,487,489],{"className":480},[87],[89,482],{"disabled":91,"type":92}," It compiles fine — ",[32,485,360],{}," is only required to exit a function before reaching its final expression; the last line ",[32,488,476],{},", with no semicolon, is just as valid an implicit return.",[141,491,492,494,502],{},[144,493,146],{},[148,495,496,498,499,501],{},[151,497,153],{}," D — It compiles fine — ",[32,500,360],{}," is only required to exit a function before its final expression.",[148,503,504,506,507,510,511,514,515,518,519,521,522,524],{},[151,505,159],{}," Rust supports two equally valid ways to produce a function's return value: an explicit ",[32,508,509],{},"return expr;"," for early exits (as in the ",[32,512,513],{},"if"," branch here), and an implicit tail expression with no semicolon for the \"falls through to the end\" case. Both can coexist freely in the same function — there's no rule requiring consistency between them (rules out B), and a bare tail expression is not merely tolerated but idiomatic (rules out A). The final line is very much reachable whenever ",[32,516,517],{},"a \u003C= b",", since the ",[32,520,360],{}," only fires inside the ",[32,523,513],{}," block (rules out C).",[14,526,527,539,626,666],{"language":16},[18,528,530,531,534,535,538],{"id":529},"q4-square-is-called-in-main-before-its-definition-appears-in-the-file-what-happens","Q4. ",[32,532,533],{},"square"," is called in ",[32,536,537],{},"main"," before its definition appears in the file. What happens?",[23,540,541],{"language":16},[26,542,544],{"className":28,"code":543,"language":16,"meta":30,"style":30},"fn main() {\n    println!(\"{}\", square(5));\n}\n\nfn square(x: i32) -> i32 {\n    x * x\n}\n",[32,545,546,556,581,585,590,611,621],{"__ignoreMap":30},[35,547,548,550,553],{"class":37,"line":38},[35,549,42],{"class":41},[35,551,552],{"class":45}," main",[35,554,555],{"class":49},"() {\n",[35,557,558,561,564,568,571,573,575,578],{"class":37,"line":62},[35,559,560],{"class":45},"    println!",[35,562,563],{"class":49},"(",[35,565,567],{"class":566},"sJ6F3","\"{}\"",[35,569,570],{"class":49},", ",[35,572,533],{"class":45},[35,574,563],{"class":49},[35,576,577],{"class":241},"5",[35,579,580],{"class":49},"));\n",[35,582,583],{"class":37,"line":74},[35,584,77],{"class":49},[35,586,587],{"class":37,"line":427},[35,588,589],{"emptyLinePlaceholder":91},"\n",[35,591,592,594,597,599,601,603,605,607,609],{"class":37,"line":433},[35,593,42],{"class":41},[35,595,596],{"class":45}," square",[35,598,217],{"class":49},[35,600,220],{"class":41},[35,602,56],{"class":45},[35,604,225],{"class":49},[35,606,53],{"class":41},[35,608,56],{"class":45},[35,610,59],{"class":49},[35,612,613,615,618],{"class":37,"line":444},[35,614,236],{"class":49},[35,616,617],{"class":41},"*",[35,619,620],{"class":49}," x\n",[35,622,624],{"class":37,"line":623},7,[35,625,77],{"class":49},[79,627,629,635,645,654],{"className":628},[82],[84,630,632,634],{"className":631},[87],[89,633],{"disabled":91,"type":92}," Compile error — Rust requires functions to be declared or prototyped before use, like C.",[84,636,638,640,641,644],{"className":637},[87],[89,639],{"disabled":91,"type":92}," It compiles and runs fine, printing ",[32,642,643],{},"25"," — Rust resolves item names (functions, structs, and so on) within a scope regardless of their textual order, so forward references to later-defined functions are completely normal.",[84,646,648,650,651,653],{"className":647},[87],[89,649],{"disabled":91,"type":92}," Compile error — only ",[32,652,537],{}," may be defined first; all helper functions must precede their first call site.",[84,655,657,659,660,662,663,272],{"className":656},[87],[89,658],{"disabled":91,"type":92}," It compiles but only if ",[32,661,533],{}," is marked ",[32,664,665],{},"pub",[141,667,668,670,677],{},[144,669,146],{},[148,671,672,674,675,272],{},[151,673,153],{}," B — It compiles and runs fine, printing ",[32,676,643],{},[148,678,679,160,681,683,684,686,687,689],{},[151,680,159],{},[151,682,163],{}," unlike C, which needs a prototype (or the definition itself) to appear before first use, Rust's compiler does a full pass over a scope's items — collecting function signatures, struct definitions, and so on — before checking any bodies, so the textual order of top-level items is irrelevant to name resolution. This lets you put ",[32,685,537],{}," at the top of a file with helpers defined below it, a very common and idiomatic layout. ",[32,688,665],{}," (option D) controls cross-module\u002Fcross-crate visibility, not same-file resolution order, so it's irrelevant here since both functions are in the same module (rules out D and A\u002FC, which both invent a C-like ordering requirement that doesn't exist in Rust).",[14,691,692,706,792,862],{"language":16},[18,693,695,696,699,700,703,704,361],{"id":694},"q5-log-has-no-type-in-its-signature-what-happens-when-compiling-main","Q5. ",[32,697,698],{},"log"," has no ",[32,701,702],{},"-> Type"," in its signature. What happens when compiling ",[32,705,537],{},[23,707,708],{"language":16},[26,709,711],{"className":28,"code":710,"language":16,"meta":30,"style":30},"fn log(msg: &str) {\n    println!(\"{msg}\");\n}\n\nfn main() {\n    let x = log(\"hi\") + 1;\n}\n",[32,712,713,734,746,750,754,762,788],{"__ignoreMap":30},[35,714,715,717,720,723,725,728,731],{"class":37,"line":38},[35,716,42],{"class":41},[35,718,719],{"class":45}," log",[35,721,722],{"class":49},"(msg",[35,724,220],{"class":41},[35,726,727],{"class":41}," &",[35,729,730],{"class":45},"str",[35,732,733],{"class":49},") {\n",[35,735,736,738,740,743],{"class":37,"line":62},[35,737,560],{"class":45},[35,739,563],{"class":49},[35,741,742],{"class":566},"\"{msg}\"",[35,744,745],{"class":49},");\n",[35,747,748],{"class":37,"line":74},[35,749,77],{"class":49},[35,751,752],{"class":37,"line":427},[35,753,589],{"emptyLinePlaceholder":91},[35,755,756,758,760],{"class":37,"line":433},[35,757,42],{"class":41},[35,759,552],{"class":45},[35,761,555],{"class":49},[35,763,764,767,770,773,775,777,780,782,784,786],{"class":37,"line":444},[35,765,766],{"class":41},"    let",[35,768,769],{"class":49}," x ",[35,771,772],{"class":41},"=",[35,774,719],{"class":45},[35,776,563],{"class":49},[35,778,779],{"class":566},"\"hi\"",[35,781,225],{"class":49},[35,783,68],{"class":41},[35,785,242],{"class":241},[35,787,245],{"class":49},[35,789,790],{"class":37,"line":623},[35,791,77],{"class":49},[79,793,795,816,827,849],{"className":794},[82],[84,796,798,800,801,803,804,160,806,808,809,812,813,272],{"className":797},[87],[89,799],{"disabled":91,"type":92}," It compiles — ",[32,802,698],{}," implicitly returns ",[32,805,138],{},[32,807,290],{}," when no return type is given, so ",[32,810,811],{},"x"," becomes ",[32,814,815],{},"1",[84,817,819,821,822,824,825,272],{"className":818},[87],[89,820],{"disabled":91,"type":92}," It compiles — Rust automatically discards ",[32,823,698],{},"'s return value and treats the expression as just ",[32,826,815],{},[84,828,830,832,833,835,836,838,839,841,842,845,846,272],{"className":829},[87],[89,831],{"disabled":91,"type":92}," It fails to compile — ",[32,834,698],{},"'s implicit return type is the unit type ",[32,837,264],{},", and ",[32,840,264],{}," does not implement the ",[32,843,844],{},"Add"," trait needed for ",[32,847,848],{},"() + 1",[84,850,852,854,855,857,858,861],{"className":851},[87],[89,853],{"disabled":91,"type":92}," It fails to compile because ",[32,856,698],{}," must have an explicit ",[32,859,860],{},"-> ()"," annotation to be callable at all.",[141,863,864,866,880],{},[144,865,146],{},[148,867,868,870,871,873,874,876,877,879],{},[151,869,153],{}," C — It fails to compile — ",[32,872,698],{},"'s implicit return type is ",[32,875,264],{},", which has no ",[32,878,844],{}," implementation.",[148,881,882,884,885,887,888,890,891,893,894,897,898,901,902,904,905,907,908,910],{},[151,883,159],{}," Omitting ",[32,886,702],{}," entirely is exactly equivalent to writing ",[32,889,860],{}," — a function with no arrow always implicitly returns the unit type (rules out D, since the arrow is optional shorthand, not mandatory). ",[32,892,264],{}," is a real, if trivial, value — but it's a zero-sized marker type with no arithmetic trait implementations, so ",[32,895,896],{},"log(\"hi\") + 1"," fails to type-check with an error to the effect of \"cannot add ",[32,899,900],{},"{integer}"," to ",[32,903,264],{},".\" Nothing about calling a ",[32,906,264],{},"-returning function discards it into a usable numeric default (rules out A and B) — the unit value has to be explicitly used or ignored as ",[32,909,264],{},", not silently coerced into a number.",[14,912,913,917,987,1040],{"language":16},[18,914,916],{"id":915},"q6-what-does-this-program-print","Q6. What does this program print?",[23,918,919],{"language":16},[26,920,922],{"className":28,"code":921,"language":16,"meta":30,"style":30},"fn main() {\n    let y = {\n        let x = 3;\n        x + 1\n    };\n    println!(\"{y}\");\n}\n",[32,923,924,932,943,957,967,972,983],{"__ignoreMap":30},[35,925,926,928,930],{"class":37,"line":38},[35,927,42],{"class":41},[35,929,552],{"class":45},[35,931,555],{"class":49},[35,933,934,936,939,941],{"class":37,"line":62},[35,935,766],{"class":41},[35,937,938],{"class":49}," y ",[35,940,772],{"class":41},[35,942,59],{"class":49},[35,944,945,948,950,952,955],{"class":37,"line":74},[35,946,947],{"class":41},"        let",[35,949,769],{"class":49},[35,951,772],{"class":41},[35,953,954],{"class":241}," 3",[35,956,245],{"class":49},[35,958,959,962,964],{"class":37,"line":427},[35,960,961],{"class":49},"        x ",[35,963,68],{"class":41},[35,965,966],{"class":241}," 1\n",[35,968,969],{"class":37,"line":433},[35,970,971],{"class":49},"    };\n",[35,973,974,976,978,981],{"class":37,"line":444},[35,975,560],{"class":45},[35,977,563],{"class":49},[35,979,980],{"class":566},"\"{y}\"",[35,982,745],{"class":49},[35,984,985],{"class":37,"line":623},[35,986,77],{"class":49},[79,988,990,1005,1018,1032],{"className":989},[82],[84,991,993,160,995,998,999,1001,1002,272],{"className":992},[87],[89,994],{"disabled":91,"type":92},[32,996,997],{},"4",", because the block's trailing expression ",[32,1000,268],{}," (no semicolon) is the value the whole block evaluates to, and that value is assigned to ",[32,1003,1004],{},"y",[84,1006,1008,1010,1011,1014,1015,1017],{"className":1007},[87],[89,1009],{"disabled":91,"type":92}," Compile error — a ",[32,1012,1013],{},"{ }"," block cannot be assigned directly to a ",[32,1016,120],{}," binding.",[84,1019,1021,160,1023,1026,1027,1029,1030,272],{"className":1020},[87],[89,1022],{"disabled":91,"type":92},[32,1024,1025],{},"3",", because ",[32,1028,1004],{}," binds to the inner ",[32,1031,811],{},[84,1033,1035,1037,1038,272],{"className":1034},[87],[89,1036],{"disabled":91,"type":92}," It prints nothing — blocks used as expressions always evaluate to ",[32,1039,264],{},[141,1041,1042,1044,1052],{},[144,1043,146],{},[148,1045,1046,1048,1049,1051],{},[151,1047,153],{}," A — ",[32,1050,997],{},", because the block's trailing expression is its value.",[148,1053,1054,1056,1057,1060,1061,1063,1064,1066,1067,1069,1070,1072,1073,1076,1077,1079,1080,1082,1083,1086],{},[151,1055,159],{}," A bare ",[32,1058,1059],{},"{ ... }"," block is itself an expression, governed by the exact same rule as function bodies (Q2, Q3): its value is its own trailing expression without a semicolon. Here that's ",[32,1062,268],{},", which evaluates to ",[32,1065,997],{}," inside the block, so ",[32,1068,1004],{}," binds to ",[32,1071,997],{},". The inner ",[32,1074,1075],{},"let x = 3;"," is scoped entirely to the block and never leaks into the outer scope (rules out C — there is no outer ",[32,1078,811],{}," to shadow or bind to). This is a genuine departure from C-family languages, where ",[32,1081,1013],{}," is purely a scoping construct with no value of its own (rules out B and D, which both assume block-as-value doesn't work or defaults to unit — it only defaults to unit if the block's last line ",[319,1084,1085],{},"does"," end in a semicolon or has no trailing expression).",[14,1088,1089,1093,1219,1256],{"language":16},[18,1090,1092],{"id":1091},"q7-what-does-this-program-print","Q7. What does this program print?",[23,1094,1095],{"language":16},[26,1096,1098],{"className":28,"code":1097,"language":16,"meta":30,"style":30},"fn factorial(n: u64) -> u64 {\n    if n == 0 {\n        1\n    } else {\n        n * factorial(n - 1)\n    }\n}\n\nfn main() {\n    println!(\"{}\", factorial(5));\n}\n",[32,1099,1100,1123,1138,1143,1153,1172,1176,1180,1185,1194,1214],{"__ignoreMap":30},[35,1101,1102,1104,1107,1110,1112,1115,1117,1119,1121],{"class":37,"line":38},[35,1103,42],{"class":41},[35,1105,1106],{"class":45}," factorial",[35,1108,1109],{"class":49},"(n",[35,1111,220],{"class":41},[35,1113,1114],{"class":45}," u64",[35,1116,225],{"class":49},[35,1118,53],{"class":41},[35,1120,1114],{"class":45},[35,1122,59],{"class":49},[35,1124,1125,1127,1130,1133,1136],{"class":37,"line":62},[35,1126,402],{"class":41},[35,1128,1129],{"class":49}," n ",[35,1131,1132],{"class":41},"==",[35,1134,1135],{"class":241}," 0",[35,1137,59],{"class":49},[35,1139,1140],{"class":37,"line":74},[35,1141,1142],{"class":241},"        1\n",[35,1144,1145,1148,1151],{"class":37,"line":427},[35,1146,1147],{"class":49},"    } ",[35,1149,1150],{"class":41},"else",[35,1152,59],{"class":49},[35,1154,1155,1158,1160,1162,1165,1167,1169],{"class":37,"line":433},[35,1156,1157],{"class":49},"        n ",[35,1159,617],{"class":41},[35,1161,1106],{"class":45},[35,1163,1164],{"class":49},"(n ",[35,1166,421],{"class":41},[35,1168,242],{"class":241},[35,1170,1171],{"class":49},")\n",[35,1173,1174],{"class":37,"line":444},[35,1175,430],{"class":49},[35,1177,1178],{"class":37,"line":623},[35,1179,77],{"class":49},[35,1181,1183],{"class":37,"line":1182},8,[35,1184,589],{"emptyLinePlaceholder":91},[35,1186,1188,1190,1192],{"class":37,"line":1187},9,[35,1189,42],{"class":41},[35,1191,552],{"class":45},[35,1193,555],{"class":49},[35,1195,1197,1199,1201,1203,1205,1208,1210,1212],{"class":37,"line":1196},10,[35,1198,560],{"class":45},[35,1200,563],{"class":49},[35,1202,567],{"class":566},[35,1204,570],{"class":49},[35,1206,1207],{"class":45},"factorial",[35,1209,563],{"class":49},[35,1211,577],{"class":241},[35,1213,580],{"class":49},[35,1215,1217],{"class":37,"line":1216},11,[35,1218,77],{"class":49},[79,1220,1222,1230,1238,1248],{"className":1221},[82],[84,1223,1225,160,1227],{"className":1224},[87],[89,1226],{"disabled":91,"type":92},[32,1228,1229],{},"100",[84,1231,1233,160,1235],{"className":1232},[87],[89,1234],{"disabled":91,"type":92},[32,1236,1237],{},"24",[84,1239,1241,1243,1244,1247],{"className":1240},[87],[89,1242],{"disabled":91,"type":92}," Compile error — Rust does not support recursive function calls without an explicit ",[32,1245,1246],{},"#[recursive]"," attribute.",[84,1249,1251,160,1253],{"className":1250},[87],[89,1252],{"disabled":91,"type":92},[32,1254,1255],{},"120",[141,1257,1258,1260,1267],{},[144,1259,146],{},[148,1261,1262,1264,1265,272],{},[151,1263,153],{}," D — ",[32,1266,1255],{},[148,1268,1269,160,1271,1274,1275,1278,1279,1282,1283,1285,1286,1289,1290,1292,1293,1295,1296,838,1299,1301],{},[151,1270,159],{},[32,1272,1273],{},"factorial(5)"," unwinds as ",[32,1276,1277],{},"5 * factorial(4) = 5 * 4 * factorial(3) = ... = 5 * 4 * 3 * 2 * 1 * factorial(0)",", and the base case ",[32,1280,1281],{},"n == 0"," returns ",[32,1284,815],{},", giving ",[32,1287,1288],{},"5 * 4 * 3 * 2 * 1 = 120",". Rust supports ordinary recursion with no special opt-in attribute (rules out C — there's no such thing as ",[32,1291,1246],{},"; any function may call itself, limited only by available stack space for very deep recursion, not by any annotation). B and A are simple arithmetic slips (",[32,1294,1237],{}," is ",[32,1297,1298],{},"4!",[32,1300,1229],{}," doesn't correspond to any factorial in this chain).",[14,1303,1304,1312,1404,1431],{"language":16},[18,1305,1307,1308,1311],{"id":1306},"q8-this-is-an-attempt-to-overload-parse-based-on-parameter-type-as-is-common-in-c-or-java-what-happens-when-this-module-is-compiled","Q8. This is an attempt to overload ",[32,1309,1310],{},"parse"," based on parameter type, as is common in C++ or Java. What happens when this module is compiled?",[23,1313,1314],{"language":16},[26,1315,1317],{"className":28,"code":1316,"language":16,"meta":30,"style":30},"fn parse(input: &str) -> i32 {\n    input.len() as i32\n}\n\nfn parse(input: i32) -> i32 {\n    input * 2\n}\n",[32,1318,1319,1343,1362,1366,1370,1390,1400],{"__ignoreMap":30},[35,1320,1321,1323,1326,1329,1331,1333,1335,1337,1339,1341],{"class":37,"line":38},[35,1322,42],{"class":41},[35,1324,1325],{"class":45}," parse",[35,1327,1328],{"class":49},"(input",[35,1330,220],{"class":41},[35,1332,727],{"class":41},[35,1334,730],{"class":45},[35,1336,225],{"class":49},[35,1338,53],{"class":41},[35,1340,56],{"class":45},[35,1342,59],{"class":49},[35,1344,1345,1348,1350,1353,1356,1359],{"class":37,"line":62},[35,1346,1347],{"class":49},"    input",[35,1349,272],{"class":41},[35,1351,1352],{"class":45},"len",[35,1354,1355],{"class":49},"() ",[35,1357,1358],{"class":41},"as",[35,1360,1361],{"class":45}," i32\n",[35,1363,1364],{"class":37,"line":74},[35,1365,77],{"class":49},[35,1367,1368],{"class":37,"line":427},[35,1369,589],{"emptyLinePlaceholder":91},[35,1371,1372,1374,1376,1378,1380,1382,1384,1386,1388],{"class":37,"line":433},[35,1373,42],{"class":41},[35,1375,1325],{"class":45},[35,1377,1328],{"class":49},[35,1379,220],{"class":41},[35,1381,56],{"class":45},[35,1383,225],{"class":49},[35,1385,53],{"class":41},[35,1387,56],{"class":45},[35,1389,59],{"class":49},[35,1391,1392,1395,1397],{"class":37,"line":444},[35,1393,1394],{"class":49},"    input ",[35,1396,617],{"class":41},[35,1398,1399],{"class":241}," 2\n",[35,1401,1402],{"class":37,"line":623},[35,1403,77],{"class":49},[79,1405,1407,1413,1419,1425],{"className":1406},[82],[84,1408,1410,1412],{"className":1409},[87],[89,1411],{"disabled":91,"type":92}," It compiles, and Rust picks the correct overload based on the argument type at each call site.",[84,1414,1416,1418],{"className":1415},[87],[89,1417],{"disabled":91,"type":92}," It fails to compile with a \"duplicate definitions\" error — Rust has no function overloading; two items with the same name cannot coexist in the same scope regardless of differing parameter types. Idiomatic alternatives are distinct function names, generics, or trait methods.",[84,1420,1422,1424],{"className":1421},[87],[89,1423],{"disabled":91,"type":92}," It compiles, but only the second definition is kept; the first is silently shadowed.",[84,1426,1428,1430],{"className":1427},[87],[89,1429],{"disabled":91,"type":92}," It compiles only if the two functions have different return types.",[141,1432,1433,1435,1440],{},[144,1434,146],{},[148,1436,1437,1439],{},[151,1438,153],{}," B — It fails to compile with a \"duplicate definitions\" error.",[148,1441,1442,160,1444,1446,1447,1449,1450,1453,1454,1456,1457,1460,1461,1464,1465,1468],{},[151,1443,159],{},[151,1445,163],{}," unlike C++ or Java, Rust resolves function calls purely by name within a scope, with no overload-resolution step based on argument types — so defining two items named ",[32,1448,1310],{}," in the same module is simply a name collision (",[32,1451,1452],{},"the name 'parse' is defined multiple times","), regardless of how their signatures differ. There's no silent-shadowing behavior for top-level items the way there is for ",[32,1455,120],{}," bindings (rules out C), and differing return types don't rescue it either (rules out D) — the conflict is purely on the name. The idiomatic fixes are distinct names (",[32,1458,1459],{},"parse_str"," \u002F ",[32,1462,1463],{},"parse_int","), a generic function with a trait bound, or implementing a trait like ",[32,1466,1467],{},"FromStr"," per type.",[14,1470,1471,1475,1529,1579],{"language":16},[18,1472,1474],{"id":1473},"q9-what-happens-when-this-code-is-compiled","Q9. What happens when this code is compiled?",[23,1476,1477],{"language":16},[26,1478,1480],{"className":28,"code":1479,"language":16,"meta":30,"style":30},"fn connect(host: &str, port: u16 = 8080) {\n    println!(\"{host}:{port}\");\n}\n",[32,1481,1482,1514,1525],{"__ignoreMap":30},[35,1483,1484,1486,1489,1492,1494,1496,1498,1501,1503,1506,1509,1512],{"class":37,"line":38},[35,1485,42],{"class":41},[35,1487,1488],{"class":45}," connect",[35,1490,1491],{"class":49},"(host",[35,1493,220],{"class":41},[35,1495,727],{"class":41},[35,1497,730],{"class":45},[35,1499,1500],{"class":49},", port",[35,1502,220],{"class":41},[35,1504,1505],{"class":45}," u16",[35,1507,1508],{"class":41}," =",[35,1510,1511],{"class":241}," 8080",[35,1513,733],{"class":49},[35,1515,1516,1518,1520,1523],{"class":37,"line":62},[35,1517,560],{"class":45},[35,1519,563],{"class":49},[35,1521,1522],{"class":566},"\"{host}:{port}\"",[35,1524,745],{"class":49},[35,1526,1527],{"class":37,"line":74},[35,1528,77],{"class":49},[79,1530,1532,1546,1552,1562],{"className":1531},[82],[84,1533,1535,1537,1538,1541,1542,1545],{"className":1534},[87],[89,1536],{"disabled":91,"type":92}," It compiles, and calling ",[32,1539,1540],{},"connect(\"localhost\")"," uses ",[32,1543,1544],{},"port = 8080"," automatically.",[84,1547,1549,1551],{"className":1548},[87],[89,1550],{"disabled":91,"type":92}," It compiles, but the default is only applied in debug builds.",[84,1553,1555,1557,1558,1561],{"className":1554},[87],[89,1556],{"disabled":91,"type":92}," It compiles, and ",[32,1559,1560],{},"= 8080"," is treated as an assertion that must hold for any caller-supplied port.",[84,1563,1565,1567,1568,1571,1572,97,1575,1578],{"className":1564},[87],[89,1566],{"disabled":91,"type":92}," It fails to compile — Rust has no default-parameter syntax at all; every parameter must be supplied explicitly at every call site. The idiomatic workarounds are an ",[32,1569,1570],{},"Option\u003Cu16>"," parameter, multiple clearly-named functions (e.g. ",[32,1573,1574],{},"connect",[32,1576,1577],{},"connect_with_port","), or the builder pattern.",[141,1580,1581,1583,1588],{},[144,1582,146],{},[148,1584,1585,1587],{},[151,1586,153],{}," D — It fails to compile — Rust has no default-parameter syntax at all.",[148,1589,1590,160,1592,160,1594,1597,1598,1601,1602,1604],{},[151,1591,159],{},[151,1593,163],{},[32,1595,1596],{},"param: Type = value"," in a function signature isn't valid Rust syntax at all — this is a hard parse error, not merely a missing feature caught later. Coming from Python or JavaScript, where default parameter values are routine, this is a genuine gap: Rust deliberately requires every call site to supply every parameter explicitly, so the workarounds are an ",[32,1599,1600],{},"Option\u003CT>"," parameter with an explicit fallback inside the body, multiple distinctly-named functions, or a builder for many optional settings. None of A, B, or C describe real Rust behavior — there is no build-profile-dependent default (B) and no assertion semantics for ",[32,1603,772],{}," in a parameter list (C).",[14,1606,1607,1611,1709,1773],{"language":16},[18,1608,1610],{"id":1609},"q10-what-happens-when-this-is-compiled","Q10. What happens when this is compiled?",[23,1612,1613],{"language":16},[26,1614,1616],{"className":28,"code":1615,"language":16,"meta":30,"style":30},"fn print_owned(s: String) {\n    println!(\"{s}\");\n}\n\nfn main() {\n    let greeting = String::from(\"hello\");\n    print_owned(greeting);\n    println!(\"{greeting}\");\n}\n",[32,1617,1618,1635,1646,1650,1654,1662,1686,1694,1705],{"__ignoreMap":30},[35,1619,1620,1622,1625,1628,1630,1633],{"class":37,"line":38},[35,1621,42],{"class":41},[35,1623,1624],{"class":45}," print_owned",[35,1626,1627],{"class":49},"(s",[35,1629,220],{"class":41},[35,1631,1632],{"class":45}," String",[35,1634,733],{"class":49},[35,1636,1637,1639,1641,1644],{"class":37,"line":62},[35,1638,560],{"class":45},[35,1640,563],{"class":49},[35,1642,1643],{"class":566},"\"{s}\"",[35,1645,745],{"class":49},[35,1647,1648],{"class":37,"line":74},[35,1649,77],{"class":49},[35,1651,1652],{"class":37,"line":427},[35,1653,589],{"emptyLinePlaceholder":91},[35,1655,1656,1658,1660],{"class":37,"line":433},[35,1657,42],{"class":41},[35,1659,552],{"class":45},[35,1661,555],{"class":49},[35,1663,1664,1666,1669,1671,1673,1676,1679,1681,1684],{"class":37,"line":444},[35,1665,766],{"class":41},[35,1667,1668],{"class":49}," greeting ",[35,1670,772],{"class":41},[35,1672,1632],{"class":45},[35,1674,1675],{"class":41},"::",[35,1677,1678],{"class":45},"from",[35,1680,563],{"class":49},[35,1682,1683],{"class":566},"\"hello\"",[35,1685,745],{"class":49},[35,1687,1688,1691],{"class":37,"line":623},[35,1689,1690],{"class":45},"    print_owned",[35,1692,1693],{"class":49},"(greeting);\n",[35,1695,1696,1698,1700,1703],{"class":37,"line":1182},[35,1697,560],{"class":45},[35,1699,563],{"class":49},[35,1701,1702],{"class":566},"\"{greeting}\"",[35,1704,745],{"class":49},[35,1706,1707],{"class":37,"line":1187},[35,1708,77],{"class":49},[79,1710,1712,1740,1750,1765],{"className":1711},[82],[84,1713,1715,1717,1718,1721,1722,1725,1726,1729,1730,1732,1733,1735,1736,1739],{"className":1714},[87],[89,1716],{"disabled":91,"type":92}," It fails to compile — passing ",[32,1719,1720],{},"greeting"," by value into ",[32,1723,1724],{},"print_owned"," moves ownership of the ",[32,1727,1728],{},"String"," into the function; ",[32,1731,1720],{}," is no longer valid in ",[32,1734,537],{}," afterward, so the second ",[32,1737,1738],{},"println!"," triggers a \"use of moved value\" error.",[84,1741,1743,1745,1746,1749],{"className":1742},[87],[89,1744],{"disabled":91,"type":92}," It compiles and prints ",[32,1747,1748],{},"hello"," twice.",[84,1751,1753,93,1755,1757,1758,1761,1762,1764],{"className":1752},[87],[89,1754],{"disabled":91,"type":92},[32,1756,1720],{}," is empty (",[32,1759,1760],{},"\"\"",") by the time the second ",[32,1763,1738],{}," runs.",[84,1766,1768,854,1770,1772],{"className":1767},[87],[89,1769],{"disabled":91,"type":92},[32,1771,1728],{}," cannot be passed as a function argument at all.",[141,1774,1775,1777,1788],{},[144,1776,146],{},[148,1778,1779,1781,1782,1784,1785,1787],{},[151,1780,153],{}," A — It fails to compile — passing ",[32,1783,1720],{}," by value moves it, and the second ",[32,1786,1738],{}," uses a moved value.",[148,1789,1790,160,1792,1794,1795,1798,1799,1801,1802,1805,1806,1808,1809,1811,1812,1814,1815,1818,1819,1821,1822,1824],{},[151,1791,159],{},[32,1793,1728],{}," is a heap-allocated, non-",[32,1796,1797],{},"Copy"," type, so passing it by value into ",[32,1800,1724],{}," transfers ownership into the function's parameter ",[32,1803,1804],{},"s"," — ",[32,1807,1720],{}," in ",[32,1810,537],{}," is left uninitialized from the compiler's point of view, and any later use of it (the second ",[32,1813,1738],{},") is rejected at compile time with ",[32,1816,1817],{},"error[E0382]: use of moved value: 'greeting'",". This is a basic, unavoidable consequence of by-value parameter passing for owned types, not silent data loss (rules out C — the value isn't emptied at runtime, the code simply never runs because it doesn't compile) and ",[32,1820,1728],{}," is of course a perfectly ordinary argument type otherwise (rules out D). Contrast this with a ",[32,1823,1797],{}," type in the next question.",[14,1826,1827,1834,1920,1980],{"language":16},[18,1828,1830,1831,1833],{"id":1829},"q11-unlike-the-string-example-what-happens-here","Q11. Unlike the ",[32,1832,1728],{}," example, what happens here?",[23,1835,1836],{"language":16},[26,1837,1839],{"className":28,"code":1838,"language":16,"meta":30,"style":30},"fn print_value(n: i32) {\n    println!(\"{n}\");\n}\n\nfn main() {\n    let count = 5;\n    print_value(count);\n    println!(\"{count}\");\n}\n",[32,1840,1841,1856,1867,1871,1875,1883,1897,1905,1916],{"__ignoreMap":30},[35,1842,1843,1845,1848,1850,1852,1854],{"class":37,"line":38},[35,1844,42],{"class":41},[35,1846,1847],{"class":45}," print_value",[35,1849,1109],{"class":49},[35,1851,220],{"class":41},[35,1853,56],{"class":45},[35,1855,733],{"class":49},[35,1857,1858,1860,1862,1865],{"class":37,"line":62},[35,1859,560],{"class":45},[35,1861,563],{"class":49},[35,1863,1864],{"class":566},"\"{n}\"",[35,1866,745],{"class":49},[35,1868,1869],{"class":37,"line":74},[35,1870,77],{"class":49},[35,1872,1873],{"class":37,"line":427},[35,1874,589],{"emptyLinePlaceholder":91},[35,1876,1877,1879,1881],{"class":37,"line":433},[35,1878,42],{"class":41},[35,1880,552],{"class":45},[35,1882,555],{"class":49},[35,1884,1885,1887,1890,1892,1895],{"class":37,"line":444},[35,1886,766],{"class":41},[35,1888,1889],{"class":49}," count ",[35,1891,772],{"class":41},[35,1893,1894],{"class":241}," 5",[35,1896,245],{"class":49},[35,1898,1899,1902],{"class":37,"line":623},[35,1900,1901],{"class":45},"    print_value",[35,1903,1904],{"class":49},"(count);\n",[35,1906,1907,1909,1911,1914],{"class":37,"line":1182},[35,1908,560],{"class":45},[35,1910,563],{"class":49},[35,1912,1913],{"class":566},"\"{count}\"",[35,1915,745],{"class":49},[35,1917,1918],{"class":37,"line":1187},[35,1919,77],{"class":49},[79,1921,1923,1932,1948,1967],{"className":1922},[82],[84,1924,1926,1928,1929,1931],{"className":1925},[87],[89,1927],{"disabled":91,"type":92}," It fails to compile with the same \"use of moved value\" error as passing a ",[32,1930,1728],{}," by value.",[84,1933,1935,93,1937,812,1940,1808,1942,1944,1945,1947],{"className":1934},[87],[89,1936],{"disabled":91,"type":92},[32,1938,1939],{},"count",[32,1941,290],{},[32,1943,537],{}," after the call, since ownership still transfers even for ",[32,1946,1797],{}," types.",[84,1949,1951,1745,1953,1955,1956,1958,1959,1961,1962,1808,1964,1966],{"className":1950},[87],[89,1952],{"disabled":91,"type":92},[32,1954,577],{}," twice — ",[32,1957,138],{}," implements the ",[32,1960,1797],{}," trait, so passing it by value copies the bits into the function; the original ",[32,1963,1939],{},[32,1965,537],{}," remains valid and usable afterward.",[84,1968,1970,1972,1973,1975,1976,1979],{"className":1969},[87],[89,1971],{"disabled":91,"type":92}," It compiles only because ",[32,1974,1939],{}," happens to be a small value; larger integers like ",[32,1977,1978],{},"i128"," would move instead.",[141,1981,1982,1984,1996],{},[144,1983,146],{},[148,1985,1986,1988,1989,1991,1992,1295,1994,272],{},[151,1987,153],{}," C — It compiles and prints ",[32,1990,577],{}," twice, since ",[32,1993,138],{},[32,1995,1797],{},[148,1997,1998,2000,2001,570,2003,570,2006,838,2009,2012,2013,2015,2016,2018,2019,2022,2023,2025,2026,2028,2029,2031,2032,135,2034,2036],{},[151,1999,159],{}," Simple, fixed-size scalar types like ",[32,2002,138],{},[32,2004,2005],{},"bool",[32,2007,2008],{},"char",[32,2010,2011],{},"f64"," implement the ",[32,2014,1797],{}," trait, which changes what \"passing by value\" means: instead of moving ownership (Q10), the bits are duplicated, leaving both the original ",[32,2017,1939],{}," and the function's local ",[32,2020,2021],{},"n"," as independently valid, usable values. This directly contrasts with the ",[32,2024,1728],{}," case — the difference is entirely about the type implementing ",[32,2027,1797],{},", not about the specific value (rules out D — ",[32,2030,1978],{}," is just as ",[32,2033,1797],{},[32,2035,138],{},"; magnitude is irrelevant, only the type matters) and there's no ownership transfer to \"reset\" the original to a default (rules out B).",[14,2038,2039,2054,2171,2224],{"language":16},[18,2040,2042,2043,2046,2047,2050,2051,361],{"id":2041},"q12-bail-is-declared-to-return-the-never-type-and-always-panics-what-does-this-let-the-compiler-do-with-classify","Q12. ",[32,2044,2045],{},"bail"," is declared to return ",[32,2048,2049],{},"!"," (the \"never\" type) and always panics. What does this let the compiler do with ",[32,2052,2053],{},"classify",[23,2055,2056],{"language":16},[26,2057,2059],{"className":28,"code":2058,"language":16,"meta":30,"style":30},"fn bail(msg: &str) -> ! {\n    panic!(\"{msg}\");\n}\n\nfn classify(n: i32) -> i32 {\n    if n >= 0 {\n        n\n    } else {\n        bail(\"negative numbers are not supported\")\n    }\n}\n",[32,2060,2061,2085,2096,2100,2104,2125,2138,2143,2151,2163,2167],{"__ignoreMap":30},[35,2062,2063,2065,2068,2070,2072,2074,2076,2078,2080,2083],{"class":37,"line":38},[35,2064,42],{"class":41},[35,2066,2067],{"class":45}," bail",[35,2069,722],{"class":49},[35,2071,220],{"class":41},[35,2073,727],{"class":41},[35,2075,730],{"class":45},[35,2077,225],{"class":49},[35,2079,53],{"class":41},[35,2081,2082],{"class":41}," !",[35,2084,59],{"class":49},[35,2086,2087,2090,2092,2094],{"class":37,"line":62},[35,2088,2089],{"class":45},"    panic!",[35,2091,563],{"class":49},[35,2093,742],{"class":566},[35,2095,745],{"class":49},[35,2097,2098],{"class":37,"line":74},[35,2099,77],{"class":49},[35,2101,2102],{"class":37,"line":427},[35,2103,589],{"emptyLinePlaceholder":91},[35,2105,2106,2108,2111,2113,2115,2117,2119,2121,2123],{"class":37,"line":433},[35,2107,42],{"class":41},[35,2109,2110],{"class":45}," classify",[35,2112,1109],{"class":49},[35,2114,220],{"class":41},[35,2116,56],{"class":45},[35,2118,225],{"class":49},[35,2120,53],{"class":41},[35,2122,56],{"class":45},[35,2124,59],{"class":49},[35,2126,2127,2129,2131,2134,2136],{"class":37,"line":444},[35,2128,402],{"class":41},[35,2130,1129],{"class":49},[35,2132,2133],{"class":41},">=",[35,2135,1135],{"class":241},[35,2137,59],{"class":49},[35,2139,2140],{"class":37,"line":623},[35,2141,2142],{"class":49},"        n\n",[35,2144,2145,2147,2149],{"class":37,"line":1182},[35,2146,1147],{"class":49},[35,2148,1150],{"class":41},[35,2150,59],{"class":49},[35,2152,2153,2156,2158,2161],{"class":37,"line":1187},[35,2154,2155],{"class":45},"        bail",[35,2157,563],{"class":49},[35,2159,2160],{"class":566},"\"negative numbers are not supported\"",[35,2162,1171],{"class":49},[35,2164,2165],{"class":37,"line":1196},[35,2166,430],{"class":49},[35,2168,2169],{"class":37,"line":1216},[35,2170,77],{"class":49},[79,2172,2174,2188,2202,2212],{"className":2173},[82],[84,2175,2177,2179,2180,2182,2183,97,2185,2187],{"className":2176},[87],[89,2178],{"disabled":91,"type":92}," It fails to compile — the two branches of the ",[32,2181,513],{}," return different types (",[32,2184,138],{},[32,2186,2049],{},"), which normally must match exactly.",[84,2189,2191,800,2193,2195,2196,2198,2199,2201],{"className":2190},[87],[89,2192],{"disabled":91,"type":92},[32,2194,2049],{}," is a special type that coerces to whatever type is expected in context (here ",[32,2197,138],{},"), because a call to a function returning ",[32,2200,2049],{}," never actually produces a value to be wrong about, so it can stand in for any branch type.",[84,2203,2205,2207,2208,2211],{"className":2204},[87],[89,2206],{"disabled":91,"type":92}," It compiles, but only because ",[32,2209,2210],{},"panic!"," is a macro exempt from normal type-checking rules.",[84,2213,2215,2217,2218,2220,2221,2223],{"className":2214},[87],[89,2216],{"disabled":91,"type":92}," It fails to compile unless ",[32,2219,2045],{},"'s return type is changed to match ",[32,2222,138],{}," exactly.",[141,2225,2226,2228,2236],{},[144,2227,146],{},[148,2229,2230,2232,2233,2235],{},[151,2231,153],{}," B — It compiles — ",[32,2234,2049],{}," coerces to whatever type is expected in context.",[148,2237,2238,160,2240,160,2242,2244,2245,2247,2248,2250,2251,2253,2254,2257,2258,2260,2261,2250,2263,2265,2266,2269,2270,2272,2273,2275,2276,2278,2279,2282,2283,2285],{},[151,2239,159],{},[151,2241,163],{},[32,2243,2049],{},", the \"never\" type, marks a function (or expression) that never returns control to its caller — it always panics, exits the process, or loops forever. Because a diverging call genuinely never produces a value, the compiler is free to let ",[32,2246,2049],{}," unify with any type an ",[32,2249,513],{},"\u002F",[32,2252,1150],{}," or ",[32,2255,2256],{},"match"," branch needs, here ",[32,2259,138],{},", since there's no risk of a mismatched value actually surfacing at runtime. This is a deliberate special case, not a general relaxation of the rule that ",[32,2262,513],{},[32,2264,1150],{}," branches must agree in type (rules out A, which describes the ",[319,2267,2268],{},"normal"," rule that ",[32,2271,2049],{}," is specifically exempt from) — and it has nothing to do with ",[32,2274,2210],{}," being a macro (rules out C; the special-casing lives in the type system's treatment of ",[32,2277,2049],{}," itself, and applies equally to any ",[32,2280,2281],{},"-> !"," function, not just ones using ",[32,2284,2210],{},").",[14,2287,2288,2295,2426,2486],{"language":16},[18,2289,2291,2292,2294],{"id":2290},"q13-what-does-this-program-print-and-what-does-it-demonstrate-about-plain-fn-items","Q13. What does this program print, and what does it demonstrate about plain ",[32,2293,42],{}," items?",[23,2296,2297],{"language":16},[26,2298,2300],{"className":28,"code":2299,"language":16,"meta":30,"style":30},"fn square(x: i32) -> i32 {\n    x * x\n}\n\nfn apply(f: fn(i32) -> i32, value: i32) -> i32 {\n    f(value)\n}\n\nfn main() {\n    println!(\"{}\", apply(square, 4));\n}\n",[32,2301,2302,2322,2330,2334,2338,2378,2386,2390,2394,2402,2422],{"__ignoreMap":30},[35,2303,2304,2306,2308,2310,2312,2314,2316,2318,2320],{"class":37,"line":38},[35,2305,42],{"class":41},[35,2307,596],{"class":45},[35,2309,217],{"class":49},[35,2311,220],{"class":41},[35,2313,56],{"class":45},[35,2315,225],{"class":49},[35,2317,53],{"class":41},[35,2319,56],{"class":45},[35,2321,59],{"class":49},[35,2323,2324,2326,2328],{"class":37,"line":62},[35,2325,236],{"class":49},[35,2327,617],{"class":41},[35,2329,620],{"class":49},[35,2331,2332],{"class":37,"line":74},[35,2333,77],{"class":49},[35,2335,2336],{"class":37,"line":427},[35,2337,589],{"emptyLinePlaceholder":91},[35,2339,2340,2342,2345,2348,2350,2353,2355,2357,2359,2361,2363,2366,2368,2370,2372,2374,2376],{"class":37,"line":433},[35,2341,42],{"class":41},[35,2343,2344],{"class":45}," apply",[35,2346,2347],{"class":49},"(f",[35,2349,220],{"class":41},[35,2351,2352],{"class":41}," fn",[35,2354,563],{"class":49},[35,2356,138],{"class":45},[35,2358,225],{"class":49},[35,2360,53],{"class":41},[35,2362,56],{"class":45},[35,2364,2365],{"class":49},", value",[35,2367,220],{"class":41},[35,2369,56],{"class":45},[35,2371,225],{"class":49},[35,2373,53],{"class":41},[35,2375,56],{"class":45},[35,2377,59],{"class":49},[35,2379,2380,2383],{"class":37,"line":444},[35,2381,2382],{"class":45},"    f",[35,2384,2385],{"class":49},"(value)\n",[35,2387,2388],{"class":37,"line":623},[35,2389,77],{"class":49},[35,2391,2392],{"class":37,"line":1182},[35,2393,589],{"emptyLinePlaceholder":91},[35,2395,2396,2398,2400],{"class":37,"line":1187},[35,2397,42],{"class":41},[35,2399,552],{"class":45},[35,2401,555],{"class":49},[35,2403,2404,2406,2408,2410,2412,2415,2418,2420],{"class":37,"line":1196},[35,2405,560],{"class":45},[35,2407,563],{"class":49},[35,2409,567],{"class":566},[35,2411,570],{"class":49},[35,2413,2414],{"class":45},"apply",[35,2416,2417],{"class":49},"(square, ",[35,2419,997],{"class":241},[35,2421,580],{"class":49},[35,2423,2424],{"class":37,"line":1216},[35,2425,77],{"class":49},[79,2427,2429,2438,2450,2469],{"className":2428},[82],[84,2430,2432,2434,2435,2437],{"className":2431},[87],[89,2433],{"disabled":91,"type":92}," Compile error — ",[32,2436,533],{}," is a function, not a value, and cannot be passed as an argument without wrapping it in a closure first.",[84,2439,2441,2434,2443,2445,2446,2449],{"className":2440},[87],[89,2442],{"disabled":91,"type":92},[32,2444,2414],{},"'s parameter type ",[32,2447,2448],{},"fn(i32) -> i32"," is invalid syntax; function pointer types don't exist in Rust.",[84,2451,2453,160,2455,2458,2459,2461,2462,2464,2465,2468],{"className":2452},[87],[89,2454],{"disabled":91,"type":92},[32,2456,2457],{},"16"," — but only because ",[32,2460,2414],{}," implicitly converts ",[32,2463,533],{}," into a ",[32,2466,2467],{},"Box\u003Cdyn Fn(i32) -> i32>"," behind the scenes.",[84,2470,2472,160,2474,2476,2477,2479,2480,2482,2483,2485],{"className":2471},[87],[89,2473],{"disabled":91,"type":92},[32,2475,2457],{}," — a plain ",[32,2478,42],{}," item like ",[32,2481,533],{}," coerces to a function pointer of type ",[32,2484,2448],{},", a distinct concrete type from closures, and can be passed around, stored in variables, and called like any other value.",[141,2487,2488,2490,2499],{},[144,2489,146],{},[148,2491,2492,1264,2494,1805,2496,2498],{},[151,2493,153],{},[32,2495,2457],{},[32,2497,533],{}," coerces to a function pointer and is called like any other value.",[148,2500,2501,2503,2504,2506,2507,1805,2510,2513,2514,2517,2518,2250,2521,2250,2524,2527,2528,2530],{},[151,2502,159],{}," A named ",[32,2505,42],{}," item can be referenced by its bare name and coerces to the concrete function-pointer type ",[32,2508,2509],{},"fn(ArgTypes) -> RetType",[32,2511,2512],{},"apply(square, 4)"," computes ",[32,2515,2516],{},"square(4) = 16",". This is a real, lightweight, zero-allocation value type distinct from the ",[32,2519,2520],{},"Fn",[32,2522,2523],{},"FnMut",[32,2525,2526],{},"FnOnce"," closure traits (covered separately later) — no boxing or dynamic dispatch is involved here (rules out C), and function pointer types are entirely ordinary, valid Rust syntax used throughout callback-style APIs (rules out B). Functions absolutely are first-class values in this sense — passing ",[32,2529,533],{}," directly, with no closure wrapper needed, is exactly the point (rules out A).",[14,2532,2533,2537,2666,2724],{"language":16},[18,2534,2536],{"id":2535},"q14-what-does-this-program-print","Q14. What does this program print?",[23,2538,2539],{"language":16},[26,2540,2542],{"className":28,"code":2541,"language":16,"meta":30,"style":30},"const fn double(x: usize) -> usize {\n    x * 2\n}\n\nconst LEN: usize = double(4);\n\nfn main() {\n    let buffer = [0u8; LEN];\n    println!(\"{}\", buffer.len());\n}\n",[32,2543,2544,2569,2577,2581,2585,2606,2610,2618,2644,2662],{"__ignoreMap":30},[35,2545,2546,2549,2551,2554,2556,2558,2561,2563,2565,2567],{"class":37,"line":38},[35,2547,2548],{"class":41},"const",[35,2550,2352],{"class":41},[35,2552,2553],{"class":45}," double",[35,2555,217],{"class":49},[35,2557,220],{"class":41},[35,2559,2560],{"class":45}," usize",[35,2562,225],{"class":49},[35,2564,53],{"class":41},[35,2566,2560],{"class":45},[35,2568,59],{"class":49},[35,2570,2571,2573,2575],{"class":37,"line":62},[35,2572,236],{"class":49},[35,2574,617],{"class":41},[35,2576,1399],{"class":241},[35,2578,2579],{"class":37,"line":74},[35,2580,77],{"class":49},[35,2582,2583],{"class":37,"line":427},[35,2584,589],{"emptyLinePlaceholder":91},[35,2586,2587,2589,2592,2594,2596,2598,2600,2602,2604],{"class":37,"line":433},[35,2588,2548],{"class":41},[35,2590,2591],{"class":241}," LEN",[35,2593,220],{"class":41},[35,2595,2560],{"class":45},[35,2597,1508],{"class":41},[35,2599,2553],{"class":45},[35,2601,563],{"class":49},[35,2603,997],{"class":241},[35,2605,745],{"class":49},[35,2607,2608],{"class":37,"line":444},[35,2609,589],{"emptyLinePlaceholder":91},[35,2611,2612,2614,2616],{"class":37,"line":623},[35,2613,42],{"class":41},[35,2615,552],{"class":45},[35,2617,555],{"class":49},[35,2619,2620,2622,2625,2627,2630,2632,2635,2638,2641],{"class":37,"line":1182},[35,2621,766],{"class":41},[35,2623,2624],{"class":49}," buffer ",[35,2626,772],{"class":41},[35,2628,2629],{"class":49}," [",[35,2631,290],{"class":241},[35,2633,2634],{"class":45},"u8",[35,2636,2637],{"class":49},"; ",[35,2639,2640],{"class":241},"LEN",[35,2642,2643],{"class":49},"];\n",[35,2645,2646,2648,2650,2652,2655,2657,2659],{"class":37,"line":1187},[35,2647,560],{"class":45},[35,2649,563],{"class":49},[35,2651,567],{"class":566},[35,2653,2654],{"class":49},", buffer",[35,2656,272],{"class":41},[35,2658,1352],{"class":45},[35,2660,2661],{"class":49},"());\n",[35,2663,2664],{"class":37,"line":1196},[35,2665,77],{"class":49},[79,2667,2669,2692,2701,2713],{"className":2668},[82],[84,2670,2672,160,2674,2677,2678,135,2681,2684,2685,2688,2689,2691],{"className":2671},[87],[89,2673],{"disabled":91,"type":92},[32,2675,2676],{},"8"," — marking ",[32,2679,2680],{},"double",[32,2682,2683],{},"const fn"," makes it eligible for compile-time evaluation, so ",[32,2686,2687],{},"double(4)"," can be used to compute ",[32,2690,2640],{},", which in turn can be used as a fixed array length.",[84,2693,2695,2697,2698,2700],{"className":2694},[87],[89,2696],{"disabled":91,"type":92}," Compile error — function calls are never allowed in a ",[32,2699,2548],{}," initializer, only literal values.",[84,2702,2704,2434,2706,2708,2709,2712],{"className":2703},[87],[89,2705],{"disabled":91,"type":92},[32,2707,2680],{}," would need to be ",[32,2710,2711],{},"unsafe fn"," to be evaluated at compile time.",[84,2714,2716,160,2718,2720,2721,2723],{"className":2715},[87],[89,2717],{"disabled":91,"type":92},[32,2719,2676],{},", but only at runtime — ",[32,2722,2683],{}," is purely a documentation hint and has no effect on when the function actually executes.",[141,2725,2726,2728,2740],{},[144,2727,146],{},[148,2729,2730,1048,2732,1805,2734,2736,2737,2739],{},[151,2731,153],{},[32,2733,2676],{},[32,2735,2683],{}," makes ",[32,2738,2680],{}," usable in compile-time contexts like an array length.",[148,2741,2742,160,2744,2747,2748,2750,2751,2753,2754,2756,2757,2760,2761,2763,2764,2766,2767,2770,2771,225,2773,2775,2776,2778,2779,2781,2782,2785,2786,2788,2789,2791,2792,2795],{},[151,2743,159],{},[151,2745,2746],{},"Performance:"," the ",[32,2749,2548],{}," keyword on a function is what makes it eligible for evaluation during compilation, not just at runtime — ",[32,2752,2687],{}," can appear in a ",[32,2755,2548],{}," initializer and inside ",[32,2758,2759],{},"[0u8; LEN]","'s array-length position because ",[32,2762,2640],{}," is itself fully resolved to ",[32,2765,2676],{}," before codegen. An ",[319,2768,2769],{},"ordinary"," (non-",[32,2772,2548],{},[32,2774,42],{}," genuinely cannot be called from a const context (that restriction is real — it just doesn't apply here, which is what rules out B, since it describes plain ",[32,2777,42],{},", not ",[32,2780,2683],{},"). There's no ",[32,2783,2784],{},"unsafe"," requirement for compile-time evaluation (rules out C — ",[32,2787,2683],{}," is fully safe), and ",[32,2790,2683],{}," is far from cosmetic: it directly controls whether the function ",[319,2793,2794],{},"can"," run at compile time when the context demands it, though it may still be called normally at runtime too (rules out D).",[14,2797,2798,2813,3018,3076],{"language":16},[18,2799,2801,2802,2805,2806,2809,2810,361],{"id":2800},"q15-why-does-largest-require-the-t-partialord-bound-instead-of-being-written-as-fn-largesttlist-t-t","Q15. Why does ",[32,2803,2804],{},"largest"," require the ",[32,2807,2808],{},"T: PartialOrd"," bound instead of being written as ",[32,2811,2812],{},"fn largest\u003CT>(list: &[T]) -> T",[23,2814,2815],{"language":16},[26,2816,2818],{"className":28,"code":2817,"language":16,"meta":30,"style":30},"fn largest\u003CT: PartialOrd + Copy>(list: &[T]) -> T {\n    let mut largest = list[0];\n    for &item in list {\n        if item > largest {\n            largest = item;\n        }\n    }\n    largest\n}\n\nfn main() {\n    let numbers = vec![34, 50, 25, 100, 65];\n    println!(\"{}\", largest(&numbers));\n}\n",[32,2819,2820,2866,2885,2901,2914,2924,2929,2933,2938,2942,2946,2954,2992,3013],{"__ignoreMap":30},[35,2821,2822,2824,2827,2830,2833,2835,2838,2841,2844,2847,2849,2851,2854,2856,2859,2861,2864],{"class":37,"line":38},[35,2823,42],{"class":41},[35,2825,2826],{"class":45}," largest",[35,2828,2829],{"class":49},"\u003C",[35,2831,2832],{"class":45},"T",[35,2834,220],{"class":41},[35,2836,2837],{"class":45}," PartialOrd",[35,2839,2840],{"class":41}," +",[35,2842,2843],{"class":45}," Copy",[35,2845,2846],{"class":49},">(list",[35,2848,220],{"class":41},[35,2850,727],{"class":41},[35,2852,2853],{"class":49},"[",[35,2855,2832],{"class":45},[35,2857,2858],{"class":49},"]) ",[35,2860,53],{"class":41},[35,2862,2863],{"class":45}," T",[35,2865,59],{"class":49},[35,2867,2868,2870,2873,2876,2878,2881,2883],{"class":37,"line":62},[35,2869,766],{"class":41},[35,2871,2872],{"class":41}," mut",[35,2874,2875],{"class":49}," largest ",[35,2877,772],{"class":41},[35,2879,2880],{"class":49}," list[",[35,2882,290],{"class":241},[35,2884,2643],{"class":49},[35,2886,2887,2890,2892,2895,2898],{"class":37,"line":74},[35,2888,2889],{"class":41},"    for",[35,2891,727],{"class":41},[35,2893,2894],{"class":49},"item ",[35,2896,2897],{"class":41},"in",[35,2899,2900],{"class":49}," list {\n",[35,2902,2903,2906,2909,2911],{"class":37,"line":427},[35,2904,2905],{"class":41},"        if",[35,2907,2908],{"class":49}," item ",[35,2910,408],{"class":41},[35,2912,2913],{"class":49}," largest {\n",[35,2915,2916,2919,2921],{"class":37,"line":433},[35,2917,2918],{"class":49},"            largest ",[35,2920,772],{"class":41},[35,2922,2923],{"class":49}," item;\n",[35,2925,2926],{"class":37,"line":444},[35,2927,2928],{"class":49},"        }\n",[35,2930,2931],{"class":37,"line":623},[35,2932,430],{"class":49},[35,2934,2935],{"class":37,"line":1182},[35,2936,2937],{"class":49},"    largest\n",[35,2939,2940],{"class":37,"line":1187},[35,2941,77],{"class":49},[35,2943,2944],{"class":37,"line":1196},[35,2945,589],{"emptyLinePlaceholder":91},[35,2947,2948,2950,2952],{"class":37,"line":1216},[35,2949,42],{"class":41},[35,2951,552],{"class":45},[35,2953,555],{"class":49},[35,2955,2957,2959,2962,2964,2967,2969,2972,2974,2977,2979,2981,2983,2985,2987,2990],{"class":37,"line":2956},12,[35,2958,766],{"class":41},[35,2960,2961],{"class":49}," numbers ",[35,2963,772],{"class":41},[35,2965,2966],{"class":45}," vec!",[35,2968,2853],{"class":49},[35,2970,2971],{"class":241},"34",[35,2973,570],{"class":49},[35,2975,2976],{"class":241},"50",[35,2978,570],{"class":49},[35,2980,643],{"class":241},[35,2982,570],{"class":49},[35,2984,1229],{"class":241},[35,2986,570],{"class":49},[35,2988,2989],{"class":241},"65",[35,2991,2643],{"class":49},[35,2993,2995,2997,2999,3001,3003,3005,3007,3010],{"class":37,"line":2994},13,[35,2996,560],{"class":45},[35,2998,563],{"class":49},[35,3000,567],{"class":566},[35,3002,570],{"class":49},[35,3004,2804],{"class":45},[35,3006,563],{"class":49},[35,3008,3009],{"class":41},"&",[35,3011,3012],{"class":49},"numbers));\n",[35,3014,3016],{"class":37,"line":3015},14,[35,3017,77],{"class":49},[79,3019,3021,3036,3058,3067],{"className":3020},[82],[84,3022,3024,3026,3027,3030,3031,3033,3034,272],{"className":3023},[87],[89,3025],{"disabled":91,"type":92}," It doesn't actually need the bound — ",[32,3028,3029],{},"PartialOrd"," is inferred automatically for any ",[32,3032,2832],{}," used with ",[32,3035,408],{},[84,3037,3039,3041,3042,3044,3045,3047,3048,3050,3051,3053,3054,3057],{"className":3038},[87],[89,3040],{"disabled":91,"type":92}," Without a bound, ",[32,3043,2832],{}," is completely unconstrained, and the compiler has no guarantee that the ",[32,3046,408],{}," operator (which requires ",[32,3049,3029],{},") is implemented for whatever concrete type ",[32,3052,2832],{}," ends up being — the bound is what makes ",[32,3055,3056],{},"item > largest"," valid to even write.",[84,3059,3061,3063,3064,3066],{"className":3060},[87],[89,3062],{"disabled":91,"type":92}," The bound is only cosmetic documentation; removing it would still compile since ",[32,3065,408],{}," works on all types by default.",[84,3068,3070,3072,3073,3075],{"className":3069},[87],[89,3071],{"disabled":91,"type":92}," The bound is required only because the list contains integers; generic functions over ",[32,3074,1728],{}," or custom structs never need it.",[141,3077,3078,3080,3090],{},[144,3079,146],{},[148,3081,3082,3084,3085,3087,3088,272],{},[151,3083,153],{}," B — Without the bound, the compiler can't guarantee ",[32,3086,408],{}," is implemented for ",[32,3089,2832],{},[148,3091,3092,1056,3094,3096,3097,3100,3101,3103,3104,3106,3107,3109,3110,3112,3113,3115,3116,3118,3119,160,3121,3123,3124,3126,3127,3129,3130,272],{},[151,3093,159],{},[32,3095,2832],{}," with no bounds could be instantiated with ",[319,3098,3099],{},"any"," type, including ones with no comparison operators defined at all — so the compiler must reject ",[32,3102,3056],{}," unless the signature itself promises that whatever ",[32,3105,2832],{}," is, it supports ",[32,3108,3029],{}," (which is what ",[32,3111,408],{}," desugars to). This is true regardless of which concrete type ",[32,3114,2804],{}," is eventually called with, including integers (rules out D, which mistakenly treats the requirement as integer-specific) — the same signature is called here with ",[32,3117,138],{}," but the bound is what makes it work generically for ",[319,3120,3099],{},[32,3122,3029],{}," type. Nothing about trait bounds is automatic or optional cosmetic decoration (rules out A and C) — omitting ",[32,3125,3029],{}," produces a real compile error the moment ",[32,3128,408],{}," is used on an unconstrained ",[32,3131,2832],{},[14,3133,3134,3146,3190],{},[18,3135,3137,3138,3141,3142,3145],{"id":3136},"q16-a-function-create_user-conceptually-wants-an-optional-nickname-that-defaults-to-the-users-real-name-when-not-provided-rust-has-no-default-argument-syntax-whats-the-idiomatic-way-to-model-this","Q16. A function ",[32,3139,3140],{},"create_user"," conceptually wants an optional ",[32,3143,3144],{},"nickname"," that defaults to the user's real name when not provided. Rust has no default-argument syntax. What's the idiomatic way to model this?",[79,3147,3149,3158,3171,3184],{"className":3148},[82],[84,3150,3152,3154,3155,3157],{"className":3151},[87],[89,3153],{"disabled":91,"type":92}," Overload ",[32,3156,3140],{}," with two versions, one taking a nickname and one without, and rely on the compiler to disambiguate by call site.",[84,3159,3161,3163,3164,3167,3168,3170],{"className":3160},[87],[89,3162],{"disabled":91,"type":92}," Accept ",[32,3165,3166],{},"nickname: &str"," and require every caller to pass an empty string ",[32,3169,1760],{}," to mean \"no nickname.\"",[84,3172,3174,3163,3176,3179,3180,3183],{"className":3173},[87],[89,3175],{"disabled":91,"type":92},[32,3177,3178],{},"nickname: Option\u003C&str>"," (or provide a companion function like ",[32,3181,3182],{},"create_user_with_nickname","), and resolve the fallback to the real name explicitly inside the function body — making the \"no nickname supplied\" case an explicit, visible part of the signature and logic rather than hidden default-argument magic.",[84,3185,3187,3189],{"className":3186},[87],[89,3188],{"disabled":91,"type":92}," Use a macro to generate the missing default value at compile time, since only macros can simulate default arguments in Rust.",[141,3191,3192,3194,3203],{},[144,3193,146],{},[148,3195,3196,3198,3199,3202],{},[151,3197,153],{}," C — Accept ",[32,3200,3201],{},"Option\u003C&str>"," (or a companion function) and resolve the fallback explicitly.",[148,3204,3205,160,3207,3209,3210,3212,3213,2253,3216,3219,3220,3223,3224,3226,3227,3229],{},[151,3206,159],{},[151,3208,163],{}," since Rust has neither default parameters nor overloading (Q8, Q9), the idiomatic way to express \"this value is optional, with a computed fallback\" is ",[32,3211,1600],{}," in the signature — the caller passes ",[32,3214,3215],{},"None",[32,3217,3218],{},"Some(value)"," explicitly, and the function body handles both cases visibly, typically with ",[32,3221,3222],{},".unwrap_or_else(...)",". Overloading (A) isn't available in Rust at all, as established in Q8. An empty-string sentinel (B) is a classic anti-pattern: it conflates \"no value was given\" with \"a valid-looking but empty value,\" and nothing stops a caller from passing ",[32,3225,1760],{}," by mistake with a completely different intent. A macro (D) is unnecessary machinery for a problem ",[32,3228,1600],{}," already solves cleanly.",[14,3231,3232,3249,3343,3403],{"language":16},[18,3233,3235,3236,3238,3239,3242,3243,3246,3247,361],{"id":3234},"q17-both-match-arms-here-have-to-produce-a-value-of-the-same-type-for-the-match-to-type-check-why-does-this-compile-even-though-the-err-arms-panic-never-produces-an-i32","Q17. Both match arms here have to produce a value of the same type for the ",[32,3237,2256],{}," to type-check. Why does this compile even though the ",[32,3240,3241],{},"Err"," arm's ",[32,3244,3245],{},"panic!(...)"," never produces an ",[32,3248,138],{},[23,3250,3251],{"language":16},[26,3252,3254],{"className":28,"code":3253,"language":16,"meta":30,"style":30},"fn parse_or_die(input: &str) -> i32 {\n    match input.parse::\u003Ci32>() {\n        Ok(n) => n,\n        Err(_) => panic!(\"invalid number: {input}\"),\n    }\n}\n",[32,3255,3256,3279,3300,3314,3335,3339],{"__ignoreMap":30},[35,3257,3258,3260,3263,3265,3267,3269,3271,3273,3275,3277],{"class":37,"line":38},[35,3259,42],{"class":41},[35,3261,3262],{"class":45}," parse_or_die",[35,3264,1328],{"class":49},[35,3266,220],{"class":41},[35,3268,727],{"class":41},[35,3270,730],{"class":45},[35,3272,225],{"class":49},[35,3274,53],{"class":41},[35,3276,56],{"class":45},[35,3278,59],{"class":49},[35,3280,3281,3284,3287,3289,3291,3293,3295,3297],{"class":37,"line":62},[35,3282,3283],{"class":41},"    match",[35,3285,3286],{"class":49}," input",[35,3288,272],{"class":41},[35,3290,1310],{"class":45},[35,3292,1675],{"class":41},[35,3294,2829],{"class":49},[35,3296,138],{"class":45},[35,3298,3299],{"class":49},">() {\n",[35,3301,3302,3305,3308,3311],{"class":37,"line":74},[35,3303,3304],{"class":45},"        Ok",[35,3306,3307],{"class":49},"(n) ",[35,3309,3310],{"class":41},"=>",[35,3312,3313],{"class":49}," n,\n",[35,3315,3316,3319,3322,3324,3327,3329,3332],{"class":37,"line":427},[35,3317,3318],{"class":45},"        Err",[35,3320,3321],{"class":49},"(_) ",[35,3323,3310],{"class":41},[35,3325,3326],{"class":45}," panic!",[35,3328,563],{"class":49},[35,3330,3331],{"class":566},"\"invalid number: {input}\"",[35,3333,3334],{"class":49},"),\n",[35,3336,3337],{"class":37,"line":433},[35,3338,430],{"class":49},[35,3340,3341],{"class":37,"line":444},[35,3342,77],{"class":49},[79,3344,3346,3362,3370,3386],{"className":3345},[82],[84,3347,3349,3351,3352,1282,3354,3356,3357,3242,3360,272],{"className":3348},[87],[89,3350],{"disabled":91,"type":92}," It doesn't compile — ",[32,3353,2210],{},[32,3355,1728],{},", which mismatches the ",[32,3358,3359],{},"Ok",[32,3361,138],{},[84,3363,3365,160,3367,3369],{"className":3364},[87],[89,3366],{"disabled":91,"type":92},[32,3368,2210],{}," is special-cased by the match exhaustiveness checker to be silently skipped during type-checking.",[84,3371,3373,3375,3376,3379,3380,3382,3383,3385],{"className":3372},[87],[89,3374],{"disabled":91,"type":92}," It only compiles because ",[32,3377,3378],{},"parse_or_die","'s return type is inferred from the ",[32,3381,3359],{}," arm alone, and the ",[32,3384,3241],{}," arm's type is never checked.",[84,3387,3389,160,3391,3393,3394,3396,3397,3399,3400,3402],{"className":3388},[87],[89,3390],{"disabled":91,"type":92},[32,3392,3245],{}," has type ",[32,3395,2049],{}," (never), and like any diverging expression, ",[32,3398,2049],{}," coerces to match whatever type the other arms settle on — here ",[32,3401,138],{}," — because a diverging arm never actually returns control to produce a mismatched value.",[141,3404,3405,3407,3416],{},[144,3406,146],{},[148,3408,3409,1264,3411,3393,3413,3415],{},[151,3410,153],{},[32,3412,3245],{},[32,3414,2049],{},", which coerces to match the other arms' type.",[148,3417,3418,160,3420,3422,3423,3425,3426,570,3429,570,3432,3435,3436,3438,3439,3441,3442,838,3444,3446,3447,3449,3450,3452,3453,3455,3456,3458,3459,3461,3462,3464],{},[151,3419,159],{},[151,3421,163],{}," this is the same never-type coercion from Q12, now shown in its most common real-world form — using ",[32,3424,2210],{}," (or ",[32,3427,3428],{},"unreachable!",[32,3430,3431],{},"todo!",[32,3433,3434],{},".expect(...)","'s internal panic) as one arm of a ",[32,3437,2256],{}," whose other arms produce a real value. ",[32,3440,3245],{}," expands to code with type ",[32,3443,2049],{},[32,3445,2049],{}," unifies with any expected type, so the whole ",[32,3448,2256],{}," expression's type comes out as ",[32,3451,138],{}," even though one arm can never actually produce an ",[32,3454,138],{},". It's not that the arm goes unchecked (rules out B and C — every arm genuinely is type-checked; ",[32,3457,2049],{}," participates in that check, it isn't exempted from it) and ",[32,3460,2210],{}," certainly doesn't return ",[32,3463,1728],{}," — it never returns at all (rules out A).",[14,3466,3467,3475,3522],{},[18,3468,3470,3471,3474],{"id":3469},"q18-a-servernew-constructor-conceptually-could-take-up-to-six-independent-optional-settings-timeout-retries-tls-max-connections-and-so-on-given-rust-has-neither-default-arguments-nor-overloading-whats-considered-the-more-idiomatic-design-as-the-number-of-optional-settings-grows","Q18. A ",[32,3472,3473],{},"Server::new"," constructor conceptually could take up to six independent optional settings (timeout, retries, TLS, max connections, and so on). Given Rust has neither default arguments nor overloading, what's considered the more idiomatic design as the number of optional settings grows?",[79,3476,3478,3495,3504,3510],{"className":3477},[82],[84,3479,3481,3483,3484,3486,3487,3490,3491,3494],{"className":3480},[87],[89,3482],{"disabled":91,"type":92}," For a small number of optional settings, ",[32,3485,1600],{}," parameters (or a couple of named constructor functions) are fine; as the count grows, the builder pattern (a separate ",[32,3488,3489],{},"ServerBuilder"," with chainable setter methods and a final ",[32,3492,3493],{},".build()",") scales better by avoiding an unwieldy parameter list while keeping each call site self-documenting.",[84,3496,3498,3500,3501,3503],{"className":3497},[87],[89,3499],{"disabled":91,"type":92}," A single function with six ",[32,3502,1600],{}," parameters is always preferred, no matter how many optional settings exist.",[84,3505,3507,3509],{"className":3506},[87],[89,3508],{"disabled":91,"type":92}," Always use the builder pattern, even for a single optional parameter, since it's the only idiomatic Rust pattern for optional values.",[84,3511,3513,3515,3516,3518,3519,3521],{"className":3512},[87],[89,3514],{"disabled":91,"type":92}," Define the struct's fields as ",[32,3517,665],{}," and let callers construct it directly with struct-update syntax, since this is strictly superior to both ",[32,3520,1600],{}," parameters and the builder pattern in every case.",[141,3523,3524,3526,3534],{},[144,3525,146],{},[148,3527,3528,3530,3531,3533],{},[151,3529,153],{}," A — Small counts favor ",[32,3532,1600],{}," params or named functions; larger counts favor the builder pattern.",[148,3535,3536,160,3538,3540,3541,3543,3544,3546],{},[151,3537,159],{},[151,3539,163],{}," this is a judgment call that scales with complexity — a couple of ",[32,3542,1600],{}," parameters (Q16) or a couple of clearly-named constructors stay readable, but a six-parameter function (positional or not) becomes error-prone and hard to read at the call site, which is exactly the problem the builder pattern solves by naming each setting via a chained method call. Absolutist answers are the tell for the wrong choice here: always using six ",[32,3545,1600],{}," params (B) ignores how unwieldy that gets, always reaching for a builder even for one flag (C) is needless ceremony, and treating public struct-update construction as \"strictly superior... in every case\" (D) ignores that it bypasses validation and forces every field to be independently public, which a constructor or builder can avoid.",[14,3548,3549,3561,3655,3724],{"language":16},[18,3550,3552,3553,3556,3557,3560],{"id":3551},"q19-in-javascript-or-python-a-nested-function-like-multiply-would-close-over-factor-from-the-enclosing-scope-automatically-what-happens-when-this-rust-code-is-compiled","Q19. In JavaScript or Python, a nested function like ",[32,3554,3555],{},"multiply"," would close over ",[32,3558,3559],{},"factor"," from the enclosing scope automatically. What happens when this Rust code is compiled?",[23,3562,3563],{"language":16},[26,3564,3566],{"className":28,"code":3565,"language":16,"meta":30,"style":30},"fn main() {\n    let factor = 10;\n\n    fn multiply(x: i32) -> i32 {\n        x * factor\n    }\n\n    println!(\"{}\", multiply(5));\n}\n",[32,3567,3568,3576,3590,3594,3616,3625,3629,3633,3651],{"__ignoreMap":30},[35,3569,3570,3572,3574],{"class":37,"line":38},[35,3571,42],{"class":41},[35,3573,552],{"class":45},[35,3575,555],{"class":49},[35,3577,3578,3580,3583,3585,3588],{"class":37,"line":62},[35,3579,766],{"class":41},[35,3581,3582],{"class":49}," factor ",[35,3584,772],{"class":41},[35,3586,3587],{"class":241}," 10",[35,3589,245],{"class":49},[35,3591,3592],{"class":37,"line":74},[35,3593,589],{"emptyLinePlaceholder":91},[35,3595,3596,3599,3602,3604,3606,3608,3610,3612,3614],{"class":37,"line":427},[35,3597,3598],{"class":41},"    fn",[35,3600,3601],{"class":45}," multiply",[35,3603,217],{"class":49},[35,3605,220],{"class":41},[35,3607,56],{"class":45},[35,3609,225],{"class":49},[35,3611,53],{"class":41},[35,3613,56],{"class":45},[35,3615,59],{"class":49},[35,3617,3618,3620,3622],{"class":37,"line":433},[35,3619,961],{"class":49},[35,3621,617],{"class":41},[35,3623,3624],{"class":49}," factor\n",[35,3626,3627],{"class":37,"line":444},[35,3628,430],{"class":49},[35,3630,3631],{"class":37,"line":623},[35,3632,589],{"emptyLinePlaceholder":91},[35,3634,3635,3637,3639,3641,3643,3645,3647,3649],{"class":37,"line":1182},[35,3636,560],{"class":45},[35,3638,563],{"class":49},[35,3640,567],{"class":566},[35,3642,570],{"class":49},[35,3644,3555],{"class":45},[35,3646,563],{"class":49},[35,3648,577],{"class":241},[35,3650,580],{"class":49},[35,3652,3653],{"class":37,"line":1187},[35,3654,77],{"class":49},[79,3656,3658,3669,3683,3712],{"className":3657},[82],[84,3659,3661,1745,3663,3665,3666,3668],{"className":3660},[87],[89,3662],{"disabled":91,"type":92},[32,3664,2976],{}," — nested ",[32,3667,42],{}," items behave exactly like closures and capture enclosing local variables automatically.",[84,3670,3672,93,3674,3676,3677,3679,3680,3682],{"className":3671},[87],[89,3673],{"disabled":91,"type":92},[32,3675,3559],{}," is treated as ",[32,3678,290],{}," inside ",[32,3681,3555],{}," since it wasn't explicitly passed in.",[84,3684,3686,3688,3689,3691,3692,3694,3695,3697,3698,3700,3701,570,3703,3705,3706,3709,3710,272],{"className":3685},[87],[89,3687],{"disabled":91,"type":92}," It fails to compile — a plain ",[32,3690,42],{}," item, even one nested inside another function, never implicitly captures variables from its enclosing scope; ",[32,3693,3559],{}," is simply not in scope inside ",[32,3696,3555],{},"'s body, giving a \"cannot find value ",[32,3699,3559],{},"\" error. To capture ",[32,3702,3559],{},[32,3704,3555],{}," would need to be rewritten as a closure (",[32,3707,3708],{},"|x| x * factor",") instead of a plain ",[32,3711,42],{},[84,3713,3715,3717,3718,3720,3721,3723],{"className":3714},[87],[89,3716],{"disabled":91,"type":92}," It fails to compile because nested ",[32,3719,42],{}," items cannot reference ",[32,3722,138],{}," parameters at all.",[141,3725,3726,3728,3736],{},[144,3727,146],{},[148,3729,3730,3732,3733,3735],{},[151,3731,153],{}," C — It fails to compile — a plain ",[32,3734,42],{}," never implicitly captures its enclosing scope.",[148,3737,3738,160,3740,3742,3743,3746,3747,3749,3750,3753,3754,3756,3757,3759,3760,3762,3763,3765,3766,3768,3769,3772],{},[151,3739,159],{},[151,3741,316],{}," this is a frequent surprise for developers coming from JavaScript or Python, where a nested function definition is a closure by default. Rust draws a hard line between function ",[319,3744,3745],{},"items"," (",[32,3748,42],{},") and closures (",[32,3751,3752],{},"|...| ...","): a ",[32,3755,42],{},", whether at module scope or nested inside another function, has no implicit environment — it can only see its own parameters, its own locals, and other items, never a local variable from an enclosing function body. ",[32,3758,3559],{}," genuinely isn't visible inside ",[32,3761,3555],{},", giving a compile-time \"cannot find value\" error, not a runtime ",[32,3764,290],{}," (rules out B, which imagines a silent fallback that doesn't exist) and definitely not automatic capture (rules out A). Nested ",[32,3767,42],{}," items are completely legal and commonly used for local helpers (rules out D) — the restriction is specifically about capturing outer ",[319,3770,3771],{},"variables",", not about referencing parameter types.",[14,3774,3775,3784,3830],{},[18,3776,3778,3779,3781,3782,361],{"id":3777},"q20-which-statement-correctly-describes-a-key-limitation-of-const-fn-compared-to-an-ordinary-fn","Q20. Which statement correctly describes a key limitation of ",[32,3780,2683],{}," compared to an ordinary ",[32,3783,42],{},[79,3785,3787,3796,3811,3819],{"className":3786},[82],[84,3788,3790,3792,3793,3795],{"className":3789},[87],[89,3791],{"disabled":91,"type":92}," There is no difference at all — ",[32,3794,2683],{}," is purely a marker with no effect on what the function body may contain.",[84,3797,3799,3801,3802,3804,3805,3807,3808,3810],{"className":3798},[87],[89,3800],{"disabled":91,"type":92}," A ",[32,3803,2683],{}," is restricted to operations the compiler can guarantee are evaluable at compile time (no arbitrary heap allocation, no calls to non-",[32,3806,2548],{}," functions, limited trait usage, and so on); it is a strict subset of what an ordinary ",[32,3809,42],{}," may do, in exchange for being usable in const contexts like array lengths.",[84,3812,3814,3801,3816,3818],{"className":3813},[87],[89,3815],{"disabled":91,"type":92},[32,3817,2683],{}," can only be called at compile time; calling it at runtime with a non-constant argument is a compile error.",[84,3820,3822,3801,3824,3826,3827,3829],{"className":3821},[87],[89,3823],{"disabled":91,"type":92},[32,3825,2683],{}," automatically becomes ",[32,3828,2784],{}," because compile-time execution bypasses the borrow checker.",[141,3831,3832,3834,3842],{},[144,3833,146],{},[148,3835,3836,3838,3839,3841],{},[151,3837,153],{}," B — ",[32,3840,2683],{}," is restricted to a subset of compile-time-evaluable operations, in exchange for const-context usability.",[148,3843,3844,160,3846,160,3848,3850,3851,3853,3854,3856,3857,3859,3860,2250,3862,3865,3866,3869,3870,3872,3873,3875,3876,3878],{},[151,3845,159],{},[151,3847,2746],{},[32,3849,2683],{}," is a genuine trade-off, not a free-lunch marker (rules out A) — its body must stick to operations the compiler can prove are evaluable at compile time, which historically excludes things like arbitrary heap allocation or calling non-",[32,3852,2548],{}," functions (the exact boundary of what's permitted has grown across Rust editions, but it remains narrower than what an ordinary ",[32,3855,42],{}," allows). In exchange, a ",[32,3858,2683],{}," gains the ability to appear in const contexts such as array lengths (Q14) or ",[32,3861,2548],{},[32,3863,3864],{},"static"," initializers. A common misconception is that this makes it compile-time-",[319,3867,3868],{},"only"," (C) — in fact a ",[32,3871,2683],{}," is dual-purpose: called with a ",[32,3874,2548],{},"-eligible argument, it may run at compile time; called with an ordinary runtime value, it just runs as a normal function at runtime, no error involved. It also has nothing to do with ",[32,3877,2784],{}," or the borrow checker (rules out D) — const evaluation is fully safety-checked, just under a more restricted rule set.",[3880,3881,3882],"style",{},"html pre.shiki code .svdQ7, html code.shiki .svdQ7{--shiki-default:#D73A49;--shiki-github-dark:#F97583}html pre.shiki code .sIsaT, html code.shiki .sIsaT{--shiki-default:#6F42C1;--shiki-github-dark:#B392F0}html pre.shiki code .ssxIu, html code.shiki .ssxIu{--shiki-default:#24292E;--shiki-github-dark:#E1E4E8}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .github-dark .shiki span {color: var(--shiki-github-dark);background: var(--shiki-github-dark-bg);font-style: var(--shiki-github-dark-font-style);font-weight: var(--shiki-github-dark-font-weight);text-decoration: var(--shiki-github-dark-text-decoration);}html.github-dark .shiki span {color: var(--shiki-github-dark);background: var(--shiki-github-dark-bg);font-style: var(--shiki-github-dark-font-style);font-weight: var(--shiki-github-dark-font-weight);text-decoration: var(--shiki-github-dark-text-decoration);}html pre.shiki code .snvgF, html code.shiki .snvgF{--shiki-default:#005CC5;--shiki-github-dark:#79B8FF}html pre.shiki code .sJ6F3, html code.shiki .sJ6F3{--shiki-default:#032F62;--shiki-github-dark:#9ECBFF}",{"title":30,"searchDepth":62,"depth":62,"links":3884},[3885,3886,3888,3890,3892,3894,3895,3896,3898,3899,3900,3902,3904,3906,3907,3909,3911,3913,3915,3917],{"id":20,"depth":74,"text":21},{"id":196,"depth":74,"text":3887},"Q2. This function is declared to return i32. What happens when you try to compile it?",{"id":356,"depth":74,"text":3889},"Q3. Is this function valid, and what does it demonstrate about return?",{"id":529,"depth":74,"text":3891},"Q4. square is called in main before its definition appears in the file. What happens?",{"id":694,"depth":74,"text":3893},"Q5. log has no -> Type in its signature. What happens when compiling main?",{"id":915,"depth":74,"text":916},{"id":1091,"depth":74,"text":1092},{"id":1306,"depth":74,"text":3897},"Q8. This is an attempt to overload parse based on parameter type, as is common in C++ or Java. What happens when this module is compiled?",{"id":1473,"depth":74,"text":1474},{"id":1609,"depth":74,"text":1610},{"id":1829,"depth":74,"text":3901},"Q11. Unlike the String example, what happens here?",{"id":2041,"depth":74,"text":3903},"Q12. bail is declared to return ! (the \"never\" type) and always panics. What does this let the compiler do with classify?",{"id":2290,"depth":74,"text":3905},"Q13. What does this program print, and what does it demonstrate about plain fn items?",{"id":2535,"depth":74,"text":2536},{"id":2800,"depth":74,"text":3908},"Q15. Why does largest require the T: PartialOrd bound instead of being written as fn largest\u003CT>(list: &[T]) -> T?",{"id":3136,"depth":74,"text":3910},"Q16. A function create_user conceptually wants an optional nickname that defaults to the user's real name when not provided. Rust has no default-argument syntax. What's the idiomatic way to model this?",{"id":3234,"depth":74,"text":3912},"Q17. Both match arms here have to produce a value of the same type for the match to type-check. Why does this compile even though the Err arm's panic!(...) never produces an i32?",{"id":3469,"depth":74,"text":3914},"Q18. A Server::new constructor conceptually could take up to six independent optional settings (timeout, retries, TLS, max connections, and so on). Given Rust has neither default arguments nor overloading, what's considered the more idiomatic design as the number of optional settings grows?",{"id":3551,"depth":74,"text":3916},"Q19. In JavaScript or Python, a nested function like multiply would close over factor from the enclosing scope automatically. What happens when this Rust code is compiled?",{"id":3777,"depth":74,"text":3918},"Q20. Which statement correctly describes a key limitation of const fn compared to an ordinary fn?","md",{},"\u002Frust\u002F05-functions",{"title":5,"description":30},"rust\u002F05-functions","kljDDX0vkWpF4KQD8NwrigxXEeZhBNo7AezXiIA809E",1787335398370]