[{"data":1,"prerenderedAt":1652},["ShallowReactive",2],{"page-\u002Frust\u002F10-lifetimes":3},{"id":4,"title":5,"body":6,"description":1645,"extension":1646,"meta":1647,"navigation":505,"path":1648,"seo":1649,"stem":1650,"__hash__":1651},"content\u002Frust\u002F10-lifetimes.md","10 — Lifetimes",{"type":7,"value":8,"toc":1623},"minimark",[9,13,22,27,35,130,134,141,243,271,275,278,316,319,381,387,393,416,448,460,464,467,593,600,604,709,715,719,786,804,808,828,857,866,870,936,948,955,1004,1013,1017,1020,1082,1086,1178,1182,1295,1307,1314,1389,1397,1401,1445,1449,1526,1530,1595,1599,1602,1616,1619],[10,11,5],"h1",{"id":12},"_10-lifetimes",[14,15,16,17,21],"p",{},"Lifetimes are the borrow checker's way of tracking ",[18,19,20],"strong",{},"how long a reference is valid",". They're a compile-time concept; there's zero runtime cost.",[23,24,26],"h2",{"id":25},"the-core-idea","The Core Idea",[14,28,29,30,34],{},"A reference's lifetime is the region of code where it's valid to use. The compiler ",[31,32,33],"em",{},"rejects"," code where a reference could outlive the data it points to:",[36,37,39],"code-wrapper",{"language":38},"rust",[40,41,45],"pre",{"className":42,"code":43,"language":38,"meta":44,"style":44},"language-rust shiki shiki-themes github-light github-dark","let r;\n{\n    let x = 5;\n    r = &x;\n}                       \u002F\u002F x dropped here\nprintln!(\"{r}\");        \u002F\u002F ERROR: x does not live long enough\n","",[46,47,48,61,67,86,100,110],"code",{"__ignoreMap":44},[49,50,53,57],"span",{"class":51,"line":52},"line",1,[49,54,56],{"class":55},"svdQ7","let",[49,58,60],{"class":59},"ssxIu"," r;\n",[49,62,64],{"class":51,"line":63},2,[49,65,66],{"class":59},"{\n",[49,68,70,73,76,79,83],{"class":51,"line":69},3,[49,71,72],{"class":55},"    let",[49,74,75],{"class":59}," x ",[49,77,78],{"class":55},"=",[49,80,82],{"class":81},"snvgF"," 5",[49,84,85],{"class":59},";\n",[49,87,89,92,94,97],{"class":51,"line":88},4,[49,90,91],{"class":59},"    r ",[49,93,78],{"class":55},[49,95,96],{"class":55}," &",[49,98,99],{"class":59},"x;\n",[49,101,103,106],{"class":51,"line":102},5,[49,104,105],{"class":59},"}                       ",[49,107,109],{"class":108},"sdCPZ","\u002F\u002F x dropped here\n",[49,111,113,117,120,124,127],{"class":51,"line":112},6,[49,114,116],{"class":115},"sIsaT","println!",[49,118,119],{"class":59},"(",[49,121,123],{"class":122},"sJ6F3","\"{r}\"",[49,125,126],{"class":59},");        ",[49,128,129],{"class":108},"\u002F\u002F ERROR: x does not live long enough\n",[23,131,133],{"id":132},"generic-lifetime-parameters","Generic Lifetime Parameters",[14,135,136,137,140],{},"When a function returns a reference, the compiler needs to know its lifetime is tied to ",[31,138,139],{},"some"," input:",[36,142,143],{"language":38},[40,144,146],{"className":42,"code":145,"language":38,"meta":44,"style":44},"fn longest\u003C'a>(x: &'a str, y: &'a str) -> &'a str {\n    if x.len() > y.len() { x } else { y }\n}\n",[46,147,148,208,238],{"__ignoreMap":44},[49,149,150,153,156,159,162,165,168,170,173,175,178,181,183,185,187,189,191,194,197,199,201,203,205],{"class":51,"line":52},[49,151,152],{"class":55},"fn",[49,154,155],{"class":115}," longest",[49,157,158],{"class":59},"\u003C'",[49,160,161],{"class":115},"a",[49,163,164],{"class":59},">(x",[49,166,167],{"class":55},":",[49,169,96],{"class":55},[49,171,172],{"class":59},"'",[49,174,161],{"class":115},[49,176,177],{"class":115}," str",[49,179,180],{"class":59},", y",[49,182,167],{"class":55},[49,184,96],{"class":55},[49,186,172],{"class":59},[49,188,161],{"class":115},[49,190,177],{"class":115},[49,192,193],{"class":59},") ",[49,195,196],{"class":55},"->",[49,198,96],{"class":55},[49,200,172],{"class":59},[49,202,161],{"class":115},[49,204,177],{"class":115},[49,206,207],{"class":59}," {\n",[49,209,210,213,216,219,222,225,227,229,232,235],{"class":51,"line":63},[49,211,212],{"class":55},"    if",[49,214,215],{"class":59}," x",[49,217,218],{"class":55},".",[49,220,221],{"class":115},"len",[49,223,224],{"class":59},"() > y",[49,226,218],{"class":55},[49,228,221],{"class":115},[49,230,231],{"class":59},"() { x } ",[49,233,234],{"class":55},"else",[49,236,237],{"class":59}," { y }\n",[49,239,240],{"class":51,"line":69},[49,241,242],{"class":59},"}\n",[14,244,245,248,249,252,253,256,257,260,261],{},[46,246,247],{},"\u003C'a>"," declares a generic lifetime. ",[46,250,251],{},"&'a str"," means \"a ",[46,254,255],{},"str"," reference valid for at least ",[46,258,259],{},"'a","\". The signature says: ",[31,262,263,264,267,268,218],{},"the returned reference is valid for at least as long as the shorter of ",[46,265,266],{},"x"," and ",[46,269,270],{},"y",[23,272,274],{"id":273},"lifetime-elision-rules","Lifetime Elision Rules",[14,276,277],{},"To reduce boilerplate, the compiler applies three elision rules:",[279,280,281,292,301],"ol",{},[282,283,284,285,288,289,218],"li",{},"Each input reference gets its own lifetime: ",[46,286,287],{},"fn f(x: &str, y: &str)"," → ",[46,290,291],{},"fn f\u003C'a, 'b>(x: &'a str, y: &'b str)",[282,293,294,295,288,298,218],{},"If there's exactly one input lifetime, all output references get that lifetime: ",[46,296,297],{},"fn f(x: &str) -> &str",[46,299,300],{},"fn f\u003C'a>(x: &'a str) -> &'a str",[282,302,303,304,307,308,311,312,315],{},"If there are multiple inputs but one is ",[46,305,306],{},"&self","\u002F",[46,309,310],{},"&mut self",", all output lifetimes get ",[46,313,314],{},"self","'s lifetime (method elision).",[14,317,318],{},"If after these rules the output lifetime is ambiguous, you must write it explicitly:",[36,320,321],{"language":38},[40,322,324],{"className":42,"code":323,"language":38,"meta":44,"style":44},"fn longest\u003C'a>(x: &'a str, y: &'a str) -> &'a str { ... }\n",[46,325,326],{"__ignoreMap":44},[49,327,328,330,332,334,336,338,340,342,344,346,348,350,352,354,356,358,360,362,364,366,368,370,372,375,378],{"class":51,"line":52},[49,329,152],{"class":55},[49,331,155],{"class":115},[49,333,158],{"class":59},[49,335,161],{"class":115},[49,337,164],{"class":59},[49,339,167],{"class":55},[49,341,96],{"class":55},[49,343,172],{"class":59},[49,345,161],{"class":115},[49,347,177],{"class":115},[49,349,180],{"class":59},[49,351,167],{"class":55},[49,353,96],{"class":55},[49,355,172],{"class":59},[49,357,161],{"class":115},[49,359,177],{"class":115},[49,361,193],{"class":59},[49,363,196],{"class":55},[49,365,96],{"class":55},[49,367,172],{"class":59},[49,369,161],{"class":115},[49,371,177],{"class":115},[49,373,374],{"class":59}," { ",[49,376,377],{"class":55},"...",[49,379,380],{"class":59}," }\n",[23,382,384],{"id":383},"static",[46,385,386],{},"'static",[14,388,389,390,392],{},"The ",[46,391,386],{}," lifetime lasts the entire program. Examples:",[394,395,396,407,413],"ul",{},[282,397,398,399,402,403,406],{},"All string literals: ",[46,400,401],{},"\"hello\""," has type ",[46,404,405],{},"&'static str"," (stored in the binary).",[282,408,409,412],{},[46,410,411],{},"const"," values that are references.",[282,414,415],{},"Global statics.",[36,417,418],{"language":38},[40,419,421],{"className":42,"code":420,"language":38,"meta":44,"style":44},"let s: &'static str = \"I live forever\";\n",[46,422,423],{"__ignoreMap":44},[49,424,425,427,430,432,434,436,438,440,443,446],{"class":51,"line":52},[49,426,56],{"class":55},[49,428,429],{"class":59}," s",[49,431,167],{"class":55},[49,433,96],{"class":55},[49,435,172],{"class":59},[49,437,383],{"class":115},[49,439,177],{"class":115},[49,441,442],{"class":55}," =",[49,444,445],{"class":122}," \"I live forever\"",[49,447,85],{"class":59},[14,449,450,451,453,454,456,457,459],{},"Don't reach for ",[46,452,386],{}," to silence lifetime errors — it usually means a design problem. Common accidental ",[46,455,386],{},": spawning threads that capture references requires ",[46,458,386],{}," (see Concurrency chapter).",[23,461,463],{"id":462},"structs-holding-references","Structs Holding References",[14,465,466],{},"If a struct holds a reference, it must declare a lifetime:",[36,468,469],{"language":38},[40,470,472],{"className":42,"code":471,"language":38,"meta":44,"style":44},"struct Excerpt\u003C'a> { part: &'a str }\n\nlet novel = String::from(\"a long novel...\");\nlet first = novel.split(' ').next().unwrap();\nlet e = Excerpt { part: first };\n",[46,473,474,501,507,533,574],{"__ignoreMap":44},[49,475,476,479,482,484,486,489,491,493,495,497,499],{"class":51,"line":52},[49,477,478],{"class":55},"struct",[49,480,481],{"class":115}," Excerpt",[49,483,158],{"class":59},[49,485,161],{"class":115},[49,487,488],{"class":59},"> { part",[49,490,167],{"class":55},[49,492,96],{"class":55},[49,494,172],{"class":59},[49,496,161],{"class":115},[49,498,177],{"class":115},[49,500,380],{"class":59},[49,502,503],{"class":51,"line":63},[49,504,506],{"emptyLinePlaceholder":505},true,"\n",[49,508,509,511,514,516,519,522,525,527,530],{"class":51,"line":69},[49,510,56],{"class":55},[49,512,513],{"class":59}," novel ",[49,515,78],{"class":55},[49,517,518],{"class":115}," String",[49,520,521],{"class":55},"::",[49,523,524],{"class":115},"from",[49,526,119],{"class":59},[49,528,529],{"class":122},"\"a long novel...\"",[49,531,532],{"class":59},");\n",[49,534,535,537,540,542,545,547,550,552,555,558,560,563,566,568,571],{"class":51,"line":88},[49,536,56],{"class":55},[49,538,539],{"class":59}," first ",[49,541,78],{"class":55},[49,543,544],{"class":59}," novel",[49,546,218],{"class":55},[49,548,549],{"class":115},"split",[49,551,119],{"class":59},[49,553,554],{"class":122},"' '",[49,556,557],{"class":59},")",[49,559,218],{"class":55},[49,561,562],{"class":115},"next",[49,564,565],{"class":59},"()",[49,567,218],{"class":55},[49,569,570],{"class":115},"unwrap",[49,572,573],{"class":59},"();\n",[49,575,576,578,581,583,585,588,590],{"class":51,"line":102},[49,577,56],{"class":55},[49,579,580],{"class":59}," e ",[49,582,78],{"class":55},[49,584,481],{"class":115},[49,586,587],{"class":59}," { part",[49,589,167],{"class":55},[49,591,592],{"class":59}," first };\n",[14,594,595,596,599],{},"The struct can't outlive the data ",[46,597,598],{},"part"," borrows. The compiler enforces this.",[23,601,603],{"id":602},"lifetimes-in-method-signatures","Lifetimes in Method Signatures",[36,605,606],{"language":38},[40,607,609],{"className":42,"code":608,"language":38,"meta":44,"style":44},"impl\u003C'a> Excerpt\u003C'a> {\n    fn announce(&self, msg: &str) -> &str {\n        println!(\"{msg}{}\", self.part);\n        self.part            \u002F\u002F elided: returns &'a str (rule 3)\n    }\n}\n",[46,610,611,633,667,687,700,705],{"__ignoreMap":44},[49,612,613,616,618,620,623,626,628,630],{"class":51,"line":52},[49,614,615],{"class":55},"impl",[49,617,158],{"class":59},[49,619,161],{"class":115},[49,621,622],{"class":59},"> ",[49,624,625],{"class":115},"Excerpt",[49,627,158],{"class":59},[49,629,161],{"class":115},[49,631,632],{"class":59},"> {\n",[49,634,635,638,641,643,646,648,651,653,655,657,659,661,663,665],{"class":51,"line":63},[49,636,637],{"class":55},"    fn",[49,639,640],{"class":115}," announce",[49,642,119],{"class":59},[49,644,645],{"class":55},"&",[49,647,314],{"class":81},[49,649,650],{"class":59},", msg",[49,652,167],{"class":55},[49,654,96],{"class":55},[49,656,255],{"class":115},[49,658,193],{"class":59},[49,660,196],{"class":55},[49,662,96],{"class":55},[49,664,255],{"class":115},[49,666,207],{"class":59},[49,668,669,672,674,677,680,682,684],{"class":51,"line":69},[49,670,671],{"class":115},"        println!",[49,673,119],{"class":59},[49,675,676],{"class":122},"\"{msg}{}\"",[49,678,679],{"class":59},", ",[49,681,314],{"class":81},[49,683,218],{"class":55},[49,685,686],{"class":59},"part);\n",[49,688,689,692,694,697],{"class":51,"line":88},[49,690,691],{"class":81},"        self",[49,693,218],{"class":55},[49,695,696],{"class":59},"part            ",[49,698,699],{"class":108},"\u002F\u002F elided: returns &'a str (rule 3)\n",[49,701,702],{"class":51,"line":102},[49,703,704],{"class":59},"    }\n",[49,706,707],{"class":51,"line":112},[49,708,242],{"class":59},[14,710,711,712,714],{},"Output references are tied to ",[46,713,306],{}," automatically when there's no other choice.",[23,716,718],{"id":717},"multiple-lifetimes","Multiple Lifetimes",[36,720,721],{"language":38},[40,722,724],{"className":42,"code":723,"language":38,"meta":44,"style":44},"struct Parser\u003C'src, 'arena> {\n    source: &'src str,\n    arena: &'arena Arena,\n}\n",[46,725,726,746,764,782],{"__ignoreMap":44},[49,727,728,730,733,735,738,741,744],{"class":51,"line":52},[49,729,478],{"class":55},[49,731,732],{"class":115}," Parser",[49,734,158],{"class":59},[49,736,737],{"class":115},"src",[49,739,740],{"class":59},", '",[49,742,743],{"class":115},"arena",[49,745,632],{"class":59},[49,747,748,751,753,755,757,759,761],{"class":51,"line":63},[49,749,750],{"class":59},"    source",[49,752,167],{"class":55},[49,754,96],{"class":55},[49,756,172],{"class":59},[49,758,737],{"class":115},[49,760,177],{"class":115},[49,762,763],{"class":59},",\n",[49,765,766,769,771,773,775,777,780],{"class":51,"line":69},[49,767,768],{"class":59},"    arena",[49,770,167],{"class":55},[49,772,96],{"class":55},[49,774,172],{"class":59},[49,776,743],{"class":115},[49,778,779],{"class":115}," Arena",[49,781,763],{"class":59},[49,783,784],{"class":51,"line":88},[49,785,242],{"class":59},[14,787,788,789,792,793,796,797,800,801,803],{},"Two distinct lifetimes express \"the source lives at least ",[46,790,791],{},"'src",", the arena lives at least ",[46,794,795],{},"'arena","\". If the struct never mixes them (e.g., never puts ",[46,798,799],{},"source"," into ",[46,802,743],{},"), this is more flexible than collapsing to one lifetime.",[23,805,807],{"id":806},"lifetime-variance-advanced","Lifetime Variance (advanced)",[14,809,810,811,814,815,818,819,821,822,824,825,827],{},"A ",[46,812,813],{},"&'a T"," is ",[18,816,817],{},"covariant"," in ",[46,820,259],{},": you can use a longer-lived reference where a shorter-lived one is expected. ",[46,823,405],{}," fits anywhere ",[46,826,251],{}," is needed.",[14,829,830,833,834,836,837,818,840,843,844,846,847,679,850,679,853,856],{},[46,831,832],{},"&'a mut T"," is covariant in ",[46,835,259],{}," but ",[18,838,839],{},"invariant",[46,841,842],{},"T"," (you can't shorten the borrow of ",[46,845,842],{}," because mutation could write back). ",[46,848,849],{},"Cell\u003CT>",[46,851,852],{},"RefCell\u003CT>",[46,854,855],{},"UnsafeCell\u003CT>"," are invariant.",[14,858,859,862,863,865],{},[46,860,861],{},"fn(&'a T)"," is contravariant in ",[46,864,259],{},". Most code doesn't think about variance, but it explains why some seemingly valid code compiles or doesn't.",[23,867,869],{"id":868},"higher-rank-trait-bounds-hrtb","Higher-Rank Trait Bounds (HRTB)",[36,871,872],{"language":38},[40,873,875],{"className":42,"code":874,"language":38,"meta":44,"style":44},"fn foo\u003CF>(f: F) where F: for\u003C'a> Fn(&'a str) { ... }\n",[46,876,877],{"__ignoreMap":44},[49,878,879,881,884,887,890,893,895,898,900,903,905,907,910,912,914,916,919,921,923,925,927,929,932,934],{"class":51,"line":52},[49,880,152],{"class":55},[49,882,883],{"class":115}," foo",[49,885,886],{"class":59},"\u003C",[49,888,889],{"class":115},"F",[49,891,892],{"class":59},">(f",[49,894,167],{"class":55},[49,896,897],{"class":115}," F",[49,899,193],{"class":59},[49,901,902],{"class":55},"where",[49,904,897],{"class":115},[49,906,167],{"class":55},[49,908,909],{"class":55}," for",[49,911,158],{"class":59},[49,913,161],{"class":115},[49,915,622],{"class":59},[49,917,918],{"class":115},"Fn",[49,920,119],{"class":59},[49,922,645],{"class":55},[49,924,172],{"class":59},[49,926,161],{"class":115},[49,928,177],{"class":115},[49,930,931],{"class":59},") { ",[49,933,377],{"class":55},[49,935,380],{"class":59},[14,937,938,941,942,944,945,947],{},[46,939,940],{},"for\u003C'a>"," means \"for all possible lifetimes ",[46,943,259],{},"\". Closures that work with any borrowed input need this. The ",[46,946,918],{}," traits implicitly have HRTB on their arguments.",[23,949,951,952],{"id":950},"anonymous-lifetime-_","Anonymous Lifetime ",[46,953,954],{},"'_",[36,956,957],{"language":38},[40,958,960],{"className":42,"code":959,"language":38,"meta":44,"style":44},"fn longest(x: &str, y: &str) -> &'_ str { ... }\n",[46,961,962],{"__ignoreMap":44},[49,963,964,966,968,971,973,975,977,979,981,983,985,987,989,991,993,996,998,1000,1002],{"class":51,"line":52},[49,965,152],{"class":55},[49,967,155],{"class":115},[49,969,970],{"class":59},"(x",[49,972,167],{"class":55},[49,974,96],{"class":55},[49,976,255],{"class":115},[49,978,180],{"class":59},[49,980,167],{"class":55},[49,982,96],{"class":55},[49,984,255],{"class":115},[49,986,193],{"class":59},[49,988,196],{"class":55},[49,990,96],{"class":55},[49,992,172],{"class":59},[49,994,995],{"class":115},"_",[49,997,177],{"class":115},[49,999,374],{"class":59},[49,1001,377],{"class":55},[49,1003,380],{"class":59},[14,1005,1006,1008,1009,1012],{},[46,1007,954],{}," is \"use elision here\". Useful in ",[46,1010,1011],{},"impl Trait"," positions and to silence \"elided lifetime in path\" warnings. Don't sprinkle it; only when you specifically want elision.",[23,1014,1016],{"id":1015},"lifetime-in-enums","Lifetime in Enums",[14,1018,1019],{},"Same rules as structs:",[36,1021,1022],{"language":38},[40,1023,1025],{"className":42,"code":1024,"language":38,"meta":44,"style":44},"enum Node\u003C'a> { Leaf(&'a str), Branch(&'a [Node\u003C'a>]) }\n",[46,1026,1027],{"__ignoreMap":44},[49,1028,1029,1032,1035,1037,1039,1042,1045,1047,1049,1051,1053,1055,1058,1061,1063,1065,1067,1069,1072,1075,1077,1079],{"class":51,"line":52},[49,1030,1031],{"class":55},"enum",[49,1033,1034],{"class":115}," Node",[49,1036,158],{"class":59},[49,1038,161],{"class":115},[49,1040,1041],{"class":59},"> { ",[49,1043,1044],{"class":115},"Leaf",[49,1046,119],{"class":59},[49,1048,645],{"class":55},[49,1050,172],{"class":59},[49,1052,161],{"class":115},[49,1054,177],{"class":115},[49,1056,1057],{"class":59},"), ",[49,1059,1060],{"class":115},"Branch",[49,1062,119],{"class":59},[49,1064,645],{"class":55},[49,1066,172],{"class":59},[49,1068,161],{"class":115},[49,1070,1071],{"class":59}," [",[49,1073,1074],{"class":115},"Node",[49,1076,158],{"class":59},[49,1078,161],{"class":115},[49,1080,1081],{"class":59},">]) }\n",[23,1083,1085],{"id":1084},"static-vs-stack-lifetimes","Static vs Stack Lifetimes",[36,1087,1088],{"language":38},[40,1089,1091],{"className":42,"code":1090,"language":38,"meta":44,"style":44},"fn returns_str() -> &'static str { \"literal\" }  \u002F\u002F OK\nfn returns_stack() -> &str {\n    let local = String::from(\"hi\");\n    &local                  \u002F\u002F ERROR: local does not live long enough\n}\n",[46,1092,1093,1124,1141,1163,1174],{"__ignoreMap":44},[49,1094,1095,1097,1100,1103,1105,1107,1109,1111,1113,1115,1118,1121],{"class":51,"line":52},[49,1096,152],{"class":55},[49,1098,1099],{"class":115}," returns_str",[49,1101,1102],{"class":59},"() ",[49,1104,196],{"class":55},[49,1106,96],{"class":55},[49,1108,172],{"class":59},[49,1110,383],{"class":115},[49,1112,177],{"class":115},[49,1114,374],{"class":59},[49,1116,1117],{"class":122},"\"literal\"",[49,1119,1120],{"class":59}," }  ",[49,1122,1123],{"class":108},"\u002F\u002F OK\n",[49,1125,1126,1128,1131,1133,1135,1137,1139],{"class":51,"line":63},[49,1127,152],{"class":55},[49,1129,1130],{"class":115}," returns_stack",[49,1132,1102],{"class":59},[49,1134,196],{"class":55},[49,1136,96],{"class":55},[49,1138,255],{"class":115},[49,1140,207],{"class":59},[49,1142,1143,1145,1148,1150,1152,1154,1156,1158,1161],{"class":51,"line":69},[49,1144,72],{"class":55},[49,1146,1147],{"class":59}," local ",[49,1149,78],{"class":55},[49,1151,518],{"class":115},[49,1153,521],{"class":55},[49,1155,524],{"class":115},[49,1157,119],{"class":59},[49,1159,1160],{"class":122},"\"hi\"",[49,1162,532],{"class":59},[49,1164,1165,1168,1171],{"class":51,"line":88},[49,1166,1167],{"class":55},"    &",[49,1169,1170],{"class":59},"local                  ",[49,1172,1173],{"class":108},"\u002F\u002F ERROR: local does not live long enough\n",[49,1175,1176],{"class":51,"line":102},[49,1177,242],{"class":59},[23,1179,1181],{"id":1180},"lifetime-bounds-on-generics","Lifetime Bounds on Generics",[36,1183,1184],{"language":38},[40,1185,1187],{"className":42,"code":1186,"language":38,"meta":44,"style":44},"fn parse\u003CT>(s: &str) -> T where T: FromStr, T::Err: Debug { ... }\nfn longest_anon\u003C'a, T: 'a>(x: &'a T) -> &'a T { x }\n",[46,1188,1189,1246],{"__ignoreMap":44},[49,1190,1191,1193,1196,1198,1200,1203,1205,1207,1209,1211,1213,1216,1219,1221,1223,1226,1228,1230,1232,1235,1237,1240,1242,1244],{"class":51,"line":52},[49,1192,152],{"class":55},[49,1194,1195],{"class":115}," parse",[49,1197,886],{"class":59},[49,1199,842],{"class":115},[49,1201,1202],{"class":59},">(s",[49,1204,167],{"class":55},[49,1206,96],{"class":55},[49,1208,255],{"class":115},[49,1210,193],{"class":59},[49,1212,196],{"class":55},[49,1214,1215],{"class":115}," T",[49,1217,1218],{"class":55}," where",[49,1220,1215],{"class":115},[49,1222,167],{"class":55},[49,1224,1225],{"class":115}," FromStr",[49,1227,679],{"class":59},[49,1229,842],{"class":115},[49,1231,521],{"class":55},[49,1233,1234],{"class":115},"Err",[49,1236,167],{"class":55},[49,1238,1239],{"class":115}," Debug",[49,1241,374],{"class":59},[49,1243,377],{"class":55},[49,1245,380],{"class":59},[49,1247,1248,1250,1253,1255,1257,1259,1261,1263,1266,1268,1270,1272,1274,1276,1278,1280,1282,1284,1286,1288,1290,1292],{"class":51,"line":63},[49,1249,152],{"class":55},[49,1251,1252],{"class":115}," longest_anon",[49,1254,158],{"class":59},[49,1256,161],{"class":115},[49,1258,679],{"class":59},[49,1260,842],{"class":115},[49,1262,167],{"class":55},[49,1264,1265],{"class":59}," '",[49,1267,161],{"class":115},[49,1269,164],{"class":59},[49,1271,167],{"class":55},[49,1273,96],{"class":55},[49,1275,172],{"class":59},[49,1277,161],{"class":115},[49,1279,1215],{"class":115},[49,1281,193],{"class":59},[49,1283,196],{"class":55},[49,1285,96],{"class":55},[49,1287,172],{"class":59},[49,1289,161],{"class":115},[49,1291,1215],{"class":115},[49,1293,1294],{"class":59}," { x }\n",[14,1296,1297,1300,1301,1303,1304,1306],{},[46,1298,1299],{},"T: 'a"," means \"T's owned references (if any) outlive ",[46,1302,259],{},"\". Often implicit, but needed when ",[46,1305,842],{}," itself contains references.",[23,1308,1310,1311],{"id":1309},"lifetime-extension-via-boxleak","Lifetime Extension via ",[46,1312,1313],{},"Box::leak",[36,1315,1316],{"language":38},[40,1317,1319],{"className":42,"code":1318,"language":38,"meta":44,"style":44},"let leaked: &'static mut [u8] = Box::leak(vec![1, 2, 3].into_boxed_slice());\n",[46,1320,1321],{"__ignoreMap":44},[49,1322,1323,1325,1328,1330,1332,1334,1336,1339,1341,1344,1347,1349,1352,1354,1357,1359,1362,1365,1368,1370,1373,1375,1378,1381,1383,1386],{"class":51,"line":52},[49,1324,56],{"class":55},[49,1326,1327],{"class":59}," leaked",[49,1329,167],{"class":55},[49,1331,96],{"class":55},[49,1333,172],{"class":59},[49,1335,383],{"class":115},[49,1337,1338],{"class":55}," mut",[49,1340,1071],{"class":59},[49,1342,1343],{"class":115},"u8",[49,1345,1346],{"class":59},"] ",[49,1348,78],{"class":55},[49,1350,1351],{"class":115}," Box",[49,1353,521],{"class":55},[49,1355,1356],{"class":115},"leak",[49,1358,119],{"class":59},[49,1360,1361],{"class":115},"vec!",[49,1363,1364],{"class":59},"[",[49,1366,1367],{"class":81},"1",[49,1369,679],{"class":59},[49,1371,1372],{"class":81},"2",[49,1374,679],{"class":59},[49,1376,1377],{"class":81},"3",[49,1379,1380],{"class":59},"]",[49,1382,218],{"class":55},[49,1384,1385],{"class":115},"into_boxed_slice",[49,1387,1388],{"class":59},"());\n",[14,1390,1391,1393,1394,1396],{},[46,1392,1313],{}," turns an owned heap value into a ",[46,1395,386],{}," reference (memory is never reclaimed). Useful for long-lived configs but a memory leak by design.",[23,1398,1400],{"id":1399},"common-lifetime-errors-fixes","Common Lifetime Errors & Fixes",[394,1402,1403,1409,1419,1433],{},[282,1404,1405,1408],{},[18,1406,1407],{},"\"borrowed value does not live long enough\"",": the referent's scope is too short. Restructure so it outlives the borrow, or clone\u002Fown.",[282,1410,1411,1414,1415,1418],{},[18,1412,1413],{},"\"lifetime may not live long enough\"",": explicit lifetimes where the relationship is wrong; usually you need to express that the output is bound to ",[31,1416,1417],{},"one specific"," input.",[282,1420,1421,1424,1425,1428,1429,1432],{},[18,1422,1423],{},"\"returns a value referencing data owned by the current function\"",": returning a reference to a local. You must return an owned value (e.g., ",[46,1426,1427],{},"String"," not ",[46,1430,1431],{},"&str","), or take the data as input.",[282,1434,1435,1441,1442,1444],{},[18,1436,1437,1438,1440],{},"Adding ",[46,1439,386],{}," to silence",": usually wrong. Thread spawns require ",[46,1443,386],{}," for closure captures; you need owned data there.",[23,1446,1448],{"id":1447},"lifetime-patterns-cheat-sheet","Lifetime Patterns Cheat Sheet",[1450,1451,1452,1465],"table",{},[1453,1454,1455],"thead",{},[1456,1457,1458,1462],"tr",{},[1459,1460,1461],"th",{},"Function returns…",[1459,1463,1464],{},"What you do",[1466,1467,1468,1477,1485,1501,1515],"tbody",{},[1456,1469,1470,1474],{},[1471,1472,1473],"td",{},"A reference clearly tied to one input",[1471,1475,1476],{},"Rely on elision",[1456,1478,1479,1482],{},[1471,1480,1481],{},"A reference derived from multiple inputs",[1471,1483,1484],{},"Pick the relevant input lifetime and annotate",[1456,1486,1487,1490],{},[1471,1488,1489],{},"A reference to newly created data",[1471,1491,1492,1493,679,1495,1498,1499],{},"Return owned (",[46,1494,1427],{},[46,1496,1497],{},"Vec\u003CT>","), not ",[46,1500,645],{},[1456,1502,1503,1508],{},[1471,1504,810,1505,1507],{},[46,1506,386],{}," literal or constant",[1471,1509,1510,1511,1514],{},"Write ",[46,1512,1513],{},"&'static"," explicitly",[1456,1516,1517,1520],{},[1471,1518,1519],{},"Data tied to a self-borrow",[1471,1521,1522,1523,1525],{},"Use ",[46,1524,314],{},"-elision",[23,1527,1529],{"id":1528},"edge-cases","Edge Cases",[394,1531,1532,1556,1562,1573,1585],{},[282,1533,1534,1543,1544,1547,1548,1551,1552,267,1554,218],{},[18,1535,1536,1538,1539,1542],{},[46,1537,259],{}," ties output to the ",[31,1540,1541],{},"shortest"," input",": ",[46,1545,1546],{},"longest\u003C'a>(x: &'a, y: &'a)"," means the result lives at most as long as the ",[31,1549,1550],{},"shorter"," of ",[46,1553,266],{},[46,1555,270],{},[282,1557,1558,1561],{},[18,1559,1560],{},"Closures capturing references",": the closure's lifetime must include the captured references' lifetimes.",[282,1563,1564,1543,1567,1570,1571,218],{},[18,1565,1566],{},"Iterators holding references",[46,1568,1569],{},"std::slice::Iter\u003C'a, T>"," borrows the slice for ",[46,1572,259],{},[282,1574,1575,1580,1581,1584],{},[18,1576,1577,1579],{},[46,1578,314],{},"-referential structs"," are famously hard in safe Rust; use ",[46,1582,1583],{},"ouroboros"," crate or restructure. The borrow checker can't express \"this field borrows from that field of the same struct\".",[282,1586,1587,1590,1591,1594],{},[18,1588,1589],{},"Async functions"," desugar to state machines that hold references across ",[46,1592,1593],{},".await"," points — lifetimes get complex; usually you must own the data instead of borrowing.",[23,1596,1598],{"id":1597},"summary","Summary",[14,1600,1601],{},"Lifetimes are how Rust makes references safe. They:",[394,1603,1604,1607,1610,1613],{},[282,1605,1606],{},"Annotate relationships between inputs and outputs.",[282,1608,1609],{},"Get elided in common cases.",[282,1611,1612],{},"Enforce that no reference outlives its referent.",[282,1614,1615],{},"Sometimes need explicit annotation when multiple inputs feed an output.",[14,1617,1618],{},"Next: Structs and enums — the algebraic data types at the heart of Rust modeling.",[1620,1621,1622],"style",{},"html pre.shiki code .svdQ7, html code.shiki .svdQ7{--shiki-default:#D73A49;--shiki-github-dark:#F97583}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 .sdCPZ, html code.shiki .sdCPZ{--shiki-default:#6A737D;--shiki-github-dark:#6A737D}html pre.shiki code .sIsaT, html code.shiki .sIsaT{--shiki-default:#6F42C1;--shiki-github-dark:#B392F0}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":44,"searchDepth":63,"depth":63,"links":1624},[1625,1626,1627,1628,1629,1630,1631,1632,1633,1634,1636,1637,1638,1639,1641,1642,1643,1644],{"id":25,"depth":63,"text":26},{"id":132,"depth":63,"text":133},{"id":273,"depth":63,"text":274},{"id":383,"depth":63,"text":386},{"id":462,"depth":63,"text":463},{"id":602,"depth":63,"text":603},{"id":717,"depth":63,"text":718},{"id":806,"depth":63,"text":807},{"id":868,"depth":63,"text":869},{"id":950,"depth":63,"text":1635},"Anonymous Lifetime '_",{"id":1015,"depth":63,"text":1016},{"id":1084,"depth":63,"text":1085},{"id":1180,"depth":63,"text":1181},{"id":1309,"depth":63,"text":1640},"Lifetime Extension via Box::leak",{"id":1399,"depth":63,"text":1400},{"id":1447,"depth":63,"text":1448},{"id":1528,"depth":63,"text":1529},{"id":1597,"depth":63,"text":1598},"Lifetimes are the borrow checker's way of tracking how long a reference is valid. They're a compile-time concept; there's zero runtime cost.","md",{},"\u002Frust\u002F10-lifetimes",{"title":5,"description":1645},"rust\u002F10-lifetimes","X2Ikd4gLMmWji-UcKNzsYf7eRpvpFXNchIhNiF4a-kw",1785944093221]