[{"data":1,"prerenderedAt":2838},["ShallowReactive",2],{"page-\u002Frust\u002F03-variables-and-mutability":3},{"id":4,"title":5,"body":6,"description":30,"extension":2832,"meta":2833,"navigation":118,"path":2834,"seo":2835,"stem":2836,"__hash__":2837},"content\u002Frust\u002F03-variables-and-mutability.md","03 — Variables & Mutability",{"type":7,"value":8,"toc":2798},"minimark",[9,13,183,258,433,517,635,740,878,1063,1248,1478,1647,1805,1988,2182,2255,2325,2410,2559,2637,2794],[10,11,5],"h1",{"id":12},"_03-variables-mutability",[14,15,17,22,105,148],"question-wrapper",{"language":16},"rust",[18,19,21],"h3",{"id":20},"q1-what-happens-when-this-code-is-compiled","Q1. What happens when this code is compiled?",[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 main() {\n    let x = 5;\n    x = 6;\n    println!(\"{x}\");\n}\n","",[32,33,34,51,70,83,99],"code",{"__ignoreMap":30},[35,36,39,43,47],"span",{"class":37,"line":38},"line",1,[35,40,42],{"class":41},"svdQ7","fn",[35,44,46],{"class":45},"sIsaT"," main",[35,48,50],{"class":49},"ssxIu","() {\n",[35,52,54,57,60,63,67],{"class":37,"line":53},2,[35,55,56],{"class":41},"    let",[35,58,59],{"class":49}," x ",[35,61,62],{"class":41},"=",[35,64,66],{"class":65},"snvgF"," 5",[35,68,69],{"class":49},";\n",[35,71,73,76,78,81],{"class":37,"line":72},3,[35,74,75],{"class":49},"    x ",[35,77,62],{"class":41},[35,79,80],{"class":65}," 6",[35,82,69],{"class":49},[35,84,86,89,92,96],{"class":37,"line":85},4,[35,87,88],{"class":45},"    println!",[35,90,91],{"class":49},"(",[35,93,95],{"class":94},"sJ6F3","\"{x}\"",[35,97,98],{"class":49},");\n",[35,100,102],{"class":37,"line":101},5,[35,103,104],{"class":49},"}\n",[106,107,110,124,133,142],"ul",{"className":108},[109],"contains-task-list",[111,112,115,120,121],"li",{"className":113},[114],"task-list-item",[116,117],"input",{"disabled":118,"type":119},true,"checkbox"," It compiles and prints ",[32,122,123],{},"6",[111,125,127,129,130],{"className":126},[114],[116,128],{"disabled":118,"type":119}," Compile error: cannot assign twice to immutable variable ",[32,131,132],{},"x",[111,134,136,120,138,141],{"className":135},[114],[116,137],{"disabled":118,"type":119},[32,139,140],{},"5",", since the second assignment is silently ignored",[111,143,145,147],{"className":144},[114],[116,146],{"disabled":118,"type":119}," Runtime panic: attempted reassignment of immutable variable",[149,150,151,155,165],"details",{},[152,153,154],"summary",{},"Show Answer",[156,157,158,162,163],"p",{},[159,160,161],"strong",{},"Answer:"," B — Compile error: cannot assign twice to immutable variable ",[32,164,132],{},[156,166,167,170,171,174,175,178,179,182],{},[159,168,169],{},"Explanation:"," Variables in Rust are immutable by default; ",[32,172,173],{},"let x = 5;"," without ",[32,176,177],{},"mut"," forbids any further assignment, and the compiler catches this at compile time with ",[32,180,181],{},"error[E0384]",", not at runtime (ruling out D, since immutability is a compile-time property, not a runtime check). Nothing is silently ignored (C) — the code simply fails to compile.",[14,184,185,189,224],{},[18,186,188],{"id":187},"q2-what-is-required-to-make-the-reassignment-in-q1-valid","Q2. What is required to make the reassignment in Q1 valid?",[106,190,192,201,210,218],{"className":191},[109],[111,193,195,197,198],{"className":194},[114],[116,196],{"disabled":118,"type":119}," Wrapping the value in ",[32,199,200],{},"Cell::new(5)",[111,202,204,206,207],{"className":203},[114],[116,205],{"disabled":118,"type":119}," Declaring the variable with ",[32,208,209],{},"let mut x = 5;",[111,211,213,206,215],{"className":212},[114],[116,214],{"disabled":118,"type":119},[32,216,217],{},"const mut x: i32 = 5;",[111,219,221,223],{"className":220},[114],[116,222],{"disabled":118,"type":119}," Nothing — the original code already compiles",[149,225,226,228,235],{},[152,227,154],{},[156,229,230,232,233],{},[159,231,161],{}," B — Declaring the variable with ",[32,234,209],{},[156,236,237,239,240,242,243,246,247,250,251,253,254,257],{},[159,238,169],{}," The ",[32,241,177],{}," keyword explicitly opts a binding into mutability, after which ",[32,244,245],{},"x = 6;"," is a legal reassignment. ",[32,248,249],{},"const"," (C) can never be combined with ",[32,252,177],{}," — constants are always immutable and this is a syntax error, not a valid alternative. ",[32,255,256],{},"Cell::new"," (A) provides interior mutability for shared references, which is a different, more advanced mechanism not needed for a simple owned local variable.",[14,259,260,264,342,393],{"language":16},[18,261,263],{"id":262},"q3-what-is-the-difference-between-shadowing-and-mutation-in-this-snippet","Q3. What is the difference between shadowing and mutation in this snippet?",[23,265,266],{"language":16},[26,267,269],{"className":28,"code":268,"language":16,"meta":30,"style":30},"fn main() {\n    let x = 5;\n    let x = x + 1;\n    let x = x * 2;\n    println!(\"{x}\");\n}\n",[32,270,271,279,291,309,327,337],{"__ignoreMap":30},[35,272,273,275,277],{"class":37,"line":38},[35,274,42],{"class":41},[35,276,46],{"class":45},[35,278,50],{"class":49},[35,280,281,283,285,287,289],{"class":37,"line":53},[35,282,56],{"class":41},[35,284,59],{"class":49},[35,286,62],{"class":41},[35,288,66],{"class":65},[35,290,69],{"class":49},[35,292,293,295,297,299,301,304,307],{"class":37,"line":72},[35,294,56],{"class":41},[35,296,59],{"class":49},[35,298,62],{"class":41},[35,300,59],{"class":49},[35,302,303],{"class":41},"+",[35,305,306],{"class":65}," 1",[35,308,69],{"class":49},[35,310,311,313,315,317,319,322,325],{"class":37,"line":85},[35,312,56],{"class":41},[35,314,59],{"class":49},[35,316,62],{"class":41},[35,318,59],{"class":49},[35,320,321],{"class":41},"*",[35,323,324],{"class":65}," 2",[35,326,69],{"class":49},[35,328,329,331,333,335],{"class":37,"line":101},[35,330,88],{"class":45},[35,332,91],{"class":49},[35,334,95],{"class":94},[35,336,98],{"class":49},[35,338,340],{"class":37,"line":339},6,[35,341,104],{"class":49},[106,343,345,356,369,383],{"className":344},[109],[111,346,348,350,351,353,354],{"className":347},[114],[116,349],{"disabled":118,"type":119}," This is a compile error because ",[32,352,132],{}," is redeclared without ",[32,355,177],{},[111,357,359,361,362,365,366],{"className":358},[114],[116,360],{"disabled":118,"type":119}," This is valid shadowing: each ",[32,363,364],{},"let x = ..."," creates a brand-new binding that temporarily hides the previous one; it prints ",[32,367,368],{},"12",[111,370,372,374,375,377,378,380,381],{"className":371},[114],[116,373],{"disabled":118,"type":119}," This mutates the original ",[32,376,132],{}," in place, requiring ",[32,379,177],{},", and prints ",[32,382,368],{},[111,384,386,388,389,392],{"className":385},[114],[116,387],{"disabled":118,"type":119}," Only the first two ",[32,390,391],{},"let x"," lines are valid; the third causes a compile error for redefining twice",[149,394,395,397,406],{},[152,396,154],{},[156,398,399,401,402,365,404],{},[159,400,161],{}," B — This is valid shadowing: each ",[32,403,364],{},[32,405,368],{},[156,407,408,410,411,414,415,417,418,420,421,424,425,429,430,432],{},[159,409,169],{}," Shadowing with repeated ",[32,412,413],{},"let"," is explicitly allowed any number of times and does not require ",[32,416,177],{},", because each ",[32,419,413],{}," introduces a distinct variable (",[32,422,423],{},"(5+1)*2 = 12",") rather than mutating existing storage — unlike mutation, which changes a value in place through the same binding. This is a common point of confusion: shadowing ",[426,427,428],"em",{},"looks"," like reassignment but is a completely different mechanism (C incorrectly claims ",[32,431,177],{}," is required).",[14,434,435,445,483],{},[18,436,438,439,441,442,444],{"id":437},"q4-which-of-the-following-is-only-possible-with-shadowing-let-not-with-plain-mutation-mut","Q4. Which of the following is only possible with shadowing (",[32,440,413],{},"), not with plain mutation (",[32,443,177],{},")?",[106,446,448,454,471,477],{"className":447},[109],[111,449,451,453],{"className":450},[114],[116,452],{"disabled":118,"type":119}," Increasing a numeric value",[111,455,457,459,460,463,464,467,468],{"className":456},[114],[116,458],{"disabled":118,"type":119}," Changing the ",[426,461,462],{},"type"," of the variable binding, e.g. from ",[32,465,466],{},"&str"," to ",[32,469,470],{},"usize",[111,472,474,476],{"className":473},[114],[116,475],{"disabled":118,"type":119}," Using the variable inside a loop",[111,478,480,482],{"className":479},[114],[116,481],{"disabled":118,"type":119}," Passing the variable to a function",[149,484,485,487,498],{},[152,486,154],{},[156,488,489,491,492,463,494,467,496],{},[159,490,161],{}," B — Changing the ",[426,493,462],{},[32,495,466],{},[32,497,470],{},[156,499,500,502,503,505,506,509,510,512,513,516],{},[159,501,169],{}," A ",[32,504,177],{}," binding keeps a fixed type for its entire lifetime — ",[32,507,508],{},"let mut x = \"5\"; x = 5;"," is a compile error (type mismatch: expected ",[32,511,466],{},", found integer). Shadowing, however, creates an entirely new variable each time, so its type can freely differ: ",[32,514,515],{},"let x = \"5\"; let x: usize = x.parse().unwrap();"," is valid and idiomatic, commonly used to convert a value while reusing a meaningful name.",[14,518,519,526,554,596],{"language":16},[18,520,522,523,525],{"id":521},"q5-what-is-the-correct-syntax-and-semantics-of-a-const","Q5. What is the correct syntax and semantics of a ",[32,524,249],{},"?",[23,527,528],{"language":16},[26,529,531],{"className":28,"code":530,"language":16,"meta":30,"style":30},"const MAX_RETRIES: u32 = 3;\n",[32,532,533],{"__ignoreMap":30},[35,534,535,537,540,543,546,549,552],{"class":37,"line":38},[35,536,249],{"class":41},[35,538,539],{"class":65}," MAX_RETRIES",[35,541,542],{"class":41},":",[35,544,545],{"class":45}," u32",[35,547,548],{"class":41}," =",[35,550,551],{"class":65}," 3",[35,553,69],{"class":49},[106,555,557,566,577,588],{"className":556},[109],[111,558,560,562,563,565],{"className":559},[114],[116,561],{"disabled":118,"type":119}," ",[32,564,249],{}," values must have an explicit type annotation, are evaluated at compile time, and can never be mutated",[111,567,569,562,571,573,574,576],{"className":568},[114],[116,570],{"disabled":118,"type":119},[32,572,249],{}," values are inferred automatically like ",[32,575,413],{},", and type annotations are optional",[111,578,580,562,582,584,585,587],{"className":579},[114],[116,581],{"disabled":118,"type":119},[32,583,249],{}," behaves exactly like ",[32,586,413],{}," but with a different keyword for style purposes",[111,589,591,562,593,595],{"className":590},[114],[116,592],{"disabled":118,"type":119},[32,594,249],{}," values are computed once at program startup (runtime), similar to a lazily initialized static",[149,597,598,600,607],{},[152,599,154],{},[156,601,602,604,605,565],{},[159,603,161],{}," A — ",[32,606,249],{},[156,608,609,611,612,614,615,617,618,620,621,623,624,626,627,630,631,634],{},[159,610,169],{}," Unlike ",[32,613,413],{},", ",[32,616,249],{}," requires an explicit type annotation and its initializer must be a compile-time constant expression — the compiler effectively inlines its value wherever it's used, and ",[32,619,177],{}," is never permitted on a ",[32,622,249],{},". Option D describes lazy runtime initialization, which is not how plain ",[32,625,249],{}," works (that pattern would need something like ",[32,628,629],{},"std::sync::LazyLock","\u002F",[32,632,633],{},"once_cell"," for genuinely runtime-computed constants).",[14,636,637,647,706],{},[18,638,640,641,644,645,525],{"id":639},"q6-how-does-static-differ-from-const","Q6. How does ",[32,642,643],{},"static"," differ from ",[32,646,249],{},[106,648,650,659,681,695],{"className":649},[109],[111,651,653,655,656,658],{"className":652},[114],[116,654],{"disabled":118,"type":119}," They are identical in every respect; ",[32,657,643],{}," is just older syntax",[111,660,662,502,664,666,667,670,671,673,674,677,678,680],{"className":661},[114],[116,663],{"disabled":118,"type":119},[32,665,643],{}," has a fixed memory address and lives for the entire program (",[32,668,669],{},"'static"," lifetime), and can be declared ",[32,672,177],{}," (though accessing a mutable static requires ",[32,675,676],{},"unsafe","); a ",[32,679,249],{}," is inlined at each use site and has no fixed address",[111,682,684,562,686,688,689,691,692,694],{"className":683},[114],[116,685],{"disabled":118,"type":119},[32,687,249],{}," can be mutable with the ",[32,690,177],{}," keyword, but ",[32,693,643],{}," cannot",[111,696,698,562,700,702,703,705],{"className":697},[114],[116,699],{"disabled":118,"type":119},[32,701,643],{}," values must be recomputed every time they're accessed, while ",[32,704,249],{}," is cached",[149,707,708,710,726],{},[152,709,154],{},[156,711,712,714,715,717,718,720,721,723,724,680],{},[159,713,161],{}," B — A ",[32,716,643],{}," has a fixed memory address and lives for the entire program, and can be declared ",[32,719,177],{}," (requiring ",[32,722,676],{}," to access); a ",[32,725,249],{},[156,727,728,562,730,732,733,735,736,739],{},[159,729,169],{},[32,731,249],{}," is conceptually a compile-time substitution with no guaranteed single memory location (the compiler may duplicate its value at each usage), whereas ",[32,734,643],{}," reserves one fixed, program-lifetime memory location — this matters when you need a stable address (e.g., for FFI) or genuine shared mutable global state, which is only possible (and only unsafely) via ",[32,737,738],{},"static mut"," (option C has the mutability rule backwards).",[14,741,742,746,810,852],{"language":16},[18,743,745],{"id":744},"q7-what-does-this-program-print","Q7. What does this program print?",[23,747,748],{"language":16},[26,749,751],{"className":28,"code":750,"language":16,"meta":30,"style":30},"fn main() {\n    let spaces = \"   \";\n    let spaces = spaces.len();\n    println!(\"{spaces}\");\n}\n",[32,752,753,761,775,795,806],{"__ignoreMap":30},[35,754,755,757,759],{"class":37,"line":38},[35,756,42],{"class":41},[35,758,46],{"class":45},[35,760,50],{"class":49},[35,762,763,765,768,770,773],{"class":37,"line":53},[35,764,56],{"class":41},[35,766,767],{"class":49}," spaces ",[35,769,62],{"class":41},[35,771,772],{"class":94}," \"   \"",[35,774,69],{"class":49},[35,776,777,779,781,783,786,789,792],{"class":37,"line":72},[35,778,56],{"class":41},[35,780,767],{"class":49},[35,782,62],{"class":41},[35,784,785],{"class":49}," spaces",[35,787,788],{"class":41},".",[35,790,791],{"class":45},"len",[35,793,794],{"class":49},"();\n",[35,796,797,799,801,804],{"class":37,"line":85},[35,798,88],{"class":45},[35,800,91],{"class":49},[35,802,803],{"class":94},"\"{spaces}\"",[35,805,98],{"class":49},[35,807,808],{"class":37,"line":101},[35,809,104],{"class":49},[106,811,813,824,832,842],{"className":812},[109],[111,814,816,818,819,821,822],{"className":815},[114],[116,817],{"disabled":118,"type":119}," Compile error: mismatched types between ",[32,820,466],{}," and ",[32,823,470],{},[111,825,827,562,829],{"className":826},[114],[116,828],{"disabled":118,"type":119},[32,830,831],{},"3",[111,833,835,837,838,841],{"className":834},[114],[116,836],{"disabled":118,"type":119}," It prints the string ",[32,839,840],{},"\"   \""," itself",[111,843,845,847,848,851],{"className":844},[114],[116,846],{"disabled":118,"type":119}," Compile error: ",[32,849,850],{},"spaces"," was already defined",[149,853,854,856,863],{},[152,855,154],{},[156,857,858,860,861],{},[159,859,161],{}," B — ",[32,862,831],{},[156,864,865,867,868,871,872,874,875,877],{},[159,866,169],{}," This is the canonical shadowing example: the second ",[32,869,870],{},"let spaces"," shadows the first with a new binding of a completely different type (",[32,873,470],{},", holding the length of the original three-space string), which compiles fine specifically because shadowing (unlike mutation) permits type changes. Option A is the tempting mistake of assuming this is a ",[32,876,177],{}," reassignment, where a type mismatch really would occur.",[14,879,880,884,968,1020],{"language":16},[18,881,883],{"id":882},"q8-what-happens-with-this-nested-scope-shadowing","Q8. What happens with this nested-scope shadowing?",[23,885,886],{"language":16},[26,887,889],{"className":28,"code":888,"language":16,"meta":30,"style":30},"fn main() {\n    let x = 1;\n    {\n        let x = x * 100;\n        println!(\"inner: {x}\");\n    }\n    println!(\"outer: {x}\");\n}\n",[32,890,891,899,911,916,934,946,951,963],{"__ignoreMap":30},[35,892,893,895,897],{"class":37,"line":38},[35,894,42],{"class":41},[35,896,46],{"class":45},[35,898,50],{"class":49},[35,900,901,903,905,907,909],{"class":37,"line":53},[35,902,56],{"class":41},[35,904,59],{"class":49},[35,906,62],{"class":41},[35,908,306],{"class":65},[35,910,69],{"class":49},[35,912,913],{"class":37,"line":72},[35,914,915],{"class":49},"    {\n",[35,917,918,921,923,925,927,929,932],{"class":37,"line":85},[35,919,920],{"class":41},"        let",[35,922,59],{"class":49},[35,924,62],{"class":41},[35,926,59],{"class":49},[35,928,321],{"class":41},[35,930,931],{"class":65}," 100",[35,933,69],{"class":49},[35,935,936,939,941,944],{"class":37,"line":101},[35,937,938],{"class":45},"        println!",[35,940,91],{"class":49},[35,942,943],{"class":94},"\"inner: {x}\"",[35,945,98],{"class":49},[35,947,948],{"class":37,"line":339},[35,949,950],{"class":49},"    }\n",[35,952,954,956,958,961],{"class":37,"line":953},7,[35,955,88],{"class":45},[35,957,91],{"class":49},[35,959,960],{"class":94},"\"outer: {x}\"",[35,962,98],{"class":49},[35,964,966],{"class":37,"line":965},8,[35,967,104],{"class":49},[106,969,971,987,1001,1009],{"className":970},[109],[111,972,974,976,977,980,981,984,985],{"className":973},[114],[116,975],{"disabled":118,"type":119}," Prints ",[32,978,979],{},"inner: 100"," then ",[32,982,983],{},"outer: 100"," — the inner shadow permanently changes ",[32,986,132],{},[111,988,990,976,992,980,994,997,998,1000],{"className":989},[114],[116,991],{"disabled":118,"type":119},[32,993,979],{},[32,995,996],{},"outer: 1"," — the inner shadow only applies within its block scope; once the block ends, the outer ",[32,999,132],{}," is unaffected",[111,1002,1004,847,1006,1008],{"className":1003},[114],[116,1005],{"disabled":118,"type":119},[32,1007,132],{}," used before being fully shadowed",[111,1010,1012,976,1014,980,1017,1019],{"className":1011},[114],[116,1013],{"disabled":118,"type":119},[32,1015,1016],{},"inner: 1",[32,1018,983],{},", since block scoping is evaluated bottom-up",[149,1021,1022,1024,1034],{},[152,1023,154],{},[156,1025,1026,1028,1029,980,1031,1033],{},[159,1027,161],{}," B — Prints ",[32,1030,979],{},[32,1032,996],{}," — the inner shadow only applies within its block scope",[156,1035,1036,1038,1039,1041,1042,1045,1046,1049,1050,1052,1053,1056,1057,1059,1060,1062],{},[159,1037,169],{}," Shadowing is scoped like any other ",[32,1040,413],{}," binding: the inner ",[32,1043,1044],{},"let x = x * 100;"," creates a new variable visible only inside the ",[32,1047,1048],{},"{ }"," block; once that block ends, that shadow goes out of scope and the original outer ",[32,1051,132],{}," (still ",[32,1054,1055],{},"1",") becomes visible again. This is a key edge case distinguishing shadowing from mutation — a ",[32,1058,177],{}," variable's value change would persist outside a nested block that merely reassigns it (assuming no re-",[32,1061,413],{},"), whereas a shadow's effect is strictly scoped.",[14,1064,1065,1073,1149,1193],{"language":16},[18,1066,1068,1069,1072],{"id":1067},"q9-what-happens-when-an-immutable-variable-holding-a-vec-is-used-like-this","Q9. What happens when an immutable variable holding a ",[32,1070,1071],{},"Vec"," is used like this?",[23,1074,1075],{"language":16},[26,1076,1078],{"className":28,"code":1077,"language":16,"meta":30,"style":30},"fn main() {\n    let v = vec![1, 2, 3];\n    v.push(4);\n    println!(\"{v:?}\");\n}\n",[32,1079,1080,1088,1117,1134,1145],{"__ignoreMap":30},[35,1081,1082,1084,1086],{"class":37,"line":38},[35,1083,42],{"class":41},[35,1085,46],{"class":45},[35,1087,50],{"class":49},[35,1089,1090,1092,1095,1097,1100,1103,1105,1107,1110,1112,1114],{"class":37,"line":53},[35,1091,56],{"class":41},[35,1093,1094],{"class":49}," v ",[35,1096,62],{"class":41},[35,1098,1099],{"class":45}," vec!",[35,1101,1102],{"class":49},"[",[35,1104,1055],{"class":65},[35,1106,614],{"class":49},[35,1108,1109],{"class":65},"2",[35,1111,614],{"class":49},[35,1113,831],{"class":65},[35,1115,1116],{"class":49},"];\n",[35,1118,1119,1122,1124,1127,1129,1132],{"class":37,"line":72},[35,1120,1121],{"class":49},"    v",[35,1123,788],{"class":41},[35,1125,1126],{"class":45},"push",[35,1128,91],{"class":49},[35,1130,1131],{"class":65},"4",[35,1133,98],{"class":49},[35,1135,1136,1138,1140,1143],{"class":37,"line":85},[35,1137,88],{"class":45},[35,1139,91],{"class":49},[35,1141,1142],{"class":94},"\"{v:?}\"",[35,1144,98],{"class":49},[35,1146,1147],{"class":37,"line":101},[35,1148,104],{"class":49},[106,1150,1152,1165,1175,1184],{"className":1151},[109],[111,1153,1155,1157,1158,1160,1161,1164],{"className":1154},[114],[116,1156],{"disabled":118,"type":119}," Compiles fine — ",[32,1159,1126],{}," only needs ",[32,1162,1163],{},"&self",", so mutability of the binding doesn't matter",[111,1166,1168,1170,1171,1174],{"className":1167},[114],[116,1169],{"disabled":118,"type":119}," Compile error: cannot borrow ",[32,1172,1173],{},"v"," as mutable, as it is not declared as mutable",[111,1176,1178,1180,1181,1183],{"className":1177},[114],[116,1179],{"disabled":118,"type":119}," Compiles but panics at runtime because ",[32,1182,1173],{}," is immutable",[111,1185,1187,1189,1190,1192],{"className":1186},[114],[116,1188],{"disabled":118,"type":119}," Compiles fine because ",[32,1191,1071],{}," has interior mutability by default",[149,1194,1195,1197,1204],{},[152,1196,154],{},[156,1198,1199,1201,1202,1174],{},[159,1200,161],{}," B — Compile error: cannot borrow ",[32,1203,1173],{},[156,1205,1206,562,1208,1211,1212,1215,1216,1218,1219,1221,1222,1224,1225,1228,1229,1231,1232,1234,1235,630,1238,1241,1242,1244,1245,788],{},[159,1207,169],{},[32,1209,1210],{},"Vec::push"," takes ",[32,1213,1214],{},"&mut self",", and obtaining a mutable borrow of ",[32,1217,1173],{}," requires ",[32,1220,1173],{}," itself to be declared ",[32,1223,177],{}," — immutability is not just about direct reassignment (",[32,1226,1227],{},"v = ...",") but about disallowing any mutable access to the value's contents, including through method calls that need ",[32,1230,1214],{},". ",[32,1233,1071],{}," does not have interior mutability (D describes types like ",[32,1236,1237],{},"RefCell",[32,1239,1240],{},"Cell",", not ",[32,1243,1071],{},"), so the fix is simply ",[32,1246,1247],{},"let mut v = vec![1, 2, 3];",[14,1249,1250,1254,1352,1418],{"language":16},[18,1251,1253],{"id":1252},"q10-what-is-the-effect-of-shadowing-inside-a-loop-as-shown-here","Q10. What is the effect of shadowing inside a loop, as shown here?",[23,1255,1256],{"language":16},[26,1257,1259],{"className":28,"code":1258,"language":16,"meta":30,"style":30},"fn main() {\n    let mut count = 0;\n    for i in 0..3 {\n        let count = count + i;\n        println!(\"{count}\");\n    }\n    println!(\"final: {count}\");\n}\n",[32,1260,1261,1269,1286,1307,1322,1333,1337,1348],{"__ignoreMap":30},[35,1262,1263,1265,1267],{"class":37,"line":38},[35,1264,42],{"class":41},[35,1266,46],{"class":45},[35,1268,50],{"class":49},[35,1270,1271,1273,1276,1279,1281,1284],{"class":37,"line":53},[35,1272,56],{"class":41},[35,1274,1275],{"class":41}," mut",[35,1277,1278],{"class":49}," count ",[35,1280,62],{"class":41},[35,1282,1283],{"class":65}," 0",[35,1285,69],{"class":49},[35,1287,1288,1291,1294,1297,1299,1302,1304],{"class":37,"line":72},[35,1289,1290],{"class":41},"    for",[35,1292,1293],{"class":49}," i ",[35,1295,1296],{"class":41},"in",[35,1298,1283],{"class":65},[35,1300,1301],{"class":41},"..",[35,1303,831],{"class":65},[35,1305,1306],{"class":49}," {\n",[35,1308,1309,1311,1313,1315,1317,1319],{"class":37,"line":85},[35,1310,920],{"class":41},[35,1312,1278],{"class":49},[35,1314,62],{"class":41},[35,1316,1278],{"class":49},[35,1318,303],{"class":41},[35,1320,1321],{"class":49}," i;\n",[35,1323,1324,1326,1328,1331],{"class":37,"line":101},[35,1325,938],{"class":45},[35,1327,91],{"class":49},[35,1329,1330],{"class":94},"\"{count}\"",[35,1332,98],{"class":49},[35,1334,1335],{"class":37,"line":339},[35,1336,950],{"class":49},[35,1338,1339,1341,1343,1346],{"class":37,"line":953},[35,1340,88],{"class":45},[35,1342,91],{"class":49},[35,1344,1345],{"class":94},"\"final: {count}\"",[35,1347,98],{"class":49},[35,1349,1350],{"class":37,"line":965},[35,1351,104],{"class":49},[106,1353,1355,1379,1394,1405],{"className":1354},[109],[111,1356,1358,976,1360,614,1363,614,1365,821,1367,1370,1371,1374,1375,1378],{"className":1357},[114],[116,1359],{"disabled":118,"type":119},[32,1361,1362],{},"0",[32,1364,1055],{},[32,1366,1109],{},[32,1368,1369],{},"final: 0"," — the shadowed ",[32,1372,1373],{},"count"," inside the loop body never affects the outer ",[32,1376,1377],{},"mut count",", which is never actually mutated anywhere in this program",[111,1380,1382,976,1384,614,1386,614,1388,821,1390,1393],{"className":1381},[114],[116,1383],{"disabled":118,"type":119},[32,1385,1362],{},[32,1387,1055],{},[32,1389,831],{},[32,1391,1392],{},"final: 3",", since the shadow accumulates across iterations",[111,1395,1397,847,1399,1401,1402,1404],{"className":1396},[114],[116,1398],{"disabled":118,"type":119},[32,1400,1373],{}," conflicts between ",[32,1403,177],{}," and shadowed declarations",[111,1406,1408,976,1410,614,1412,614,1414,821,1416],{"className":1407},[114],[116,1409],{"disabled":118,"type":119},[32,1411,1362],{},[32,1413,1055],{},[32,1415,1109],{},[32,1417,1392],{},[149,1419,1420,1422,1439],{},[152,1421,154],{},[156,1423,1424,1426,1427,614,1429,614,1431,821,1433,1370,1435,1374,1437,1378],{},[159,1425,161],{}," A — Prints ",[32,1428,1362],{},[32,1430,1055],{},[32,1432,1109],{},[32,1434,1369],{},[32,1436,1373],{},[32,1438,1377],{},[156,1440,1441,1443,1444,1447,1448,562,1451,1453,1454,1456,1457,1460,1461,1464,1465,1467,1468,1470,1471,1473,1474,1477],{},[159,1442,169],{}," Each loop iteration re-declares a fresh shadow ",[32,1445,1446],{},"let count = count + i",", reading the ",[426,1449,1450],{},"outer",[32,1452,1373],{}," (always ",[32,1455,1362],{},", since it's never reassigned with ",[32,1458,1459],{},"count = ..."," anywhere) plus the current ",[32,1462,1463],{},"i",", then discarding that shadow at the end of the iteration's block scope. Despite being marked ",[32,1466,177],{},", the outer ",[32,1469,1373],{}," is never actually mutated — only shadowed — which is a common gotcha: declaring ",[32,1472,177],{}," doesn't mean a variable ",[426,1475,1476],{},"is"," being mutated, and shadowing inside a loop body does not accumulate like a running total would (contradicting the tempting B).",[14,1479,1480,1484,1568,1617],{"language":16},[18,1481,1483],{"id":1482},"q11-what-is-wrong-if-anything-with-this-code","Q11. What is wrong, if anything, with this code?",[23,1485,1486],{"language":16},[26,1487,1489],{"className":28,"code":1488,"language":16,"meta":30,"style":30},"const BUFFER_SIZE: usize = compute_size();\n\nfn compute_size() -> usize {\n    42\n}\n\nfn main() {\n    println!(\"{BUFFER_SIZE}\");\n}\n",[32,1490,1491,1510,1515,1531,1536,1540,1544,1552,1563],{"__ignoreMap":30},[35,1492,1493,1495,1498,1500,1503,1505,1508],{"class":37,"line":38},[35,1494,249],{"class":41},[35,1496,1497],{"class":65}," BUFFER_SIZE",[35,1499,542],{"class":41},[35,1501,1502],{"class":45}," usize",[35,1504,548],{"class":41},[35,1506,1507],{"class":45}," compute_size",[35,1509,794],{"class":49},[35,1511,1512],{"class":37,"line":53},[35,1513,1514],{"emptyLinePlaceholder":118},"\n",[35,1516,1517,1519,1521,1524,1527,1529],{"class":37,"line":72},[35,1518,42],{"class":41},[35,1520,1507],{"class":45},[35,1522,1523],{"class":49},"() ",[35,1525,1526],{"class":41},"->",[35,1528,1502],{"class":45},[35,1530,1306],{"class":49},[35,1532,1533],{"class":37,"line":85},[35,1534,1535],{"class":65},"    42\n",[35,1537,1538],{"class":37,"line":101},[35,1539,104],{"class":49},[35,1541,1542],{"class":37,"line":339},[35,1543,1514],{"emptyLinePlaceholder":118},[35,1545,1546,1548,1550],{"class":37,"line":953},[35,1547,42],{"class":41},[35,1549,46],{"class":45},[35,1551,50],{"class":49},[35,1553,1554,1556,1558,1561],{"class":37,"line":965},[35,1555,88],{"class":45},[35,1557,91],{"class":49},[35,1559,1560],{"class":94},"\"{BUFFER_SIZE}\"",[35,1562,98],{"class":49},[35,1564,1566],{"class":37,"line":1565},9,[35,1567,104],{"class":49},[106,1569,1571,1585,1597,1606],{"className":1570},[109],[111,1572,1574,1576,1577,1580,1581,1584],{"className":1573},[114],[116,1575],{"disabled":118,"type":119}," Nothing — this compiles and prints ",[32,1578,1579],{},"42",", since ",[32,1582,1583],{},"compute_size"," is a simple const-evaluable function",[111,1586,1588,847,1590,1592,1593,1596],{"className":1587},[114],[116,1589],{"disabled":118,"type":119},[32,1591,1583],{}," is not marked ",[32,1594,1595],{},"const fn",", so it cannot be called in a const-evaluation context",[111,1598,1600,1602,1603,1605],{"className":1599},[114],[116,1601],{"disabled":118,"type":119}," Runtime panic because ",[32,1604,249],{}," initializers cannot call functions",[111,1607,1609,847,1611,1613,1614],{"className":1608},[114],[116,1610],{"disabled":118,"type":119},[32,1612,249],{}," items cannot appear before ",[32,1615,1616],{},"fn main",[149,1618,1619,1621,1630],{},[152,1620,154],{},[156,1622,1623,1625,1626,1592,1628,1596],{},[159,1624,161],{}," B — Compile error: ",[32,1627,1583],{},[32,1629,1595],{},[156,1631,1632,502,1634,1636,1637,1639,1640,1643,1644,1646],{},[159,1633,169],{},[32,1635,249],{}," initializer must be evaluable entirely at compile time; calling an ordinary ",[32,1638,42],{}," is not permitted because the compiler cannot generally guarantee it has no runtime-only behavior. The fix is ",[32,1641,1642],{},"const fn compute_size() -> usize { 42 }",", which explicitly opts the function into compile-time (and still-usable-at-runtime) evaluation. Declaration order (D) is a red herring — Rust items are not order-dependent within a module, unlike ",[32,1645,413],{}," statements.",[14,1648,1649,1657,1728,1770],{"language":16},[18,1650,1652,1653,1656],{"id":1651},"q12-given-this-snippet-what-does-total-end-up-being-and-why","Q12. Given this snippet, what does ",[32,1654,1655],{},"total"," end up being, and why?",[23,1658,1659],{"language":16},[26,1660,1662],{"className":28,"code":1661,"language":16,"meta":30,"style":30},"fn main() {\n    let total = 10;\n    let total = total;\n    let total = total + 5;\n    println!(\"{total}\");\n}\n",[32,1663,1664,1672,1686,1697,1713,1724],{"__ignoreMap":30},[35,1665,1666,1668,1670],{"class":37,"line":38},[35,1667,42],{"class":41},[35,1669,46],{"class":45},[35,1671,50],{"class":49},[35,1673,1674,1676,1679,1681,1684],{"class":37,"line":53},[35,1675,56],{"class":41},[35,1677,1678],{"class":49}," total ",[35,1680,62],{"class":41},[35,1682,1683],{"class":65}," 10",[35,1685,69],{"class":49},[35,1687,1688,1690,1692,1694],{"class":37,"line":72},[35,1689,56],{"class":41},[35,1691,1678],{"class":49},[35,1693,62],{"class":41},[35,1695,1696],{"class":49}," total;\n",[35,1698,1699,1701,1703,1705,1707,1709,1711],{"class":37,"line":85},[35,1700,56],{"class":41},[35,1702,1678],{"class":49},[35,1704,62],{"class":41},[35,1706,1678],{"class":49},[35,1708,303],{"class":41},[35,1710,66],{"class":65},[35,1712,69],{"class":49},[35,1714,1715,1717,1719,1722],{"class":37,"line":101},[35,1716,88],{"class":45},[35,1718,91],{"class":49},[35,1720,1721],{"class":94},"\"{total}\"",[35,1723,98],{"class":49},[35,1725,1726],{"class":37,"line":339},[35,1727,104],{"class":49},[106,1729,1731,1741,1753,1762],{"className":1730},[109],[111,1732,1734,1736,1737,1740],{"className":1733},[114],[116,1735],{"disabled":118,"type":119}," Compile error: cannot shadow a variable with itself (",[32,1738,1739],{},"let total = total;",")",[111,1742,1744,562,1746,1749,1750,1752],{"className":1743},[114],[116,1745],{"disabled":118,"type":119},[32,1747,1748],{},"15"," — the second ",[32,1751,1739],{}," is a valid (if redundant) shadow that simply moves\u002Fcopies the value into a new binding, and shadowing continues to work normally afterward",[111,1754,1756,562,1758,1761],{"className":1755},[114],[116,1757],{"disabled":118,"type":119},[32,1759,1760],{},"10",", because the final shadow is ignored",[111,1763,1765,847,1767,1769],{"className":1764},[114],[116,1766],{"disabled":118,"type":119},[32,1768,1655],{}," used in its own initializer is a circular definition",[149,1771,1772,1774,1783],{},[152,1773,154],{},[156,1775,1776,860,1778,1749,1780,1782],{},[159,1777,161],{},[32,1779,1748],{},[32,1781,1739],{}," is a valid (if redundant) shadow that simply moves\u002Fcopies the value into a new binding",[156,1784,1785,1787,1788,1791,1792,1794,1795,1798,1799,1801,1802,788],{},[159,1786,169],{}," Each ",[32,1789,1790],{},"let total = \u003Cexpr>"," evaluates its right-hand side using whatever ",[32,1793,1655],{}," was previously in scope ",[426,1796,1797],{},"before"," introducing the new binding — this is not circular (ruling out D) because the old and new ",[32,1800,1655],{}," are different variables, not the same one referring to itself. This pattern (shadowing a variable with an expression referencing itself) is completely ordinary and compiles without any special-casing, ultimately yielding ",[32,1803,1804],{},"10 + 5 = 15",[14,1806,1807,1814,1892,1943],{"language":16},[18,1808,1810,1811,1813],{"id":1809},"q13-what-happens-with-this-pattern-matched-let-binding","Q13. What happens with this pattern-matched ",[32,1812,413],{}," binding?",[23,1815,1816],{"language":16},[26,1817,1819],{"className":28,"code":1818,"language":16,"meta":30,"style":30},"fn main() {\n    let (a, mut b) = (1, 2);\n    a = 10;\n    b = 20;\n    println!(\"{a} {b}\");\n}\n",[32,1820,1821,1829,1854,1865,1877,1888],{"__ignoreMap":30},[35,1822,1823,1825,1827],{"class":37,"line":38},[35,1824,42],{"class":41},[35,1826,46],{"class":45},[35,1828,50],{"class":49},[35,1830,1831,1833,1836,1838,1841,1843,1846,1848,1850,1852],{"class":37,"line":53},[35,1832,56],{"class":41},[35,1834,1835],{"class":49}," (a, ",[35,1837,177],{"class":41},[35,1839,1840],{"class":49}," b) ",[35,1842,62],{"class":41},[35,1844,1845],{"class":49}," (",[35,1847,1055],{"class":65},[35,1849,614],{"class":49},[35,1851,1109],{"class":65},[35,1853,98],{"class":49},[35,1855,1856,1859,1861,1863],{"class":37,"line":72},[35,1857,1858],{"class":49},"    a ",[35,1860,62],{"class":41},[35,1862,1683],{"class":65},[35,1864,69],{"class":49},[35,1866,1867,1870,1872,1875],{"class":37,"line":85},[35,1868,1869],{"class":49},"    b ",[35,1871,62],{"class":41},[35,1873,1874],{"class":65}," 20",[35,1876,69],{"class":49},[35,1878,1879,1881,1883,1886],{"class":37,"line":101},[35,1880,88],{"class":45},[35,1882,91],{"class":49},[35,1884,1885],{"class":94},"\"{a} {b}\"",[35,1887,98],{"class":49},[35,1889,1890],{"class":37,"line":339},[35,1891,104],{"class":49},[106,1893,1895,1904,1925,1937],{"className":1894},[109],[111,1896,1898,1900,1901,1903],{"className":1897},[114],[116,1899],{"disabled":118,"type":119}," Both reassignments compile fine, since ",[32,1902,177],{}," applies to the whole tuple pattern",[111,1905,1907,1909,1910,1913,1914,1917,1918,1920,1921,1924],{"className":1906},[114],[116,1908],{"disabled":118,"type":119}," Compile error on ",[32,1911,1912],{},"a = 10;"," — mutability in a destructuring pattern is per-binding; ",[32,1915,1916],{},"a"," was not marked ",[32,1919,177],{},", so only ",[32,1922,1923],{},"b"," is reassignable",[111,1926,1928,1909,1930,1933,1934,1936],{"className":1927},[114],[116,1929],{"disabled":118,"type":119},[32,1931,1932],{},"b = 20;"," — ",[32,1935,177],{}," inside a tuple pattern is not valid syntax",[111,1938,1940,1942],{"className":1939},[114],[116,1941],{"disabled":118,"type":119}," Compile error on both lines, since tuple destructuring never allows subsequent reassignment",[149,1944,1945,1947,1960],{},[152,1946,154],{},[156,1948,1949,1951,1952,1913,1954,1917,1956,1920,1958,1924],{},[159,1950,161],{}," B — Compile error on ",[32,1953,1912],{},[32,1955,1916],{},[32,1957,177],{},[32,1959,1923],{},[156,1961,1962,1964,1965,614,1968,1970,1971,1973,1974,1976,1977,614,1979,1981,1982,1984,1985,1987],{},[159,1963,169],{}," In pattern destructuring like ",[32,1966,1967],{},"let (a, mut b) = ...",[32,1969,177],{}," attaches to the individual identifier it precedes, not to the tuple as a whole — a common gotcha for developers assuming ",[32,1972,177],{}," on one part of a pattern applies globally. Since ",[32,1975,1916],{}," lacks ",[32,1978,177],{},[32,1980,1912],{}," triggers ",[32,1983,181],{},", while ",[32,1986,1932],{}," is perfectly valid.",[14,1989,1990,1994,2088,2137],{"language":16},[18,1991,1993],{"id":1992},"q14-what-is-the-behavior-of-this-code-involving-a-shadowed-reference","Q14. What is the behavior of this code involving a shadowed reference?",[23,1995,1996],{"language":16},[26,1997,1999],{"className":28,"code":1998,"language":16,"meta":30,"style":30},"fn main() {\n    let guess = \"42\";\n    let guess: i32 = guess.trim().parse().expect(\"not a number\");\n    println!(\"{}\", guess + 1);\n}\n",[32,2000,2001,2009,2023,2066,2084],{"__ignoreMap":30},[35,2002,2003,2005,2007],{"class":37,"line":38},[35,2004,42],{"class":41},[35,2006,46],{"class":45},[35,2008,50],{"class":49},[35,2010,2011,2013,2016,2018,2021],{"class":37,"line":53},[35,2012,56],{"class":41},[35,2014,2015],{"class":49}," guess ",[35,2017,62],{"class":41},[35,2019,2020],{"class":94}," \"42\"",[35,2022,69],{"class":49},[35,2024,2025,2027,2030,2032,2035,2037,2039,2041,2044,2047,2049,2052,2054,2056,2059,2061,2064],{"class":37,"line":72},[35,2026,56],{"class":41},[35,2028,2029],{"class":49}," guess",[35,2031,542],{"class":41},[35,2033,2034],{"class":45}," i32",[35,2036,548],{"class":41},[35,2038,2029],{"class":49},[35,2040,788],{"class":41},[35,2042,2043],{"class":45},"trim",[35,2045,2046],{"class":49},"()",[35,2048,788],{"class":41},[35,2050,2051],{"class":45},"parse",[35,2053,2046],{"class":49},[35,2055,788],{"class":41},[35,2057,2058],{"class":45},"expect",[35,2060,91],{"class":49},[35,2062,2063],{"class":94},"\"not a number\"",[35,2065,98],{"class":49},[35,2067,2068,2070,2072,2075,2078,2080,2082],{"class":37,"line":85},[35,2069,88],{"class":45},[35,2071,91],{"class":49},[35,2073,2074],{"class":94},"\"{}\"",[35,2076,2077],{"class":49},", guess ",[35,2079,303],{"class":41},[35,2081,306],{"class":65},[35,2083,98],{"class":49},[35,2085,2086],{"class":37,"line":101},[35,2087,104],{"class":49},[106,2089,2091,2106,2116,2125],{"className":2090},[109],[111,2092,2094,847,2096,2099,2100,2102,2103],{"className":2093},[114],[116,2095],{"disabled":118,"type":119},[32,2097,2098],{},"guess"," cannot be both a ",[32,2101,466],{}," and an ",[32,2104,2105],{},"i32",[111,2107,2109,2111,2112,2115],{"className":2108},[114],[116,2110],{"disabled":118,"type":119}," Compiles and prints ",[32,2113,2114],{},"43"," — shadowing lets the string be parsed into a numeric type under the same name, a common idiom for input validation\u002Fconversion",[111,2117,2119,1180,2121,2124],{"className":2118},[114],[116,2120],{"disabled":118,"type":119},[32,2122,2123],{},"\"42\""," is not trimmed first",[111,2126,2128,847,2130,2133,2134,2136],{"className":2127},[114],[116,2129],{"disabled":118,"type":119},[32,2131,2132],{},".expect()"," cannot be called inside a ",[32,2135,413],{}," initializer",[149,2138,2139,2141,2148],{},[152,2140,154],{},[156,2142,2143,2145,2146,2115],{},[159,2144,161],{}," B — Compiles and prints ",[32,2147,2114],{},[156,2149,2150,2152,2153,2155,2156,630,2159,2162,2163,2166,2167,2170,2171,2173,2174,2176,2177,2179,2180,788],{},[159,2151,169],{}," This is the textbook idiomatic use of shadowing shown in the official Rust book: reusing the name ",[32,2154,2098],{}," avoids inventing a separate name like ",[32,2157,2158],{},"guess_str",[32,2160,2161],{},"guess_num"," for what is conceptually \"the same value, converted.\" ",[32,2164,2165],{},".trim()"," removes whitespace before ",[32,2168,2169],{},".parse::\u003Ci32>()",", which succeeds here since ",[32,2172,2123],{}," parses cleanly, so ",[32,2175,2132],{}," never triggers its panic path, and the value is a genuine ",[32,2178,2105],{}," addition yielding ",[32,2181,2114],{},[14,2183,2184,2191,2229],{},[18,2185,2187,2188,2190],{"id":2186},"q15-when-choosing-between-mut-and-shadowing-to-transform-a-value-while-keeping-semantic-continuity-eg-trimming-and-parsing-user-input-what-is-the-idiomatic-best-practice","Q15. When choosing between ",[32,2189,177],{}," and shadowing to transform a value while keeping semantic continuity (e.g., trimming and parsing user input), what is the idiomatic best practice?",[106,2192,2194,2203,2214,2220],{"className":2193},[109],[111,2195,2197,2199,2200,2202],{"className":2196},[114],[116,2198],{"disabled":118,"type":119}," Always prefer ",[32,2201,177],{}," in every case, since shadowing is considered a deprecated pattern",[111,2204,2206,2208,2209,2211,2212,1740],{"className":2205},[114],[116,2207],{"disabled":118,"type":119}," Prefer shadowing when the transformation also changes the type or represents a distinct logical value (e.g., raw string to parsed number), and reserve ",[32,2210,177],{}," for genuine in-place accumulation\u002Fmutation of the same logical value (e.g., a running counter or a growing ",[32,2213,1071],{},[111,2215,2217,2219],{"className":2216},[114],[116,2218],{"disabled":118,"type":119}," It never matters; both are functionally and stylistically interchangeable in all cases",[111,2221,2223,2225,2226,2228],{"className":2222},[114],[116,2224],{"disabled":118,"type":119}," Always prefer shadowing over ",[32,2227,177],{},", even for loop counters and accumulators",[149,2230,2231,2233,2241],{},[152,2232,154],{},[156,2234,2235,2237,2238,2240],{},[159,2236,161],{}," B — Prefer shadowing when the transformation also changes the type or represents a distinct logical value, and reserve ",[32,2239,177],{}," for genuine in-place accumulation\u002Fmutation",[156,2242,2243,562,2245,2248,2249,2251,2252,2254],{},[159,2244,169],{},[159,2246,2247],{},"Idiom:"," shadowing communicates \"this is conceptually a new, immutable value derived from the old one\" (great for type conversions or applying a series of independent transformations), while ",[32,2250,177],{}," communicates \"this variable's value genuinely changes over time\" (ideal for counters, accumulators, or mutable collections). Defaulting to ",[32,2253,177],{}," everywhere (A) loses the compiler's help in catching accidental unintended mutation, since immutable-by-default is one of Rust's core safety features, not a style preference to override universally.",[14,2256,2257,2267,2298],{},[18,2258,2260,2261,2263,2264,525],{"id":2259},"q16-what-is-the-idiomatic-best-practice-reason-to-prefer-const-over-a-magic-number-literal-scattered-throughout-code-such-as-const-max_connections-u32-100","Q16. What is the idiomatic best-practice reason to prefer ",[32,2262,249],{}," over a \"magic number\" literal scattered throughout code, such as ",[32,2265,2266],{},"const MAX_CONNECTIONS: u32 = 100;",[106,2268,2270,2278,2284,2292],{"className":2269},[109],[111,2271,2273,562,2275,2277],{"className":2272},[114],[116,2274],{"disabled":118,"type":119},[32,2276,249],{}," values are faster at runtime than literals because they're cached in a register",[111,2279,2281,2283],{"className":2280},[114],[116,2282],{"disabled":118,"type":119}," It gives the value a descriptive name (improving readability\u002Fintent), centralizes the value for easy updates, and lets the compiler enforce its type — literal magic numbers offer none of these and are error-prone to keep in sync across a codebase",[111,2285,2287,562,2289,2291],{"className":2286},[114],[116,2288],{"disabled":118,"type":119},[32,2290,249],{}," is required by the compiler for any numeric literal used more than once",[111,2293,2295,2297],{"className":2294},[114],[116,2296],{"disabled":118,"type":119}," There is no practical benefit; it's purely a stylistic preference with zero technical merit",[149,2299,2300,2302,2307],{},[152,2301,154],{},[156,2303,2304,2306],{},[159,2305,161],{}," B — It gives the value a descriptive name, centralizes the value for easy updates, and lets the compiler enforce its type — literal magic numbers offer none of these and are error-prone to keep in sync",[156,2308,2309,562,2311,2313,2314,2316,2317,2320,2321,2324],{},[159,2310,169],{},[159,2312,2247],{}," since ",[32,2315,249],{}," values are inlined by the compiler, there's no meaningful runtime performance difference versus a literal (A is a myth) — the real benefit is entirely about maintainability and self-documenting code, e.g. changing ",[32,2318,2319],{},"MAX_CONNECTIONS"," in one place instead of hunting down every ",[32,2322,2323],{},"100"," in the codebase, some of which might mean something else entirely.",[14,2326,2327,2336,2363],{},[18,2328,2330,2331,821,2333,2335],{"id":2329},"q17-which-naming-convention-does-idiomatic-rust-use-for-const-and-static-items-as-enforced-by-default-clippyrustc-style-lints","Q17. Which naming convention does idiomatic Rust use for ",[32,2332,249],{},[32,2334,643],{}," items, as enforced by default Clippy\u002Frustc style lints?",[106,2337,2339,2345,2351,2357],{"className":2338},[109],[111,2340,2342,2344],{"className":2341},[114],[116,2343],{"disabled":118,"type":119}," camelCase, matching function and variable names",[111,2346,2348,2350],{"className":2347},[114],[116,2349],{"disabled":118,"type":119}," SCREAMING_SNAKE_CASE",[111,2352,2354,2356],{"className":2353},[114],[116,2355],{"disabled":118,"type":119}," PascalCase, matching type names",[111,2358,2360,2362],{"className":2359},[114],[116,2361],{"disabled":118,"type":119}," snake_case, identical to regular variables",[149,2364,2365,2367,2372],{},[152,2366,154],{},[156,2368,2369,2371],{},[159,2370,161],{}," B — SCREAMING_SNAKE_CASE",[156,2373,2374,562,2376,2378,2379,2382,2383,630,2385,2387,2388,2391,2392,2395,2396,2399,2400,2403,2404,2406,2407,2409],{},[159,2375,169],{},[159,2377,2247],{}," Rust's naming conventions (enforced by the ",[32,2380,2381],{},"non_upper_case_globals"," lint) require ",[32,2384,249],{},[32,2386,643],{}," identifiers in ",[32,2389,2390],{},"SCREAMING_SNAKE_CASE",", e.g. ",[32,2393,2394],{},"MAX_RETRIES",", visually distinguishing compile-time constants from ordinary ",[32,2397,2398],{},"snake_case"," variables and ",[32,2401,2402],{},"PascalCase"," types — using ",[32,2405,2398],{}," for a ",[32,2408,249],{}," (D) will trigger a compiler warning by default, not silently pass unnoticed.",[14,2411,2412,2420,2475,2517],{"language":16},[18,2413,2415,2416,2419],{"id":2414},"q18-a-function-parameter-needs-to-be-modified-locally-within-the-function-body-without-affecting-the-callers-original-value-for-copy-types-what-is-the-idiomatic-approach","Q18. A function parameter needs to be modified locally within the function body without affecting the caller's original value (for ",[32,2417,2418],{},"Copy"," types). What is the idiomatic approach?",[23,2421,2422],{"language":16},[26,2423,2425],{"className":28,"code":2424,"language":16,"meta":30,"style":30},"fn double(mut n: i32) -> i32 {\n    n *= 2;\n    n\n}\n",[32,2426,2427,2454,2466,2471],{"__ignoreMap":30},[35,2428,2429,2431,2434,2436,2438,2441,2443,2445,2448,2450,2452],{"class":37,"line":38},[35,2430,42],{"class":41},[35,2432,2433],{"class":45}," double",[35,2435,91],{"class":49},[35,2437,177],{"class":41},[35,2439,2440],{"class":49}," n",[35,2442,542],{"class":41},[35,2444,2034],{"class":45},[35,2446,2447],{"class":49},") ",[35,2449,1526],{"class":41},[35,2451,2034],{"class":45},[35,2453,1306],{"class":49},[35,2455,2456,2459,2462,2464],{"class":37,"line":53},[35,2457,2458],{"class":49},"    n ",[35,2460,2461],{"class":41},"*=",[35,2463,324],{"class":65},[35,2465,69],{"class":49},[35,2467,2468],{"class":37,"line":72},[35,2469,2470],{"class":49},"    n\n",[35,2472,2473],{"class":37,"line":85},[35,2474,104],{"class":49},[106,2476,2478,2487,2503,2509],{"className":2477},[109],[111,2479,2481,2483,2484,2486],{"className":2480},[114],[116,2482],{"disabled":118,"type":119}," This is invalid; parameters can never be declared ",[32,2485,177],{}," in a function signature",[111,2488,2490,2492,2493,2496,2497,2499,2500,2502],{"className":2489},[114],[116,2491],{"disabled":118,"type":119}," This is valid and idiomatic — ",[32,2494,2495],{},"mut n"," makes the local parameter binding mutable within the function body, and since ",[32,2498,2105],{}," is ",[32,2501,2418],{},", the caller's original value is entirely unaffected",[111,2504,2506,2508],{"className":2505},[114],[116,2507],{"disabled":118,"type":119}," This mutates the caller's original variable as a side effect, since all integers are passed by reference",[111,2510,2512,562,2514,2516],{"className":2511},[114],[116,2513],{"disabled":118,"type":119},[32,2515,177],{}," here is redundant and should be removed, since function parameters are always mutable by default",[149,2518,2519,2521,2532],{},[152,2520,154],{},[156,2522,2523,2525,2526,2496,2528,2499,2530,2502],{},[159,2524,161],{}," B — This is valid and idiomatic — ",[32,2527,2495],{},[32,2529,2105],{},[32,2531,2418],{},[156,2533,2534,2536,2537,2539,2540,2542,2543,614,2545,2548,2549,2552,2553,2555,2556,2558],{},[159,2535,169],{}," Marking a by-value parameter ",[32,2538,177],{}," is a common, idiomatic pattern when a function wants to use the parameter as a local scratch variable; because ",[32,2541,2105],{}," implements ",[32,2544,2418],{},[32,2546,2547],{},"n"," is a completely independent copy of whatever the caller passed, so mutating it inside ",[32,2550,2551],{},"double"," has zero effect on the caller's variable (ruling out C, a mistaken assumption from languages that pass primitives by reference). Function parameters are immutable by default just like ",[32,2554,413],{}," bindings (contradicting D) — ",[32,2557,177],{}," must be explicitly opted into here too.",[14,2560,2561,2568,2607],{},[18,2562,2564,2565,2567],{"id":2563},"q19-best-practice-a-function-receives-configuration-flags-that-should-never-change-after-being-read-at-startup-and-are-used-throughout-the-entire-programs-lifetime-across-multiple-modules-what-is-more-idiomatic-a-static-or-passing-values-explicitly-through-function-parametersstructs","Q19. Best practice: a function receives configuration flags that should never change after being read at startup and are used throughout the entire program's lifetime across multiple modules. What is more idiomatic — a ",[32,2566,643],{}," or passing values explicitly through function parameters\u002Fstructs?",[106,2569,2571,2580,2592,2601],{"className":2570},[109],[111,2572,2574,2576,2577,2579],{"className":2573},[114],[116,2575],{"disabled":118,"type":119}," Always use ",[32,2578,738],{}," globals for any cross-module shared configuration, since it's the simplest to set up",[111,2581,2583,2585,2586,2588,2589,2591],{"className":2582},[114],[116,2584],{"disabled":118,"type":119}," Prefer passing configuration explicitly (e.g., via a struct passed by reference or dependency injection) over global ",[32,2587,643],{}," state; reserve ",[32,2590,643],{},"\u002Fglobal state for cases with no reasonable alternative, since globals make code harder to test and reason about",[111,2593,2595,2597,2598,2600],{"className":2594},[114],[116,2596],{"disabled":118,"type":119}," Use ",[32,2599,249],{}," for configuration that is only known at runtime (e.g., read from a file or environment variable)",[111,2602,2604,2606],{"className":2603},[114],[116,2605],{"disabled":118,"type":119}," There is no idiomatic distinction; global mutable state is equally recommended in Rust as in any other language",[149,2608,2609,2611,2621],{},[152,2610,154],{},[156,2612,2613,2615,2616,2588,2618,2620],{},[159,2614,161],{}," B — Prefer passing configuration explicitly over global ",[32,2617,643],{},[32,2619,643],{},"\u002Fglobal state for cases with no reasonable alternative",[156,2622,2623,562,2625,2627,2628,2630,2631,2633,2634,2636],{},[159,2624,169],{},[159,2626,2247],{}," explicit parameter\u002Fstruct passing keeps dependencies visible and testable (you can construct different configs in different tests without global interference), whereas global mutable state (especially ",[32,2629,738],{},", which requires ",[32,2632,676],{}," and is a well-known source of data races and hard-to-trace bugs) is generally discouraged. ",[32,2635,249],{}," (C) cannot hold runtime-only values like file\u002Fenv contents at all — its initializer must be evaluable at compile time, ruling that option out entirely, not just as bad practice.",[14,2638,2639,2646,2697,2754],{"language":16},[18,2640,2642,2643,2645],{"id":2641},"q20-a-code-reviewer-flags-this-function-for-using-mut-where-it-isnt-needed","Q20. A code reviewer flags this function for using ",[32,2644,177],{}," where it isn't needed:",[23,2647,2648],{"language":16},[26,2649,2651],{"className":28,"code":2650,"language":16,"meta":30,"style":30},"fn describe(mut name: String) -> String {\n    format!(\"Hello, {name}\")\n}\n",[32,2652,2653,2680,2693],{"__ignoreMap":30},[35,2654,2655,2657,2660,2662,2664,2667,2669,2672,2674,2676,2678],{"class":37,"line":38},[35,2656,42],{"class":41},[35,2658,2659],{"class":45}," describe",[35,2661,91],{"class":49},[35,2663,177],{"class":41},[35,2665,2666],{"class":49}," name",[35,2668,542],{"class":41},[35,2670,2671],{"class":45}," String",[35,2673,2447],{"class":49},[35,2675,1526],{"class":41},[35,2677,2671],{"class":45},[35,2679,1306],{"class":49},[35,2681,2682,2685,2687,2690],{"class":37,"line":53},[35,2683,2684],{"class":45},"    format!",[35,2686,91],{"class":49},[35,2688,2689],{"class":94},"\"Hello, {name}\"",[35,2691,2692],{"class":49},")\n",[35,2694,2695],{"class":37,"line":72},[35,2696,104],{"class":49},[106,2698,2700,2720,2733,2746],{"className":2699},[109],[111,2701,2703,2705,2706,2708,2709,2712,2713,2716,2717,2719],{"className":2702},[114],[116,2704],{"disabled":118,"type":119}," The reviewer is right to flag it — ",[32,2707,177],{}," is unused here since ",[32,2710,2711],{},"name"," is never reassigned or mutated in the body, and the compiler would emit an ",[32,2714,2715],{},"unused_mut"," warning; removing ",[32,2718,177],{}," is the idiomatic fix",[111,2721,2723,2725,2726,2728,2729,2732],{"className":2722},[114],[116,2724],{"disabled":118,"type":119}," The reviewer is wrong — ",[32,2727,177],{}," is required whenever a ",[32,2730,2731],{},"String"," is taken by value",[111,2734,2736,2738,2739,2741,2742,2745],{"className":2735},[114],[116,2737],{"disabled":118,"type":119}," The reviewer is wrong — removing ",[32,2740,177],{}," would cause a compile error since ",[32,2743,2744],{},"format!"," internally mutates its arguments",[111,2747,2749,562,2751,2753],{"className":2748},[114],[116,2750],{"disabled":118,"type":119},[32,2752,177],{}," has no effect either way here and the compiler never warns about it",[149,2755,2756,2758,2766],{},[152,2757,154],{},[156,2759,2760,2762,2763,2765],{},[159,2761,161],{}," A — The reviewer is right to flag it; ",[32,2764,177],{}," is unused, and removing it is the idiomatic fix",[156,2767,2768,562,2770,562,2772,2774,2775,2778,2779,2781,2782,2784,2785,2787,2788,2790,2791,2793],{},[159,2769,169],{},[159,2771,2247],{},[32,2773,177],{}," should only be applied when a binding is actually reassigned or mutated through a ",[32,2776,2777],{},"&mut"," borrow; here ",[32,2780,2711],{}," is only read via ",[32,2783,2744],{},", so ",[32,2786,177],{}," is dead weight that the compiler flags with an ",[32,2789,2715],{}," warning by default (contradicting D). Leaving unnecessary ",[32,2792,177],{}," around is a minor but real code-smell — it misleads readers into thinking the parameter is modified, and idiomatic Rust code keeps mutability annotations minimal and accurate to signal actual intent.",[2795,2796,2797],"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 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}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);}",{"title":30,"searchDepth":53,"depth":53,"links":2799},[2800,2801,2802,2803,2805,2807,2809,2810,2811,2813,2814,2815,2817,2819,2820,2822,2824,2826,2828,2830],{"id":20,"depth":72,"text":21},{"id":187,"depth":72,"text":188},{"id":262,"depth":72,"text":263},{"id":437,"depth":72,"text":2804},"Q4. Which of the following is only possible with shadowing (let), not with plain mutation (mut)?",{"id":521,"depth":72,"text":2806},"Q5. What is the correct syntax and semantics of a const?",{"id":639,"depth":72,"text":2808},"Q6. How does static differ from const?",{"id":744,"depth":72,"text":745},{"id":882,"depth":72,"text":883},{"id":1067,"depth":72,"text":2812},"Q9. What happens when an immutable variable holding a Vec is used like this?",{"id":1252,"depth":72,"text":1253},{"id":1482,"depth":72,"text":1483},{"id":1651,"depth":72,"text":2816},"Q12. Given this snippet, what does total end up being, and why?",{"id":1809,"depth":72,"text":2818},"Q13. What happens with this pattern-matched let binding?",{"id":1992,"depth":72,"text":1993},{"id":2186,"depth":72,"text":2821},"Q15. When choosing between mut and shadowing to transform a value while keeping semantic continuity (e.g., trimming and parsing user input), what is the idiomatic best practice?",{"id":2259,"depth":72,"text":2823},"Q16. What is the idiomatic best-practice reason to prefer const over a \"magic number\" literal scattered throughout code, such as const MAX_CONNECTIONS: u32 = 100;?",{"id":2329,"depth":72,"text":2825},"Q17. Which naming convention does idiomatic Rust use for const and static items, as enforced by default Clippy\u002Frustc style lints?",{"id":2414,"depth":72,"text":2827},"Q18. A function parameter needs to be modified locally within the function body without affecting the caller's original value (for Copy types). What is the idiomatic approach?",{"id":2563,"depth":72,"text":2829},"Q19. Best practice: a function receives configuration flags that should never change after being read at startup and are used throughout the entire program's lifetime across multiple modules. What is more idiomatic — a static or passing values explicitly through function parameters\u002Fstructs?",{"id":2641,"depth":72,"text":2831},"Q20. A code reviewer flags this function for using mut where it isn't needed:","md",{},"\u002Frust\u002F03-variables-and-mutability",{"title":5,"description":30},"rust\u002F03-variables-and-mutability","mNX6jqUlMntoHcCUJXv5LhB1Lhv_BzoZ63VXdn5YoHg",1787335398352]