rust.texi 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename rust.info
  4. @settitle Rust Documentation
  5. @setchapternewpage odd
  6. @c %**end of header
  7. @syncodeindex fn cp
  8. @ifinfo
  9. This manual is for the ``Rust'' programming language.
  10. Copyright 2006-2010 Graydon Hoare
  11. Copyright 2009-2010 Mozilla Foundation
  12. All rights reserved (for the time being).
  13. @end ifinfo
  14. @dircategory Programming
  15. @direntry
  16. * rust: (rust). Rust programming language
  17. @end direntry
  18. @titlepage
  19. @title Rust
  20. @subtitle A safe, concurrent, practical language.
  21. @author Graydon Hoare
  22. @author Mozilla Foundation
  23. @page
  24. @vskip 0pt plus 1filll
  25. Copyright @copyright{} 2006-2010 Graydon Hoare
  26. Copyright @copyright{} 2009-2010 Mozilla Foundation
  27. See accompanying LICENSE.txt for terms.
  28. @end titlepage
  29. @ifnottex
  30. @node Top
  31. @top Top
  32. Rust Documentation
  33. @end ifnottex
  34. @menu
  35. * Disclaimer:: Notes on a work in progress.
  36. * Introduction:: Background, intentions, lineage.
  37. * Tutorial:: Gentle introduction to reading Rust code.
  38. * Reference:: Systematic reference of language elements.
  39. * Index:: Index
  40. @end menu
  41. @ifnottex
  42. Complete table of contents
  43. @end ifnottex
  44. @contents
  45. @c ############################################################
  46. @c Disclaimer
  47. @c ############################################################
  48. @node Disclaimer
  49. @chapter Disclaimer
  50. To the reader,
  51. Rust is a work in progress. The language continues to evolve as the design
  52. shifts and is fleshed out in working code. Certain parts work, certain parts
  53. do not, certain parts will be removed or changed.
  54. This manual is a snapshot written in the present tense. Some features
  55. described do not yet exist in working code. Some may be temporary. It
  56. is a @emph{draft}, and we ask that you not take anything you read here
  57. as either definitive or final. The manual is to help you get a sense
  58. of the language and its organization, not to serve as a complete
  59. specification. At least not yet.
  60. If you have suggestions to make, please try to focus them on @emph{reductions}
  61. to the language: possible features that can be combined or omitted. At this
  62. point, every ``additive'' feature we're likely to support is already on the
  63. table. The task ahead involves combining, trimming, and implementing.
  64. @c ############################################################
  65. @c Introduction
  66. @c ############################################################
  67. @node Introduction
  68. @chapter Introduction
  69. @quotation
  70. We have to fight chaos, and the most effective way of doing that is
  71. to prevent its emergence.
  72. @flushright
  73. - Edsger Dijkstra
  74. @end flushright
  75. @end quotation
  76. @sp 2
  77. Rust is a curly-brace, block-structured statement language. It visually
  78. resembles the C language family, but differs significantly in syntactic and
  79. semantic details. Its design is oriented toward concerns of ``programming in
  80. the large'', that is, of creating and maintaining @emph{boundaries} -- both
  81. abstract and operational -- that preserve large-system @emph{integrity},
  82. @emph{availability} and @emph{concurrency}.
  83. It supports a mixture of imperative procedural, concurrent actor, object
  84. oriented and pure functional styles. Rust also supports generic programming
  85. and metaprogramming, in both static and dynamic styles.
  86. @menu
  87. * Goals:: Intentions, motivations.
  88. * Sales Pitch:: A summary for the impatient.
  89. * Influences:: Relationship to past languages.
  90. @end menu
  91. @node Goals
  92. @section Goals
  93. The language design pursues the following goals:
  94. @sp 1
  95. @itemize
  96. @item Compile-time error detection and prevention.
  97. @item Run-time fault tolerance and containment.
  98. @item System building, analysis and maintenance affordances.
  99. @item Clarity and precision of expression.
  100. @item Implementation simplicity.
  101. @item Run-time efficiency.
  102. @item High concurrency.
  103. @end itemize
  104. @sp 1
  105. Note that most of these goals are @emph{engineering} goals, not showcases for
  106. sophisticated language technology. Most of the technology in Rust is
  107. @emph{old} and has been seen decades earlier in other languages.
  108. All new languages are developed in a technological context. Rust's goals arise
  109. from the context of writing large programs that interact with the internet --
  110. both servers and clients -- and are thus much more concerned with
  111. @emph{safety} and @emph{concurrency} than older generations of program. Our
  112. experience is that these two forces do not conflict; rather they drive system
  113. design decisions toward extensive use of @emph{partitioning} and
  114. @emph{statelessness}. Rust aims to make these a more natural part of writing
  115. programs, within the niche of lower-level, practical, resource-conscious
  116. languages.
  117. @page
  118. @node Sales Pitch
  119. @section Sales Pitch
  120. The following comprises a brief ``sales pitch'' overview of the salient
  121. features of Rust, relative to other languages.
  122. @itemize
  123. @sp 1
  124. @item No @code{null} pointers
  125. The initialization state of every slot is statically computed as part of the
  126. typestate system (see below), and requires that all slots are initialized
  127. before use. There is no @code{null} value; uninitialized slots are
  128. uninitialized, and can only be written to, not read.
  129. The common use for @code{null} in other languages -- as a sentinel value -- is
  130. subsumed into the more general facility of disjoint union types. A program
  131. must explicitly model its use of such types.
  132. @sp 1
  133. @item Lightweight tasks with no shared mutable state
  134. Like many @emph{actor} languages, Rust provides an isolation (and concurrency)
  135. model based on lightweight tasks scheduled by the language runtime. These
  136. tasks are very inexpensive and statically unable to mutate one another's local
  137. memory. Breaking the rule of task isolation is only possible by calling
  138. external (C/C++) code.
  139. Inter-task communication is typed, asynchronous and simplex, based on passing
  140. messages over channels to ports. Transmission can be rate-limited or
  141. rate-unlimited. Selection between multiple senders is pseudo-randomized on the
  142. receiver side.
  143. @sp 1
  144. @item Predictable native code, simple runtime
  145. The meaning and cost of every operation within a Rust program is intended to
  146. be easy to model for the reader. The code should not ``surprise'' the
  147. programmer once it has been compiled.
  148. Rust compiles to native code. Rust compilation units are large and the
  149. compilation model is designed around multi-file, whole-library or
  150. whole-program optimization. The compiled units are standard loadable objects
  151. (ELF, PE, Mach-O) containing standard metadata (DWARF) and are compatible with
  152. existing, standard low-level tools (disassemblers, debuggers, profilers,
  153. dynamic loaders).
  154. The Rust runtime library is a small collection of support code for scheduling,
  155. memory management, inter-task communication, reflection and runtime
  156. linkage. This library is written in standard C++ and is quite
  157. straightforward. It presents a simple interface to embeddings. No
  158. research-level virtual machine, JIT or garbage collection technology is
  159. required. It should be relatively easy to adapt a Rust front-end on to many
  160. existing native toolchains.
  161. @sp 1
  162. @item Integrated system-construction facility
  163. The units of compilation of Rust are multi-file amalgamations called
  164. @emph{crates}. A crate is described by a separate, declarative type of source
  165. file that guides the compilation of the crate, its packaging, its versioning,
  166. and its external dependencies. Crates are also the units of distribution and
  167. loading. Significantly: the dependency graph of crates is @emph{acyclic} and
  168. @emph{anonymous}: there is no global namespace for crates, and module-level
  169. recursion cannot cross crate barriers.
  170. Unlike many languages, individual modules do @emph{not} carry all the
  171. mechanisms or restrictions of crates. Modules and crates serve different
  172. roles.
  173. @sp 1
  174. @item Stack-based iterators
  175. Rust provides a type of function-like multiple-invocation iterator that is
  176. very efficient: the iterator state lives only on the stack and is tightly
  177. coupled to the loop that invoked it.
  178. @sp 1
  179. @item Direct interface to C code
  180. Rust can load and call many C library functions simply by declaring
  181. them. Calling a C function statically marks a function as ``unsafe'', unless
  182. the task calling the unsafe function is further isolated within an external
  183. ``heavyweight'' operating-system subprocess. Every ``unsafe'' function or
  184. module in a Rust compilation unit must be explicitly authorized in the crate
  185. file.
  186. @sp 1
  187. @item Structural algebraic data types
  188. The Rust type system is structural rather than nominal, and contains the
  189. standard assortment of useful ``algebraic'' type constructors from functional
  190. languages, such as function types, tuples, record types, vectors, and tagged
  191. disjoint unions. Structural types may be @emph{pattern-matched} in an
  192. @code{alt} statement.
  193. @sp 1
  194. @item Generic code
  195. Rust supports a simple form of parametric polymorphism: functions, iterators,
  196. types and objects can be parametrized by other types.
  197. @sp 1
  198. @item Argument binding
  199. Rust provides a mechanism of partially binding arguments to functions,
  200. producing new functions that accept the remaining un-bound arguments. This
  201. mechanism combines some of the features of lexical closures with some of the
  202. features of currying, in a smaller and simpler package.
  203. @sp 1
  204. @item Local type inference
  205. To save some quantity of programmer key-pressing, Rust supports local type
  206. inference: signatures of functions, objects and iterators always require type
  207. annotation, but within the body of a function or iterator many slots can be
  208. declared @code{auto} and Rust will infer the slot's type from its uses.
  209. @sp 1
  210. @item Structural object system
  211. Rust has a lightweight object system based on structural object types: there
  212. is no ``class hierarchy'' nor any concept of inheritance. Method overriding
  213. and object restriction are performed explicitly on object values, which are
  214. little more than order-insensitive records of methods sharing a common private
  215. value. Objects can be mutable or immutable, and immutable objects can have
  216. destructors.
  217. @sp 1
  218. @item Dynamic type
  219. Rust includes support for slots of a top type, @code{any}, that can hold any
  220. type of value whatsoever. An @code{any} slot is a pair of a type code and an
  221. exterior value of that type. Injection into an @code{any} and projection by
  222. type-case-selection is integrated into the language.
  223. @sp 1
  224. @item Dynamic metaprogramming (reflection)
  225. Rust supports run-time reflection on the structure of a crate, using a
  226. combination of custom descriptor structures and the DWARF metadata tables used
  227. to support crate linkage and other runtime services.
  228. @sp 1
  229. @item Static metaprogramming (syntactic extension)
  230. Rust supports a system for syntactic extensions that can be loaded into the
  231. compiler, to implement user-defined notations, macros, program-generators and
  232. the like. These notations are @emph{marked} using a special form of
  233. bracketing, such that a reader unfamiliar with the extension can still parse
  234. the surrounding text by skipping over the bracketed ``extension text''.
  235. @sp 1
  236. @item Idempotent failure
  237. If a task fails due to a signal, or if it executes the special @code{fail}
  238. statement, it enters the @emph{failing} state. A failing task unwinds its
  239. control stack, frees all of its owned resources (executing destructors) and
  240. enters the @emph{dead} state. Failure is idempotent and non-recoverable.
  241. @sp 1
  242. @item Signal handling
  243. Rust has a system for propagating task-failures and other spontaneous
  244. events between tasks. Some signals can be trapped and redirected to
  245. channels; other signals are fatal and result in task-failure. Tasks
  246. can designate other tasks to handle signals for them. This permits
  247. organizing tasks into mutually-supervising or mutually-failing groups.
  248. @sp 1
  249. @item Deterministic destruction
  250. Immutable objects can have destructor functions, which are executed
  251. deterministically in top-down ownership order, as control frames are exited
  252. and/or objects are otherwise freed from data structures holding them. The same
  253. destructors are run in the same order whether the object is deleted by
  254. unwinding during failure or normal execution.
  255. Similarly, the rules for freeing immutable memory are deterministic and
  256. predictable: on scope-exit or structure-release, interior slots are released
  257. immediately, exterior slots have their reference count decreased and are
  258. released if the count drops to zero. Alias slots are not affected by scope
  259. exit.
  260. Mutable memory is local to a task, and is subject to per-task garbage
  261. collection. As a result, unreferenced mutable memory is not necessarily freed
  262. immediately; if it is acyclic it is freed when the last reference to it drops,
  263. but if it is part of a reference cycle it will be freed when the GC collects
  264. it (or when the owning task terminates, at the latest).
  265. Mutable memory can point to immutable memory but not vice-versa. Doing so
  266. merely delays (to an undefined future time) the moment when the deterministic,
  267. top-down destruction sequence for the referenced immutable memory
  268. @emph{starts}. In other words, the immutable ``leaves'' of a mutable structure
  269. are released in a locally-predictable order, even if the ``interior'' of the
  270. mutable structure is released in an unpredictable order.
  271. @sp 1
  272. @item Typestate system
  273. Every storage slot in Rust participates in not only a conventional structural
  274. static type system, describing the interpretation of memory in the slot, but
  275. also a @emph{typestate} system. The static typestates of a program describe
  276. the set of @emph{pure, dynamic predicates} that provably hold over some set of
  277. slots, at each point in the program's control flow graph. The static
  278. calculation of the typestates of a program is a dataflow problem, and handles
  279. user-defined predicates in a similar fashion to the way the type system
  280. permits user-defined types.
  281. A short way of thinking of this is: types statically model the kinds of values
  282. held in slots, typestates statically model @emph{assertions that hold} before
  283. and after statements.
  284. @sp 1
  285. @item Static control over memory allocation, packing and aliasing.
  286. Every variable or field in Rust is a combination of a type, a mutability flag
  287. and a @emph{mode}; this combination is called a @emph{slot}. There are 3 kinds
  288. of @dfn{slot mode}, denoting 3 ways of referring to a value:
  289. @itemize
  290. @item ``interior'' (slot contains value)
  291. @item ``exterior'', (slot points to to managed heap allocation)
  292. @item ``alias'', (slot points directly to provably-live address)
  293. @end itemize
  294. Interior slots declared as variables in a function are allocated very quickly
  295. on the stack, as part of a local activation frame, as in C or C++. Alias slots
  296. permit efficient by-reference parameter passing without adjusting heap
  297. reference counts or interacting with garbage collection, as alias lifetimes
  298. are statically guaranteed to outlive callee lifetimes.
  299. Copying data between slots of different modes may cause either a simple
  300. address assignment or reference-count adjustment, or may cause a value to be
  301. ``transplanted'': copied by value from the interior of one memory structure to
  302. another, or between stack and heap. Transplanting, when necessary, is
  303. predictable and automatic, as part of the definition of the copy operator
  304. (@code{=}).
  305. In addition, slots have a static initialization state that is calculated by
  306. the typestate system. This permits late initialization of variables in
  307. functions with complex control-flow, while still guaranteeing that every use
  308. of a slot occurs after it has been initialized.
  309. @sp 1
  310. @item Static control over mutability.
  311. Slots in Rust are classified as either immutable or mutable. By default,
  312. all slots are immutable.
  313. If a slot within a type is declared as @code{mutable}, the type is a
  314. @code{state} type and must be declared as such.
  315. This classification of data types in Rust interacts with the memory allocation
  316. and transmission rules. In particular:
  317. @itemize
  318. @item Only immutable (non-state) values can be sent over channels.
  319. @item Only immutable (non-state) objects can have destructor functions.
  320. @end itemize
  321. State values are subject to local (per-task) garbage-collection. Garbage
  322. collection costs are therefore also task-local and do not interrupt or suspend
  323. other tasks.
  324. Immutable values are reference-counted and have a deterministic destruction
  325. order: top-down, immediately upon release of the last live reference.
  326. State values can refer to immutable values, but not vice-versa. Rust therefore
  327. encourages the programmer to write in a style that consists primarily of
  328. immutable types, but also permits limited, local (per-task) mutability.
  329. @end itemize
  330. @page
  331. @node Influences
  332. @section Influences
  333. @sp 2
  334. @quotation
  335. The essential problem that must be solved in making a fault-tolerant
  336. software system is therefore that of fault-isolation. Different programmers
  337. will write different modules, some modules will be correct, others will have
  338. errors. We do not want the errors in one module to adversely affect the
  339. behaviour of a module which does not have any errors.
  340. @flushright
  341. - Joe Armstrong
  342. @end flushright
  343. @end quotation
  344. @sp 2
  345. @quotation
  346. In our approach, all data is private to some process, and processes can
  347. only communicate through communications channels. @emph{Security}, as used
  348. in this paper, is the property which guarantees that processes in a system
  349. cannot affect each other except by explicit communication.
  350. When security is absent, nothing which can be proven about a single module
  351. in isolation can be guaranteed to hold when that module is embedded in a
  352. system [...]
  353. @flushright
  354. - Robert Strom and Shaula Yemini
  355. @end flushright
  356. @end quotation
  357. @sp 2
  358. @quotation
  359. Concurrent and applicative programming complement each other. The
  360. ability to send messages on channels provides I/O without side effects,
  361. while the avoidance of shared data helps keep concurrent processes from
  362. colliding.
  363. @flushright
  364. - Rob Pike
  365. @end flushright
  366. @end quotation
  367. @sp 2
  368. @page
  369. Rust is not a particularly original language. It may however appear unusual by
  370. contemporary standards, as its design elements are drawn from a number of
  371. ``historical'' languages that have, with a few exceptions, fallen out of
  372. favour. Five prominent lineages contribute the most:
  373. @itemize
  374. @sp 1
  375. @item
  376. The NIL (1981) and Hermes (1990) family. These languages were developed by
  377. Robert Strom, Shaula Yemini, David Bacon and others in their group at IBM
  378. Watson Research Center (Yorktown Heights, NY, USA).
  379. @sp 1
  380. @item
  381. The Erlang (1987) language, developed by Joe Armstrong, Robert Virding, Claes
  382. Wikstr@"om, Mike Williams and others in their group at the Ericsson Computer
  383. Science Laboratory (@"Alvsj@"o, Stockholm, Sweden) .
  384. @sp 1
  385. @item
  386. The Sather (1990) language, developed by Stephen Omohundro, Chu-Cheow Lim,
  387. Heinz Schmidt and others in their group at The International Computer Science
  388. Institute of the University of California, Berkeley (Berkeley, CA, USA).
  389. @sp 1
  390. @item
  391. The Newsqueak (1988), Alef (1995), and Limbo (1996) family. These languages
  392. were developed by Rob Pike, Phil Winterbottom, Sean Dorward and others in
  393. their group at Bell labs Computing Sciences Reserch Center (Murray Hill, NJ,
  394. USA).
  395. @sp 1
  396. @item
  397. The Napier (1985) and Napier88 (1988) family. These languages were developed
  398. by Malcolm Atkinson, Ron Morrison and others in their group at the University
  399. of St. Andrews (St. Andrews, Fife, UK).
  400. @end itemize
  401. @sp 1
  402. Additional specific influences can be seen from the following languages:
  403. @itemize
  404. @item The structural algebraic types and compilation manager of SML.
  405. @item The syntax-extension systems of Camlp4 and the Common Lisp readtable.
  406. @item The deterministic destructor system of C++.
  407. @end itemize
  408. @c ############################################################
  409. @c Tutorial
  410. @c ############################################################
  411. @node Tutorial
  412. @chapter Tutorial
  413. @emph{TODO}.
  414. @c ############################################################
  415. @c Reference
  416. @c ############################################################
  417. @node Reference
  418. @chapter Reference
  419. @menu
  420. * Ref.Lex:: Lexical structure.
  421. * Ref.Path:: References to slots and items.
  422. * Ref.Gram:: Grammar.
  423. * Ref.Comp:: Compilation and component model.
  424. * Ref.Mem:: Semantic model of memory.
  425. * Ref.Task:: Semantic model of tasks.
  426. * Ref.Item:: The components of a module.
  427. * Ref.Type:: The types of values held in memory.
  428. * Ref.Expr:: Parsed and primitive expressions.
  429. * Ref.Stmt:: Executable statements.
  430. * Ref.Run:: Organization of runtime services.
  431. @end menu
  432. @page
  433. @node Ref.Lex
  434. @section Ref.Lex
  435. @c * Ref.Lex:: Lexical structure.
  436. The lexical structure of a Rust source file or crate file is defined in terms
  437. of Unicode character codes and character properties.
  438. Groups of Unicode character codes and characters are organized into
  439. @emph{tokens}. Tokens are defined as the longest contiguous sequence of
  440. characters within the same token type (identifier, keyword, literal, symbol),
  441. or interrupted by ignored characters.
  442. Most tokens in Rust follow rules similar to the C family.
  443. Most tokens (including identifiers, whitespace, keywords, operators and
  444. structural symbols) are drawn from the ASCII-compatible range of
  445. Unicode. String and character literals, however, may include the full range of
  446. Unicode characters.
  447. @emph{TODO: formalize this section much more}.
  448. @menu
  449. * Ref.Lex.Ignore:: Ignored characters.
  450. * Ref.Lex.Ident:: Identifier tokens.
  451. * Ref.Lex.Key:: Keyword tokens.
  452. * Ref.Lex.Num:: Numeric tokens.
  453. * Ref.Lex.Text:: String and character tokens.
  454. * Ref.Lex.Syntax:: Syntactic extension tokens.
  455. * Ref.Lex.Sym:: Special symbol tokens.
  456. @end menu
  457. @page
  458. @node Ref.Lex.Ignore
  459. @subsection Ref.Lex.Ignore
  460. @c * Ref.Lex.Ignore:: Ignored tokens.
  461. The classes of @emph{whitespace} and @emph{comment} is ignored, and are not
  462. considered as tokens.
  463. @dfn{Whitespace} is any of the following Unicode characters: U+0020 (space),
  464. U+0009 (tab, @code{'\t'}), U+000A (LF, @code{'\n'}), U+000D (CR, @code{'\r'}).
  465. @dfn{Comments} are any sequence of Unicode characters beginning with U+002F
  466. U+002F (@code{//}) and extending to the next U+000a character,
  467. @emph{excluding} cases in which such a sequence occurs within a string literal
  468. token or a syntactic extension token.
  469. @page
  470. @node Ref.Lex.Ident
  471. @subsection Ref.Lex.Ident
  472. @c * Ref.Lex.Ident:: Identifier tokens.
  473. Identifiers follow the pattern of C identifiers: they begin with a
  474. @emph{letter} or underscore character @code{_} (Unicode character U+005f), and
  475. continue with any combination of @emph{letters}, @emph{digits} and
  476. underscores, and must not be equal to any keyword. @xref{Ref.Lex.Key}.
  477. A @emph{letter} is a Unicode character in the ranges U+0061-U+007A and
  478. U+0041-U+005A (@code{a-z} and @code{A-Z}).
  479. A @emph{digit} is a Unicode character in the range U+0030-U0039 (@code{0-9}).
  480. @page
  481. @node Ref.Lex.Key
  482. @subsection Ref.Lex.Key
  483. @c * Ref.Lex.Key:: Keyword tokens.
  484. The keywords are:
  485. @sp 2
  486. @multitable @columnfractions .15 .15 .15 .15 .15
  487. @item @code{use}
  488. @tab @code{meta}
  489. @tab @code{syntax}
  490. @tab @code{mutable}
  491. @tab @code{native}
  492. @item @code{mod}
  493. @tab @code{import}
  494. @tab @code{export}
  495. @tab @code{let}
  496. @tab @code{auto}
  497. @item @code{io}
  498. @tab @code{state}
  499. @tab @code{unsafe}
  500. @tab @code{auth}
  501. @tab @code{with}
  502. @item @code{bind}
  503. @tab @code{type}
  504. @tab @code{true}
  505. @tab @code{false}
  506. @item @code{any}
  507. @tab @code{int}
  508. @tab @code{uint}
  509. @tab @code{char}
  510. @tab @code{bool}
  511. @item @code{u8}
  512. @tab @code{u16}
  513. @tab @code{u32}
  514. @tab @code{u64}
  515. @tab @code{f32}
  516. @item @code{i8}
  517. @tab @code{i16}
  518. @tab @code{i32}
  519. @tab @code{i64}
  520. @tab @code{f64}
  521. @item @code{rec}
  522. @tab @code{tup}
  523. @tab @code{tag}
  524. @tab @code{vec}
  525. @tab @code{str}
  526. @item @code{fn}
  527. @tab @code{iter}
  528. @tab @code{obj}
  529. @tab @code{as}
  530. @tab @code{drop}
  531. @item @code{task}
  532. @tab @code{port}
  533. @tab @code{chan}
  534. @tab @code{flush}
  535. @tab @code{spawn}
  536. @item @code{if}
  537. @tab @code{else}
  538. @tab @code{alt}
  539. @tab @code{case}
  540. @tab @code{in}
  541. @item @code{do}
  542. @tab @code{while}
  543. @tab @code{break}
  544. @tab @code{cont}
  545. @tab @code{fail}
  546. @item @code{log}
  547. @tab @code{note}
  548. @tab @code{claim}
  549. @tab @code{check}
  550. @tab @code{prove}
  551. @item @code{for}
  552. @tab @code{each}
  553. @tab @code{ret}
  554. @tab @code{put}
  555. @tab @code{be}
  556. @end multitable
  557. @page
  558. @node Ref.Lex.Num
  559. @subsection Ref.Lex.Num
  560. @c * Ref.Lex.Num:: Numeric tokens.
  561. @emph{TODO: describe numeric literals}.
  562. @page
  563. @node Ref.Lex.Text
  564. @subsection Ref.Lex.Text
  565. @c * Ref.Lex.Key:: String and character tokens.
  566. @emph{TODO: describe string and character literals}.
  567. @page
  568. @node Ref.Lex.Syntax
  569. @subsection Ref.Lex.Syntax
  570. @c * Ref.Lex.Syntax:: Syntactic extension tokens.
  571. Syntactic extensions are marked with the @emph{pound} sigil @code{#} (U+0023),
  572. followed by a qualified name of a compile-time imported module item, an
  573. optional parenthesized list of @emph{tokens}, and an optional brace-enclosed
  574. region of free-form text (with brace-matching and brace-escaping used to
  575. determine the limit of the region). @xref{Ref.Comp.Syntax}.
  576. @emph{TODO: formalize those terms more}.
  577. @page
  578. @node Ref.Lex.Sym
  579. @subsection Ref.Lex.Sym
  580. @c * Ref.Lex.Sym:: Special symbol tokens.
  581. The special symbols are:
  582. @sp 2
  583. @multitable @columnfractions .1 .1 .1 .1 .1 .1
  584. @item @code{@@}
  585. @tab @code{_}
  586. @item @code{#}
  587. @tab @code{:}
  588. @tab @code{.}
  589. @tab @code{;}
  590. @tab @code{,}
  591. @item @code{[}
  592. @tab @code{]}
  593. @tab @code{@{}
  594. @tab @code{@}}
  595. @tab @code{(}
  596. @tab @code{)}
  597. @item @code{=}
  598. @tab @code{<-}
  599. @tab @code{<|}
  600. @tab @code{<+}
  601. @tab @code{->}
  602. @item @code{+}
  603. @tab @code{++}
  604. @tab @code{+=}
  605. @tab @code{-}
  606. @tab @code{--}
  607. @tab @code{-=}
  608. @item @code{*}
  609. @tab @code{/}
  610. @tab @code{%}
  611. @tab @code{*=}
  612. @tab @code{/=}
  613. @tab @code{%=}
  614. @item @code{&}
  615. @tab @code{|}
  616. @tab @code{!}
  617. @tab @code{~}
  618. @tab @code{^}
  619. @item @code{&=}
  620. @tab @code{|=}
  621. @tab @code{^=}
  622. @tab @code{!=}
  623. @item @code{>>}
  624. @tab @code{>>>}
  625. @tab @code{<<}
  626. @tab @code{<<=}
  627. @tab @code{>>=}
  628. @tab @code{>>>=}
  629. @item @code{<}
  630. @tab @code{<=}
  631. @tab @code{==}
  632. @tab @code{>=}
  633. @tab @code{>}
  634. @item @code{&&}
  635. @tab @code{||}
  636. @end multitable
  637. @page
  638. @page
  639. @node Ref.Path
  640. @section Ref.Path
  641. @c * Ref.Path:: References to slots and items.
  642. A @dfn{path} is a ubiquitous syntactic form in Rust that deserves special
  643. attention. A path denotes a slot or an
  644. item. @xref{Ref.Mem.Slot}. @xref{Ref.Item}. Every slot and item in a Rust
  645. crate has a @emph{canonical path} that refers to it from the crate top-level,
  646. as well as a number of shorter @emph{relative paths} that may also denote it
  647. in inner scopes of the crate. There is no way to define a slot or item without
  648. a canonical path within its crate (with the exception of the crate's implicit
  649. top-level module). Paths have meaning only within a specific
  650. crate. @xref{Ref.Comp.Crate}.
  651. Paths consist of period-separated components. In the simplest form, path
  652. components are identifiers. @xref{Ref.Lex.Ident}.
  653. Two examples of simple paths consisting of only identifier components:
  654. @example
  655. x;
  656. x.y.z;
  657. @end example
  658. Paths fall into two important categories: @emph{names} and
  659. @emph{lvals}.
  660. A @dfn{name} denotes an item, and is statically resolved to its
  661. referent at compile time.
  662. An @dfn{lval} denotes a slot, and is statically resolved to a sequence of
  663. memory operations and primitive (arithmetic) expressions required to load or
  664. store to the slot at compile time.
  665. In some contexts, the Rust grammar accepts a general @emph{path}, but a
  666. subsequent syntactic restriction requires the path to be an lval or a name. In
  667. other words: in some contexts an lval is required (for example, on the left
  668. hand side of the copy operator, @pxref{Ref.Stmt.Copy}) and in other contexts a
  669. name is required (for example, as a type parameter, @pxref{Ref.Item}). In no
  670. case is the grammar made ambiguous by accepting a general path and restricting
  671. allowed paths to names or lvals after parsing. These restrictions are noted in
  672. the grammar. @xref{Ref.Gram}.
  673. A name component may include type parameters. Type parameters are denoted by
  674. square brackets. Square brackets are used @emph{only} to denote type
  675. parameters in Rust. If a name component includes a type parameter, the type
  676. parameter must also resolve statically to a type in the environment of the
  677. name. Type parameters are only part of the names of items. @xref{Ref.Item}.
  678. An example of a name with type parameters:
  679. @example
  680. m.map[int,str];
  681. @end example
  682. An lval component may include an indexing operator. Index operators are
  683. enclosed in parentheses and can include any integral expression. Indexing
  684. operators can only be applied to vectors or strings, and imply a run-time
  685. bounds-check. @xref{Ref.Type.Vec}.
  686. An example of an lval with a dynamic indexing operator:
  687. @example
  688. x.y.(1 + v).z;
  689. @end example
  690. @page
  691. @node Ref.Gram
  692. @section Ref.Gram
  693. @c * Ref.Gram:: Grammar.
  694. @emph{TODO: LL(1), it reads like C, Alef and bits of Napier; formalize here}.
  695. @page
  696. @node Ref.Comp
  697. @section Ref.Comp
  698. @c * Ref.Comp:: Compilation and component model.
  699. Rust is a @emph{compiled} language. Its semantics are divided along a
  700. @emph{phase distinction} between compile-time and run-time. Those semantic
  701. rules that have a @emph{static interpretation} govern the success or failure
  702. of compilation. A program that fails to compile due to violation of a
  703. compile-time rule has no defined semantics at run-time; the compiler should
  704. halt with an error report, and produce no executable artifact.
  705. The compilation model centres on artifacts called @emph{crates}. Each
  706. compilation is directed towards a single crate in source form, and if
  707. successful produces a single crate in executable form.
  708. @menu
  709. * Ref.Comp.Crate:: Units of compilation and linking.
  710. * Ref.Comp.Meta:: Metadata about a crate.
  711. * Ref.Comp.Syntax:: Syntax extensions.
  712. @end menu
  713. @page
  714. @node Ref.Comp.Crate
  715. @subsection Ref.Comp.Crate
  716. @c * Ref.Comp.Crate:: Units of compilation and linking.
  717. A @dfn{crate} is a unit of compilation and linking, as well as versioning,
  718. distribution and runtime loading. Crates are defined by @emph{crate source
  719. files}, which are a type of source file written in a special declarative
  720. language: @emph{crate language}.@footnote{A crate is somewhat analogous to an
  721. @emph{assembly} in the ECMA-335 CLI model, a @emph{library} in the SML/NJ
  722. Compilation Manager, a @emph{unit} in the Owens and Flatt module system, or a
  723. @emph{configuration} in Mesa.} A crate source file describes:
  724. @itemize
  725. @item Metadata about the crate, such as author, name, version, and copyright.
  726. @item The source-file and directory modules that make up the crate.
  727. @item The set of syntax extensions to enable for the crate.
  728. @item Any external crates or native modules that the crate imports to its top level.
  729. @item The organization of the crate's internal namespace.
  730. @item The set of names exported from the crate.
  731. @end itemize
  732. A single crate source file may describe the compilation of a large number of
  733. Rust source files; it is compiled in its entirety, as a single indivisible
  734. unit. The compilation phase attempts to transform a single crate source file,
  735. and its referenced contents, into a single compiled crate. Crate source files
  736. and compiled crates have a 1:1 relationship.
  737. The syntactic form of a crate is a sequence of @emph{directives}, some of
  738. which have nested sub-directives.
  739. A crate defines an implicit top-level anonymous module: within this module,
  740. all members of the crate have canonical path names. @xref{Ref.Path}. The
  741. @code{mod} directives within a crate file specify sub-modules to include in
  742. the crate: these are either directory modules, corresponding to directories in
  743. the filesystem of the compilation environment, or file modules, corresponding
  744. to Rust source files. The names given to such modules in @code{mod} directives
  745. become prefixes of the paths of items and slots defined within any included
  746. Rust source files.
  747. The @code{use} directives within the crate specify @emph{other crates} to scan
  748. for, locate, import into the crate's module namespace during compilation, and
  749. link against at runtime. Use directives may also occur independently in rust
  750. source files. These directives may specify loose or tight ``matching
  751. criteria'' for imported crates, depending on the preferences of the crate
  752. developer. In the simplest case, a @code{use} directive may only specify a
  753. symbolic name and leave the task of locating and binding an appropriate crate
  754. to a compile-time heuristic. In a more controlled case, a @code{use} directive
  755. may specify any metadata as matching criteria, such as a URI, an author name
  756. or version number, a checksum or even a cryptographic signature, in order to
  757. select an an appropriate imported crate. @xref{Ref.Comp.Meta}.
  758. The compiled form of a crate is a loadable and executable object file full of
  759. machine code, in a standard loadable operating-system format such as ELF, PE
  760. or Mach-O. The loadable object contains extensive DWARF metadata, describing:
  761. @itemize
  762. @item Metadata required for type reflection.
  763. @item The publicly exported module structure of the crate.
  764. @item Any metadata about the crate, defined by @code{meta} directives.
  765. @item The crates to dynamically link with at run-time, with matching criteria
  766. derived from the same @code{use} directives that guided compile-time imports.
  767. @end itemize
  768. The @code{syntax} directives of a crate are similar to the @code{use}
  769. directives, except they govern the syntax extension namespace (accessed
  770. through the syntax-extension sigil @code{#}, @pxref{Ref.Comp.Syntax})
  771. available only at compile time. A @code{syntax} directive also makes its
  772. extension available to all subsequent directives in the crate file.
  773. An example of a crate:
  774. @example
  775. // Metadata about this crate
  776. meta (author = "Jane Doe",
  777. name = "projx"
  778. desc = "Project X",
  779. ver = "2.5");
  780. // Import a module.
  781. use std (ver = "1.0");
  782. // Activate a syntax-extension.
  783. syntax re;
  784. // Define some modules.
  785. mod foo = "foo.rs";
  786. mod bar @{
  787. mod quux = "quux.rs";
  788. @}
  789. @end example
  790. @page
  791. @node Ref.Comp.Meta
  792. @subsection Ref.Comp.Meta
  793. In a crate, a @code{meta} directive associates free form key-value metadata
  794. with the crate. This metadata can, in turn, be used in providing partial
  795. matching parameters to syntax-extension loading and crate importing
  796. directives, denoted by @code{syntax} and @code{use} keywords respectively.
  797. Alternatively, metadata can serve as a simple form of documentation.
  798. @page
  799. @node Ref.Comp.Syntax
  800. @subsection Ref.Comp.Syntax
  801. @c * Ref.Comp.Syntax:: Syntax extension.
  802. Rust provides a notation for @dfn{syntax extension}. The notation is a marked
  803. syntactic form that can appear as an expression, statement or item in the body
  804. of a Rust program, or as a directive in a Rust crate, and which causes the
  805. text enclosed within the marked form to be translated through a named
  806. extension function loaded into the compiler at compile-time.
  807. The compile-time extension function must return a value of the corresponding
  808. Rust AST type, either an expression node, a statement node or an item
  809. node. @footnote{The syntax-extension system is analogous to the extensible
  810. reader system provided by Lisp @emph{readtables}, or the Camlp4 system of
  811. Objective Caml.} @xref{Ref.Lex.Syntax}.
  812. A syntax extension is enabled by a @code{syntax} directive, which must occur
  813. in a crate file. When the Rust compiler encounters a @code{syntax} directive
  814. in a crate file, it immediately loads the named syntax extension, and makes it
  815. available for all subsequent crate directives within the enclosing block scope
  816. of the crate file, and all Rust source files referenced as modules from the
  817. enclosing block scope of the crate file.
  818. For example, this extension might provide a syntax for regular
  819. expression literals:
  820. @example
  821. // In a crate file:
  822. // Requests the 're' syntax extension from the compilation environment.
  823. syntax re;
  824. // Also declares an import dependency on the module 're'.
  825. use re;
  826. // Reference to a Rust source file as a module in the crate.
  827. mod foo = "foo.rs";
  828. @dots{}
  829. // In the source file "foo.rs", use the #re syntax extension and
  830. // the re module at run-time.
  831. let str s = get_string();
  832. let regex pattern = #re.pat@{ aa+b? @};
  833. let bool matched = re.match(pattern, s);
  834. @end example
  835. @page
  836. @node Ref.Mem
  837. @section Ref.Mem
  838. @c * Ref.Mem:: Semantic model of memory.
  839. A Rust task's memory consists of a static set of @emph{items}, a set of tasks
  840. each with its own @emph{stack}, and a @emph{heap}. Immutable portions of the
  841. heap may be shared between tasks, mutable portions may not.
  842. Allocations in the stack and the heap consist of @emph{slots}.
  843. @menu
  844. * Ref.Mem.Alloc:: Memory allocation model.
  845. * Ref.Mem.Own:: Memory ownership model.
  846. * Ref.Mem.Slot:: Memory containment and reference model.
  847. * Ref.Mem.Init:: Initialization state of memory.
  848. * Ref.Mem.Acct:: Memory accounting model.
  849. @end menu
  850. @page
  851. @node Ref.Mem.Alloc
  852. @subsection Ref.Mem.Alloc
  853. @c * Ref.Mem.Alloc:: Memory allocation model.
  854. The @dfn{items} of a program are those functions, iterators, objects, modules
  855. and types that have their value calculated at compile-time and stored uniquely
  856. in the memory image of the rust process. Items are neither dynamically
  857. allocated nor freed.
  858. A task's @dfn{stack} consists of activation frames automatically allocated on
  859. entry to each function as the task executes. A stack allocation is reclaimed
  860. when control leaves the frame containing it.
  861. The @dfn{heap} is a general term that describes two separate sets of exterior
  862. allocations: @emph{local heap} allocations and the @emph{shared heap}
  863. allocations.
  864. Exterior allocations of mutable types are @dfn{local heap} allocations,
  865. owned by the task. Such @dfn{local allocations} cannot pass over channels and
  866. do not outlive the task that owns them. When unreferenced, they are collected
  867. using a general (cycle-aware) garbage-collector local to each task. Garbage
  868. collection within a local heap does not interrupt execution of other tasks.
  869. Exterior allocations of immutable types are @dfn{shared heap} allocations,
  870. and can be multiply-referenced by many different tasks. Such @dfn{shared
  871. allocations} can pass over channels, and live as long as the last task
  872. referencing them. When unreferenced, they are collected immediately using
  873. reference-counting.
  874. @page
  875. @node Ref.Mem.Own
  876. @subsection Ref.Mem.Own
  877. @c * Ref.Mem.Own:: Memory ownership model.
  878. A task @emph{owns} all the interior allocations in its stack and @emph{local}
  879. exterior allocations. A task @emph{shares} ownership of @emph{shared} exterior
  880. allocations. A task does not own any items.
  881. @dfn{Ownership} of an allocation means that the owning task is the only task
  882. that can access the allocation.
  883. @dfn{Sharing} of an allocation means that the same allocation may be
  884. concurrently referenced by multiple tasks. The only shared allocations are
  885. those that are immutable.
  886. When a stack frame is exited, its interior allocations are all released, and
  887. its references to heap allocations (both shared and owned) are dropped.
  888. When a task finishes, its stack is necessarily empty. The task's interior
  889. slots are released as the task itself is released, and its references to heap
  890. allocations are dropped.
  891. @page
  892. @node Ref.Mem.Slot
  893. @subsection Ref.Mem.Slot
  894. @c * Ref.Mem.Slot:: Memory containment and reference model.
  895. A @dfn{slot} is a component of an allocation. A slot either holds a value or
  896. the address of another allocation. Every slot has one of three possible
  897. @emph{modes}.
  898. The possible @dfn{modes} of a slot are:
  899. @itemize
  900. @sp 1
  901. @item @dfn{Interior mode}
  902. The slot holds the value of the slot.
  903. @sp 1
  904. @item @dfn{Exterior mode}
  905. The slot holds the address of a heap allocation that holds the value of the
  906. slot.
  907. Exterior slots are indicated by the @emph{at} sigil @code{@@}.
  908. For example, the following code allocates an exterior record, copies it by
  909. counted-reference to a second exterior slot, then modifies the record through
  910. the second exterior slot that points to the same exterior allocation.
  911. @example
  912. type point3d = rec(int x, int y, int z);
  913. let @@point3d pt1 = rec(x=1, y=2, z=3);
  914. let @@point3d pt2 = pt1;
  915. pt2.z = 4;
  916. @end example
  917. @sp 1
  918. @item @dfn{Alias mode}
  919. The slot holds the address of a value. The referenced value may reside within
  920. a stack allocation @emph{or} a heap allocation.
  921. Alias slots can @emph{only} be declared as members of a function or iterator
  922. signature, bound to the lifetime of a stack frame. Alias slots cannot be
  923. declared as members of general data types.
  924. Alias slots are indicated by the @emph{ampersand} sigil @code{&}.
  925. The following example function accepts a single read-only alias parameter:
  926. @example
  927. type point3d = rec(int x, int y, int z);
  928. fn extract_z(&point3d p) -> int @{
  929. ret p.z;
  930. @}
  931. @end example
  932. The following example function accepts a single mutable alias
  933. parameter:
  934. @example
  935. fn incr(mutable &int i) @{
  936. i = i + 1;
  937. @}
  938. @end example
  939. @end itemize
  940. @page
  941. @node Ref.Mem.Init
  942. @subsection Ref.Mem.Init
  943. @c * Ref.Mem.Init:: Initialization state of memory.
  944. A slot is either initialized or uninitialized at every point in a program. An
  945. @dfn{initialized} slot is one that holds a value. An @dfn{uninitialized} slot
  946. is one that has not yet had a value written into it, or has had its value
  947. deleted, and so holds undefined memory. The typestate system ensures that an
  948. uninitialized slot cannot be read, but can be written to. A slot becomes
  949. initialized in any statement that writes to it, and remains initialized until
  950. explicitly destroyed or until its enclosing allocation is destroyed.
  951. @page
  952. @node Ref.Mem.Acct
  953. @subsection Ref.Mem.Acct
  954. @c * Ref.Mem.Acct:: Memory accounting model.
  955. Every task belongs to a domain, and that domain tracks the amount of memory
  956. allocated and not yet released by tasks within it. @xref{Ref.Task.Dom}. Each
  957. domain has a memory budget. The @dfn{budget} of a domain is the maximum amount
  958. of memory that can be simultaneously allocated in the domain. If a task tries
  959. to allocate memory within a domain with an exceeded budget, the task will
  960. receive a signal.
  961. Within a task, accounting is strictly enforced: all memory allocated through
  962. the runtime library, both user data, sub-domains and runtime-support
  963. structures such as channel and signal queues, are charged to a task's domain.
  964. When a communication channel crosses from one domain to another, any value
  965. sent over the channel is guaranteed to have been @emph{detached} from the
  966. domain's memory graph (singly referenced, and/or deep-copied), so its memory
  967. cost is transferred to the receiving domain.
  968. @page
  969. @node Ref.Task
  970. @section Ref.Task
  971. @c * Ref.Task:: Semantic model of tasks.
  972. A executing Rust program consists of a tree of tasks. A Rust @dfn{task}
  973. consists of an entry function, a stack, a set of outgoing communication
  974. channels and incoming communication ports, and ownership of some portion of
  975. the heap of a single operating-system process.
  976. Multiple Rust tasks may coexist in a single operating-system
  977. process. Execution of multiple Rust tasks in a single operating-system process
  978. may be either truly concurrent or interleaved by the runtime scheduler. Rust
  979. tasks are lightweight: each consumes less memory than an operating-system
  980. process, and switching between Rust tasks is faster than switching between
  981. operating-system processes.
  982. @menu
  983. * Ref.Task.Comm:: Inter-task communication.
  984. * Ref.Task.Life:: Task lifecycle and state transitions.
  985. * Ref.Task.Dom:: Task domains.
  986. * Ref.Task.Sched:: Task scheduling model.
  987. @end menu
  988. @page
  989. @node Ref.Task.Comm
  990. @subsection Ref.Task.Comm
  991. @c * Ref.Task.Comm:: Inter-task communication.
  992. With the exception of @emph{unsafe} constructs, Rust tasks are isolated from
  993. interfering with one another's memory directly. Instead of manipulating shared
  994. storage, Rust tasks communicate with one another using a typed, asynchronous,
  995. simplex message-passing system.
  996. A @dfn{port} is a communication endpoint that can @emph{receive}
  997. messages. Ports receive messages from channels.
  998. A @dfn{channel} is a communication endpoint that can @emph{send}
  999. messages. Channels send messages to ports.
  1000. Each port has a unique identity and cannot be replicated. If a port value is
  1001. copied from one slot to another, both slots refer to the @emph{same} port,
  1002. even if the slots are declared as interior-mode. New ports can be constructed
  1003. dynamically and stored in data structures.
  1004. Each channel is bound to a port when the channel is constructed, so the
  1005. destination port for a channel must exist before the channel itself. A channel
  1006. cannot be rebound to a different port from the one it was constructed with.
  1007. Many channels can be bound to the same port, but each channel is bound to a
  1008. single port. In other words, channels and ports exist in an N:1 relationship,
  1009. N channels to 1 port. @footnote{It may help to remember nautical terminology
  1010. when differentiating channels from ports. Many different waterways --
  1011. channels -- may lead to the same port.}
  1012. Each port and channel can carry only one type of message. The message type is
  1013. encoded as a parameter of the channel or port type. The message type of a
  1014. channel is equal to the message type of the port it is bound to.
  1015. Messages are sent asynchronously or semi-synchronously. A channel contains a
  1016. message queue and asynchronously sending a message merely inserts it into the
  1017. channel's queue; message receipt is the responsibility of the receiving task.
  1018. Queued messages in channels are charged to the domain of the @emph{sending}
  1019. task. If too many messages are queued for transmission from a single sending
  1020. task, without being received by a receiving task, the sending task may exceed
  1021. its memory budget, which causes a run-time signal. To help control this
  1022. possibility, a semi-synchronous send operation is possible, which blocks until
  1023. there is room in the existing queue and then executes an asynchronous send. A
  1024. full @code{flush} operation is also available, which blocks until a channel's
  1025. queue is @emph{empty}. A @code{flush} does @emph{not} guarantee that a message
  1026. has been @emph{received} by any particular recipient when the sending task is
  1027. unblocked. @xref{Ref.Stmt.Flush}.
  1028. The asynchronous message-send operator is @code{<+}. The semi-synchronous
  1029. message-send operator is @code{<|}. @xref{Ref.Stmt.Send}. The message-receive
  1030. operator is @code{<-}. @xref{Ref.Stmt.Recv}.
  1031. @page
  1032. @node Ref.Task.Life
  1033. @subsection Ref.Task.Life
  1034. @c * Ref.Task.Life:: Task lifecycle and state transitions.
  1035. The @dfn{lifecycle} of a task consists of a finite set of states and events
  1036. that cause transitions between the states. The lifecycle states of a task are:
  1037. @itemize
  1038. @item running
  1039. @item blocked
  1040. @item failing
  1041. @item dead
  1042. @end itemize
  1043. A task begins its lifecycle -- once it has been spawned -- in the
  1044. @emph{running} state. In this state it executes the statements of its entry
  1045. function, and any functions called by the entry function.
  1046. A task may transition from the @emph{running} state to the @emph{blocked}
  1047. state any time it executes a communication statement on a port or channel that
  1048. cannot be immediately completed. When the communication statement can be
  1049. completed -- when a message arrives at a sender, or a queue drains
  1050. sufficiently to complete a semi-synchronous send -- then the blocked task will
  1051. unblock and transition back to @emph{running}.
  1052. A task may transition to the @emph{failing} state at any time, due to an
  1053. un-trapped signal or the execution of a @code{fail} statement. Once
  1054. @emph{failing}, a task unwinds its stack and transitions to the @emph{dead}
  1055. state. Unwinding the stack of a task is done by the task itself, on its own
  1056. control stack. If a value with a destructor is freed during unwinding, the
  1057. code for the destructor is run, also on the task's control stack. If the
  1058. destructor code causes any subsequent state transitions, the task of unwinding
  1059. and failing may suspend temporarily, and may involve (recursive) unwinding of
  1060. the stack of a failed destructor. Nonetheless, the outermost unwinding
  1061. activity will continue until the stack is unwound and the task transitions to
  1062. the @emph{dead} state. There is no way to ``recover'' from task failure.
  1063. A task in the @emph{dead} state cannot transition to other states; it exists
  1064. only to have its termination status inspected by other tasks, and/or to await
  1065. reclamation when the last reference to it drops.
  1066. @page
  1067. @node Ref.Task.Dom
  1068. @subsection Ref.Task.Dom
  1069. @c * Ref.Task.Dom:: Task domains
  1070. Every task belongs to a domain. A @dfn{domain} is a structure that owns tasks,
  1071. schedules tasks, tracks memory allocation within tasks and manages access to
  1072. runtime services on behalf of tasks.
  1073. Typically each domain runs on a separate operating-system @emph{thread}, or
  1074. within an isolated operating-system @emph{process}. An easy way to think of a
  1075. domain is as an abstraction over either an operating-system thread @emph{or} a
  1076. process.
  1077. The key feature of a domain is that it isolates memory references created by
  1078. the Rust tasks within it. No Rust task can refer directly to memory outside
  1079. its domain.
  1080. Tasks can own sub-domains, which in turn own their own tasks. Every domain
  1081. owns one @emph{root task}, which is the root of the tree of tasks owned by the
  1082. domain.
  1083. @page
  1084. @node Ref.Task.Sched
  1085. @subsection Ref.Task.Sched
  1086. @c * Ref.Task.Sched:: Task scheduling model.
  1087. Every task is @emph{scheduled} within its domain. @xref{Ref.Task.Dom}. The
  1088. currently scheduled task is given a finite @emph{time slice} in which to
  1089. execute, after which it is @emph{descheduled} at a loop-edge or similar
  1090. preemption point, and another task within the domain is scheduled,
  1091. pseudo-randomly.
  1092. An executing task can @code{yield} control at any time, which deschedules it
  1093. immediately. Entering any other non-executing state (blocked, dead) similarly
  1094. deschedules the task.
  1095. @page
  1096. @node Ref.Item
  1097. @section Ref.Item
  1098. @c * Ref.Item:: The components of a module.
  1099. An @dfn{item} is a component of a module. Items are entirely determined at
  1100. compile-time, remain constant during execution, and may reside in read-only
  1101. memory.
  1102. There are 5 primary kinds of item: modules, functions, iterators, objects and
  1103. types.
  1104. All items form an implicit scope for the declaration of sub-items. In other
  1105. words, within a function, object or iterator, declarations of items can (in
  1106. many cases) be mixed with the statements, control blocks, and similar
  1107. artifacts that otherwise compose the item body. The meaning of these scoped
  1108. items is the same as if the item was declared outside the scope, except that
  1109. the item's @emph{path name} within the module namespace is qualified by the
  1110. name of the enclosing item. The exact locations in which sub-items may be
  1111. declared is given by the grammar. @xref{Ref.Gram}.
  1112. Functions, iterators, objects and types may be @emph{parametrized} by
  1113. type. Type parameters are given as a comma-separated list of identifiers
  1114. enclosed in square brackets (@code{[]}), after the name of the item and before
  1115. its definition. The type parameters of an item are part of the name, not the
  1116. type of the item; in order to refer to the type-parametrized item, a
  1117. referencing name must in general provide type arguments as a list of
  1118. comma-separated types enclosed within square brackets (though the
  1119. type-inference system can often infer such argument types from context). There
  1120. are no general parametric types.
  1121. @menu
  1122. * Ref.Item.Mod:: Items defining modules.
  1123. * Ref.Item.Fn:: Items defining functions.
  1124. * Ref.Item.Iter:: Items defining iterators.
  1125. * Ref.Item.Obj:: Items defining objects.
  1126. * Ref.Item.Type:: Items defining the types of values and slots.
  1127. @end menu
  1128. @page
  1129. @node Ref.Item.Mod
  1130. @subsection Ref.Item.Mod
  1131. @c * Ref.Item.Mod:: Items defining sub-modules.
  1132. A @dfn{module item} contains declarations of other @emph{items}. The items
  1133. within a module may be functions, modules, objects or types. These
  1134. declarations have both static and dynamic interpretation. The purpose of a
  1135. module is to organize @emph{names} and control @emph{visibility}. Modules are
  1136. declared with the keyword @code{mod}.
  1137. An example of a module:
  1138. @example
  1139. mod math @{
  1140. type complex = (f64,f64);
  1141. fn sin(f64) -> f64 @{
  1142. @dots{}
  1143. @}
  1144. fn cos(f64) -> f64 @{
  1145. @dots{}
  1146. @}
  1147. fn tan(f64) -> f64 @{
  1148. @dots{}
  1149. @}
  1150. @dots{}
  1151. @}
  1152. @end example
  1153. Modules may also include any number of @dfn{import and export
  1154. declarations}. These declarations must precede any module item declarations
  1155. within the module, and control the visibility of names both within the module
  1156. and outside of it.
  1157. @menu
  1158. * Ref.Item.Mod.Import:: Declarations for module-local synonyms.
  1159. * Ref.Item.Mod.Export:: Declarations for restricting visibility.
  1160. @end menu
  1161. @page
  1162. @node Ref.Item.Mod.Import
  1163. @subsubsection Ref.Item.Mod.Import
  1164. @c * Ref.Item.Mod.Import:: Declarations for module-local synonyms.
  1165. An @dfn{import declaration} creates one or more local name bindings synonymous
  1166. with some other name. Usually an import declaration is used to shorten the
  1167. path required to refer to a module item.
  1168. @emph{Note}: unlike many languages, Rust's @code{import} declarations do
  1169. @emph{not} declare linkage-dependency with external crates. Linkage
  1170. dependencies are independently declared with @code{use}
  1171. declarations. @xref{Ref.Comp.Crate}.
  1172. An example of an import:
  1173. @example
  1174. import std.math.sin;
  1175. fn main() @{
  1176. // Equivalent to 'log std.math.sin(1.0);'
  1177. log sin(1.0);
  1178. @}
  1179. @end example
  1180. @page
  1181. @node Ref.Item.Mod.Export
  1182. @subsubsection Ref.Item.Mod.Export
  1183. @c * Ref.Item.Mod.Import:: Declarations for restricting visibility.
  1184. An @dfn{export declaration} restricts the set of local declarations within a
  1185. module that can be accessed from code outside the module. By default, all
  1186. local declarations in a module are exported. If a module contains an export
  1187. declaration, this declaration replaces the default export with the export
  1188. specified.
  1189. An example of an export:
  1190. @example
  1191. mod foo @{
  1192. export primary;
  1193. fn primary() @{
  1194. helper(1, 2);
  1195. helper(3, 4);
  1196. @}
  1197. fn helper(int x, int y) @{
  1198. @dots{}
  1199. @}
  1200. @}
  1201. fn main() @{
  1202. foo.primary(); // Will compile.
  1203. foo.helper(2,3) // ERROR: will not compile.
  1204. @}
  1205. @end example
  1206. @page
  1207. @node Ref.Item.Fn
  1208. @subsection Ref.Item.Fn
  1209. @c * Ref.Item.Fn:: Items defining functions.
  1210. A @dfn{function item} defines a sequence of statements associated with a name
  1211. and a set of parameters. Functions are declared with the keyword
  1212. @code{fn}. Functions declare a set of @emph{input slots} as parameters,
  1213. through which the caller passes arguments into the function, and an
  1214. @emph{output slot} through which the function passes results back to the
  1215. caller.
  1216. A function may also be copied into a first class @emph{value}, in which case
  1217. the value has the corresponding @emph{function type}, and can be used
  1218. otherwise exactly as a function item (with a minor additional cost of calling
  1219. the function, as such a call is indirect). @xref{Ref.Type.Fn}.
  1220. Every control path in a function ends with either a @code{ret} or @code{be}
  1221. statement. If a control path lacks a @code{ret} statement in source code, an
  1222. implicit @code{ret} statement is appended to the end of the control path
  1223. during compilation, returning the implicit @code{()} value.
  1224. A function may have an @emph{effect}, which may be either @code{io},
  1225. @code{state}, @code{unsafe}. If no effect is specified, the function is said
  1226. to be @dfn{pure}.
  1227. Any pure boolean function is also called a @emph{predicate}, and may be used
  1228. as part of the static typestate system. @xref{Ref.Stmt.Stat.Constr}.
  1229. An example of a function:
  1230. @example
  1231. fn add(int x, int y) -> int @{
  1232. ret x + y;
  1233. @}
  1234. @end example
  1235. @page
  1236. @node Ref.Item.Iter
  1237. @subsection Ref.Item.Iter
  1238. @c * Ref.Item.Iter:: Items defining iterators.
  1239. Iterators are function-like items that can @code{put} multiple values during
  1240. their execution before returning or tail-calling.
  1241. Putting a value is similar to returning a value -- the argument to @code{put}
  1242. is copied into the caller's frame and control transfers back to the caller --
  1243. but the iterator frame is only @emph{suspended} during the put, and will be
  1244. @emph{resumed} at the statement after the @code{put}, on the next iteration of
  1245. the caller's loop.
  1246. The output type of an iterator is the type of value that the function will
  1247. @code{put}, before it eventually executes a @code{ret} or @code{be} statement
  1248. of type @code{()} and completes its execution.
  1249. An iterator can only be called in the loop header of a matching @code{for
  1250. each} loop or as the argument in a @code{put each} statement.
  1251. @xref{Ref.Stmt.Foreach}.
  1252. An example of an iterator:
  1253. @example
  1254. iter range(int lo, int hi) -> int @{
  1255. let int i = lo;
  1256. while (i < hi) @{
  1257. put i;
  1258. i = i + 1;
  1259. @}
  1260. @}
  1261. let int sum = 0;
  1262. for each (int x = range(0,100)) @{
  1263. sum += x;
  1264. @}
  1265. @end example
  1266. @page
  1267. @node Ref.Item.Obj
  1268. @subsection Ref.Item.Obj
  1269. @c * Ref.Item.Obj:: Items defining objects.
  1270. An @dfn{object item} defines the @emph{state} and @emph{methods} of a set of
  1271. @emph{object values}. Object values have object types. @xref{Ref.Type.Obj}.
  1272. An @emph{object item} declaration -- in addition to providing a scope for
  1273. state and method declarations -- implicitly declares a static function called
  1274. the @emph{object constructor}, as well as a named @emph{object type}. The name
  1275. given to the object item is resolved to a type when used in type context, or a
  1276. constructor function when used in value context (such as a call).
  1277. Example of an object item:
  1278. @example
  1279. obj counter(int state) @{
  1280. fn incr() @{
  1281. state += 1;
  1282. @}
  1283. fn get() -> int @{
  1284. ret state;
  1285. @}
  1286. @}
  1287. let counter c = counter(1);
  1288. c.incr();
  1289. c.incr();
  1290. check (c.get() == 3);
  1291. @end example
  1292. @page
  1293. @node Ref.Item.Type
  1294. @subsection Ref.Item.Type
  1295. @c * Ref.Item.Type:: Items defining the types of values and slots.
  1296. A @dfn{type} defines an @emph{interpretation} of a value in
  1297. memory. @xref{Ref.Type}. Types are declared with the keyword @code{type}. A
  1298. type's interpretation is used for the values held in any slot with that
  1299. type. @xref{Ref.Mem.Slot}. The interpretation of a value includes:
  1300. @itemize
  1301. @item Whether the value is composed of sub-values or is indivisible.
  1302. @item Whether the value represents textual or numerical information.
  1303. @item Whether the value represents integral or floating-point information.
  1304. @item The sequence of memory operations required to access the value.
  1305. @item Whether the value is mutable or immutable.
  1306. @end itemize
  1307. For example, the type @code{rec(u8 x, u8 y)} defines the
  1308. interpretation of values that are composite records, each containing
  1309. two unsigned two's complement 8-bit integers accessed through the
  1310. components @code{x} and @code{y}, and laid out in memory with the
  1311. @code{x} component preceding the @code{y} component.
  1312. Some types are @emph{recursive}. A recursive type is one that includes
  1313. its own definition as a component, by named reference. Recursive types
  1314. are restricted to occur only within a single crate, and only through a
  1315. restricted form of @code{tag} type. @xref{Ref.Type.Tag}.
  1316. @page
  1317. @node Ref.Type
  1318. @section Ref.Type
  1319. Every slot and value in a Rust program has a type. The @dfn{type} of a
  1320. @emph{value} defines the interpretation of the memory holding it. The type of
  1321. a @emph{slot} may also include constraints. @xref{Ref.Type.Constr}.
  1322. Built-in types and type-constructors are tightly integrated into the language,
  1323. in nontrivial ways that are not possible to emulate in user-defined
  1324. types. User-defined types have limited capabilities. In addition, every
  1325. built-in type or type-constructor name is reserved as a @emph{keyword} in
  1326. Rust; they cannot be used as user-defined identifiers in any context.
  1327. @menu
  1328. * Ref.Type.Any:: An open sum of every possible type.
  1329. * Ref.Type.Mach:: Machine-level types.
  1330. * Ref.Type.Int:: The machine-dependent integer types.
  1331. * Ref.Type.Prim:: Primitive types.
  1332. * Ref.Type.Big:: The arbitrary-precision integer type.
  1333. * Ref.Type.Text:: Strings and characters.
  1334. * Ref.Type.Rec:: Labeled products of heterogeneous types.
  1335. * Ref.Type.Tup:: Unlabeled products of homogeneous types.
  1336. * Ref.Type.Vec:: Open products of homogeneous types.
  1337. * Ref.Type.Tag:: Disjoint sums of heterogeneous types.
  1338. * Ref.Type.Fn:: Subroutine types.
  1339. * Ref.Type.Iter:: Scoped coroutine types.
  1340. * Ref.Type.Port:: Unique inter-task message-receipt endpoints.
  1341. * Ref.Type.Chan:: Copyable inter-task message-send capabilities.
  1342. * Ref.Type.Task:: General coroutine-instance types.
  1343. * Ref.Type.Obj:: Abstract types.
  1344. * Ref.Type.Constr:: Constrained types.
  1345. * Ref.Type.Type:: Types describing types.
  1346. @end menu
  1347. @page
  1348. @node Ref.Type.Any
  1349. @subsection Ref.Type.Any
  1350. The type @code{any} is the union of all possible Rust types. A value of type
  1351. @code{any} is represented in memory as a pair consisting of an exterior value
  1352. of some non-@code{any} type @var{T} and a reflection of the type @var{T}.
  1353. Values of type @code{any} can be used in an @code{alt type} statement, in
  1354. which the reflection is used to select a block corresponding to a particular
  1355. type extraction. @xref{Ref.Stmt.Alt}.
  1356. @page
  1357. @node Ref.Type.Mach
  1358. @subsection Ref.Type.Mach
  1359. The machine types are the following:
  1360. @itemize
  1361. @item
  1362. The unsigned two's complement word types @code{u8}, @code{u16}, @code{u32} and
  1363. @code{u64}, with values drawn from the integer intervals
  1364. @iftex
  1365. @math{[0, 2^8 - 1]},
  1366. @math{[0, 2^{16} - 1]},
  1367. @math{[0, 2^{32} - 1]} and
  1368. @math{[0, 2^{64} - 1]}
  1369. @end iftex
  1370. @ifhtml
  1371. @html
  1372. [0, 2<sup>8</sup>-1],
  1373. [0, 2<sup>16</sup>-1],
  1374. [0, 2<sup>32</sup>-1] and
  1375. [0, 2<sup>64</sup>-1]
  1376. @end html
  1377. @end ifhtml
  1378. respectively.
  1379. @item
  1380. The signed two's complement word types @code{i8}, @code{i16}, @code{i32} and
  1381. @code{i64}, with values drawn from the integer intervals
  1382. @iftex
  1383. @math{[-(2^7),(2^7)-1)]},
  1384. @math{[-(2^{15}),2^{15}-1)]},
  1385. @math{[-(2^{31}),2^{31}-1)]} and
  1386. @math{[-(2^{63}),2^{63}-1)]}
  1387. @end iftex
  1388. @ifhtml
  1389. @html
  1390. [-(2<sup>7</sup>), 2<sup>7</sup>-1],
  1391. [-(2<sup>15</sup>), 2<sup>15</sup>-1],
  1392. [-(2<sup>31</sup>), 2<sup>31</sup>-1] and
  1393. [-(2<sup>63</sup>), 2<sup>63</sup>-1]
  1394. @end html
  1395. @end ifhtml
  1396. respectively.
  1397. @item
  1398. The IEEE 754 single-precision and double-precision floating point types:
  1399. @code{f32} and @code{f64}, respectively.
  1400. @end itemize
  1401. @page
  1402. @node Ref.Type.Int
  1403. @subsection Ref.Type.Int
  1404. The Rust type @code{uint}@footnote{A Rust @code{uint} is analogous to a C99
  1405. @code{uintptr_t}.} is a two's complement unsigned integer type with with
  1406. target-machine-dependent size. Its size, in bits, is equal to the number of
  1407. bits required to hold any memory address on the target machine.
  1408. The Rust type @code{int}@footnote{A Rust @code{int} is analogous to a C99
  1409. @code{intptr_t}.} is a two's complement signed integer type with
  1410. target-machine-dependent size. Its size, in bits, is equal to the size of the
  1411. rust type @code{uint} on the same target machine.
  1412. @page
  1413. @node Ref.Type.Prim
  1414. @subsection Ref.Type.Prim
  1415. The primitive types are the following:
  1416. @itemize
  1417. @item
  1418. The ``nil'' type @code{()}, having the single ``nil'' value
  1419. @code{()}.@footnote{The ``nil'' value @code{()} is @emph{not} a sentinel
  1420. ``null pointer'' value for alias or exterior slots; the ``nil'' type is the
  1421. implicit return type from functions otherwise lacking a return type, and can
  1422. be used in other contexts (such as message-sending or type-parametric code) as
  1423. a zero-byte type.}
  1424. @item
  1425. The boolean type @code{bool} with values @code{true} and @code{false}.
  1426. @item
  1427. The machine types.
  1428. @item
  1429. The machine-dependent integer types.
  1430. @end itemize
  1431. @page
  1432. @node Ref.Type.Big
  1433. @subsection Ref.Type.Big
  1434. The Rust type @code{big}@footnote{A Rust @code{big} is analogous to a Lisp
  1435. bignum or a Python long integer.} is an arbitrary precision integer type that
  1436. fits in a machine word @emph{when possible} and transparently expands to a
  1437. boxed ``big integer'' allocated in the run-time heap when it overflows or
  1438. underflows outside of the range of a machine word.
  1439. A Rust @code{big} grows to accommodate extra binary digits as they are needed,
  1440. by taking extra memory from the memory budget available to each Rust task, and
  1441. should only exhaust its range due to memory exhaustion.
  1442. @page
  1443. @node Ref.Type.Text
  1444. @subsection Ref.Type.Text
  1445. The types @code{char} and @code{str} hold textual data.
  1446. A value of type @code{char} is a Unicode character, represented as a 32-bit
  1447. unsigned word holding a UCS-4 codepoint.
  1448. A value of type @code{str} is a Unicode string, represented as a vector of
  1449. 8-bit unsigned bytes holding a sequence of UTF-8 codepoints.
  1450. @page
  1451. @node Ref.Type.Rec
  1452. @subsection Ref.Type.Rec
  1453. The record type-constructor @code{rec} forms a new heterogeneous product of
  1454. slots.@footnote{The @code{rec} type-constructor is analogous to the
  1455. @code{struct} type-constructor in the Algol/C family, the @emph{record} types
  1456. of the ML family, or the @emph{structure} types of the Lisp family.} Fields of
  1457. a @code{rec} type are accessed by name and are arranged in memory in the order
  1458. specified by the @code{rec} type.
  1459. An example of a @code{rec} type and its use:
  1460. @example
  1461. type point = rec(int x, int y);
  1462. let point p = rec(x=10, y=11);
  1463. let int px = p.x;
  1464. @end example
  1465. @page
  1466. @node Ref.Type.Tup
  1467. @subsection Ref.Type.Tup
  1468. The tuple type-constructor @code{tup} forms a new heterogeneous product of
  1469. slots exactly as the @code{rec} type-constructor does, with the difference
  1470. that tuple slots are automatically assigned implicit field names, given by
  1471. ascending integers prefixed by the underscore character: @code{_0}, @code{_1},
  1472. @code{_2}, etc. The fields of a tuple are laid out in memory contiguously,
  1473. like a record, in order specified by the tuple type.
  1474. An example of a tuple type and its use:
  1475. @example
  1476. type pair = tup(int,str);
  1477. let pair p = tup(10,"hello");
  1478. check (p._0 == 10);
  1479. p._1 = "world";
  1480. check (p._1 == "world");
  1481. @end example
  1482. @page
  1483. @node Ref.Type.Vec
  1484. @subsection Ref.Type.Vec
  1485. The vector type-constructor @code{vec} represents a homogeneous array of
  1486. slots. A vector has a fixed size, and may or may not have mutable member
  1487. slots. If the slots of a vector are mutable, the vector is a @emph{state}
  1488. type.
  1489. Vectors can be sliced. A slice expression builds a new vector by copying a
  1490. contiguous range -- given by a pair of indices representing a half-open
  1491. interval -- out of the sliced vector.
  1492. And example of a @code{vec} type and its use:
  1493. @example
  1494. let vec[int] v = vec(7, 5, 3);
  1495. let int i = v.(2);
  1496. let vec[int] v2 = v.(0,1); // Form a slice.
  1497. @end example
  1498. Vectors always @emph{allocate} a storage region sufficient to store the first
  1499. power of two worth of elements greater than or equal to the size of the
  1500. largest slice sharing the storage. This behaviour supports idiomatic in-place
  1501. ``growth'' of a mutable slot holding a vector:
  1502. @example
  1503. let mutable vec[int] v = vec(1, 2, 3);
  1504. v += vec(4, 5, 6);
  1505. @end example
  1506. Normal vector concatenation causes the allocation of a fresh vector to hold
  1507. the result; in this case, however, the slot holding the vector recycles the
  1508. underlying storage in-place (since the reference-count of the underlying
  1509. storage is equal to 1).
  1510. All accessible elements of a vector are always initialized, and access to a
  1511. vector is always bounds-checked.
  1512. @page
  1513. @node Ref.Type.Tag
  1514. @subsection Ref.Type.Tag
  1515. The @code{tag} type-constructor forms new heterogeneous disjoint sum
  1516. types.@footnote{The @code{tag} type is analogous to a @code{data} constructor
  1517. declaration in ML or a @emph{pick ADT} in Limbo.} A @code{tag} type consists
  1518. of a number of @emph{variants}, each of which is independently named and takes
  1519. an optional tuple of arguments.
  1520. The variants of a @code{tag} type may be recursive: that is, the definition of
  1521. a @code{tag} type may refer to type definitions that include the defined
  1522. @code{tag} type itself. Such recursion has restrictions:
  1523. @itemize
  1524. @item Recursive types can only be introduced through @code{tag} types.
  1525. @item A recursive @code{tag} type must have at least one non-recursive
  1526. variant (in order to give the recursion a basis case).
  1527. @item The recursive slots of recursive variants must be @emph{exterior}
  1528. slots (in order to bound the in-memory size of the variant).
  1529. @item Recursive type definitions can cross module boundaries, but not module
  1530. @emph{visibility} boundaries, nor crate boundaries (in order to simplify the
  1531. module system).
  1532. @end itemize
  1533. An example of a @code{tag} type and its use:
  1534. @example
  1535. type animal = tag(dog, cat);
  1536. let animal a = dog;
  1537. a = cat;
  1538. @end example
  1539. An example of a @emph{recursive} @code{tag} type and its use:
  1540. @example
  1541. type list[T] = tag(nil,
  1542. cons(T, @@list[T]));
  1543. let list[int] a = cons(7, cons(13, nil));
  1544. @end example
  1545. @page
  1546. @node Ref.Type.Fn
  1547. @subsection Ref.Type.Fn
  1548. The function type-constructor @code{fn} forms new function types. A function
  1549. type consists of a sequence of input slots, an optional set of input
  1550. constraints (@pxref{Ref.Stmt.Stat.Constr}), an output slot, and an
  1551. @emph{effect}. @xref{Ref.Item.Fn}.
  1552. An example of a @code{fn} type:
  1553. @example
  1554. fn add(int x, int y) -> int @{
  1555. ret x + y;
  1556. @}
  1557. let int x = add(5,7);
  1558. type binop = fn(int,int) -> int;
  1559. let binop bo = add;
  1560. x = bo(5,7);
  1561. @end example
  1562. @page
  1563. @node Ref.Type.Iter
  1564. @subsection Ref.Type.Iter
  1565. The iterator type-constructor @code{iter} forms new iterator types. An
  1566. iterator type consists a sequence of input slots, an optional set of input
  1567. constraints, an output slot, and an @emph{effect}. @xref{Ref.Item.Iter}.
  1568. An example of an @code{iter} type:
  1569. @example
  1570. iter range(int x, int y) -> int @{
  1571. while (x < y) @{
  1572. put x;
  1573. x += 1;
  1574. @}
  1575. @}
  1576. for each (int i = range(5,7)) @{
  1577. @dots{};
  1578. @}
  1579. @end example
  1580. @page
  1581. @node Ref.Type.Port
  1582. @subsection Ref.Type.Port
  1583. The port type-constructor @code{port} forms types that describe ports. A port
  1584. is the @emph{receiving end} of a typed, asynchronous, simplex inter-task
  1585. communication facility. @xref{Ref.Task.Comm}. A @code{port} type takes a
  1586. single type parameter, denoting the type of value that can be received from a
  1587. @code{port} value of that type.
  1588. Ports are modeled as mutable native types with built-in meaning to the
  1589. language. They cannot be transmitted over channels or otherwise replicated,
  1590. and are always local to the task that creates them.
  1591. An example of a @code{port} type:
  1592. @example
  1593. type port[vec[str]] svp;
  1594. let svp p = get_port();
  1595. let vec[str] v;
  1596. v <- p;
  1597. @end example
  1598. @page
  1599. @node Ref.Type.Chan
  1600. @subsection Ref.Type.Chan
  1601. The channel type-constructor @code{chan} forms types that describe channels. A
  1602. channel is the @emph{sending end} of a typed, asynchronous, simplex inter-task
  1603. communication facility. @xref{Ref.Task.Comm}. A @code{chan} type takes a
  1604. single type parameter, denoting the type of value that can be sent to a
  1605. channel of that type.
  1606. Channels are immutable, and can be transmitted over channels to other
  1607. tasks. They are modeled as immutable native types with built-in meaning to the
  1608. language.
  1609. When a task sends a message into a channel, the task forms an outgoing queue
  1610. associated with that channel. The per-task queue @emph{associated} with a
  1611. channel can be indirectly manipulated by the task, but is @emph{not} otherwise
  1612. considered ``part of'' to the channel: the queue is ``part of'' the
  1613. @emph{sending task}. Sending a channel to another task does not copy the queue
  1614. associated with the channel.
  1615. Channels are also @emph{weak}: a channel is directly coupled to a particular
  1616. destination port on a particular task, but does not keep that port or task
  1617. @emph{alive}. A channel may therefore fail to operate at any moment. If a task
  1618. sends to a channel that is connected to a nonexistent port, it receives a
  1619. signal.
  1620. An example of a @code{chan} type:
  1621. @example
  1622. type chan[vec[str]] svc;
  1623. let svc c = get_chan();
  1624. let vec[str] v = vec("hello", "world");
  1625. c <| v;
  1626. @end example
  1627. @page
  1628. @node Ref.Type.Task
  1629. @subsection Ref.Type.Task
  1630. The task type @code{task} describes values that are @emph{live
  1631. tasks}.
  1632. Tasks form an @emph{ownership tree} in which each task (except the root task)
  1633. is directly owned by exactly one parent task. The purpose of a variable of
  1634. @code{task} type is to manage the lifecycle of the associated
  1635. task. Communication is carried out solely using channels and ports.
  1636. Like ports, tasks are modeled as mutable native types with built-in meaning to
  1637. the language. They cannot be transmitted over channels or otherwise
  1638. replicated, and are always local to the task that spawns them.
  1639. If all references to a task are dropped (due to the release of any slots
  1640. holding those references), the released task immediately fails.
  1641. @xref{Ref.Task.Life}.
  1642. @page
  1643. @node Ref.Type.Obj
  1644. @subsection Ref.Type.Obj
  1645. @c * Ref.Type.Obj:: Object types.
  1646. A @dfn{object type} describes values of abstract type, that carry some hidden
  1647. @emph{fields} and are accessed through a set of un-ordered
  1648. @emph{methods}. Every object item (@pxref{Ref.Item.Obj}) implicitly declares
  1649. an object type carrying methods with types derived from all the methods of the
  1650. object item.
  1651. Object types can also be declared in isolation, independent of any object item
  1652. declaration. Such a ``plain'' object type can be used to describe an interface
  1653. that a variety of particular objects may conform to, by supporting a superset
  1654. of the methods.
  1655. An object type that can contain a state must be declared as a @code{state obj}
  1656. like any other state type. And similarly a method type that performs I/O or
  1657. makes native calls must be declared @code{io} or @code{unsafe}, like any other
  1658. function.
  1659. Moreover, @emph{all} methods of a state object are implicitly state functions -- as
  1660. they all bind the same mutable state field(s) -- so implicitly have an effect
  1661. lower than @code{io}. It is therefore unnecessary to declare methods within a
  1662. state object type (or state object item) as @code{io}.
  1663. An example of an object type with two separate object items supporting it, and
  1664. a client function using both items via the object type:
  1665. @example
  1666. state type taker =
  1667. state obj @{
  1668. fn take(int);
  1669. @};
  1670. state obj adder(mutable int x) @{
  1671. fn take(int y) @{
  1672. x += y;
  1673. @}
  1674. @}
  1675. obj sender(chan[int] c) @{
  1676. io fn take(int z) @{
  1677. c <| z;
  1678. @}
  1679. @}
  1680. fn give_ints(taker t) @{
  1681. t.take(1);
  1682. t.take(2);
  1683. t.take(3);
  1684. @}
  1685. let port[int] p = port();
  1686. let taker t1 = adder(0);
  1687. let taker t2 = sender(chan(p));
  1688. give_ints(t1);
  1689. give_ints(t2);
  1690. @end example
  1691. @page
  1692. @node Ref.Type.Constr
  1693. @subsection Ref.Type.Constr
  1694. @c * Ref.Type.Constr:: Constrained types.
  1695. A @dfn{constrained type} is a type that carries a @emph{formal constraint}
  1696. (@pxref{Ref.Stmt.Stat.Constr}), which is similar to a normal constraint except
  1697. that the @emph{base name} of any slots mentioned in the constraint must be the
  1698. special @emph{formal symbol} @emph{*}.
  1699. When a constrained type is instantiated in a particular slot declaration, the
  1700. formal symbol in the constraint is replaced with the name of the declared slot
  1701. and the resulting constraint is checked immediately after the slot is
  1702. declared. @xref{Ref.Stmt.Check}.
  1703. An example of a constrained type with two separate instantiations:
  1704. @example
  1705. type ordered_range = rec(int low, int high) : less_than(*.low, *.high);
  1706. let ordered_range rng1 = rec(low=5, high=7);
  1707. // implicit: 'check less_than(rng1.low, rng1.high);'
  1708. let ordered_range rng2 = rec(low=15, high=17);
  1709. // implicit: 'check less_than(rng2.low, rng2.high);'
  1710. @end example
  1711. @page
  1712. @node Ref.Type.Type
  1713. @subsection Ref.Type.Type
  1714. @c * Ref.Type.Type:: Types describing types.
  1715. @emph{TODO}.
  1716. @page
  1717. @node Ref.Expr
  1718. @section Ref.Expr
  1719. @c * Ref.Expr:: Parsed and primitive expressions.
  1720. Rust has two kinds of expressions: @emph{parsed expressions} and
  1721. @emph{primitive expressions}. The former are syntactic sugar and are
  1722. eliminated during parsing. The latter are very minimal, consisting only of
  1723. paths and primitive literals, possibly combined via a single level
  1724. (non-recursive) unary or binary machine-level operation (ALU or
  1725. FPU). @xref{Ref.Path}.
  1726. For the most part, Rust semantics are defined in terms of @emph{statements},
  1727. which parsed expressions are desugared to. The desugaring is defined in the
  1728. grammar. @xref{Ref.Gram}. The residual primitive statements appear only in the
  1729. right hand side of copy statements, @xref{Ref.Stmt.Copy}.
  1730. @page
  1731. @node Ref.Stmt
  1732. @section Ref.Stmt
  1733. @c * Ref.Stmt:: Executable statements.
  1734. A @dfn{statement} is a component of a block, which is in turn a components of
  1735. an outer block, a function or an iterator. When a function is spawned into a
  1736. task, the task @emph{executes} statements in an order determined by the body
  1737. of the enclosing structure. Each statement causes the task to perform certain
  1738. actions.
  1739. @menu
  1740. * Ref.Stmt.Stat:: The static typestate system of statement analysis.
  1741. * Ref.Stmt.Decl:: Statement declaring an item or slot.
  1742. * Ref.Stmt.Copy:: Statement for copying a value between two slots.
  1743. * Ref.Stmt.Spawn:: Statements for creating new tasks.
  1744. * Ref.Stmt.Send:: Statements for sending a value into a channel.
  1745. * Ref.Stmt.Flush:: Statement for flushing a channel queue.
  1746. * Ref.Stmt.Recv:: Statement for receiving a value from a channel.
  1747. * Ref.Stmt.Call:: Statement for calling a function.
  1748. * Ref.Stmt.Bind:: Statement for binding arguments to functions.
  1749. * Ref.Stmt.Ret:: Statement for stopping and producing a value.
  1750. * Ref.Stmt.Be:: Statement for stopping and executing a tail call.
  1751. * Ref.Stmt.Put:: Statement for pausing and producing a value.
  1752. * Ref.Stmt.Fail:: Statement for causing task failure.
  1753. * Ref.Stmt.Log:: Statement for logging values to diagnostic buffers.
  1754. * Ref.Stmt.Note:: Statement for logging values during failure.
  1755. * Ref.Stmt.While:: Statement for simple conditional looping.
  1756. * Ref.Stmt.Break:: Statement for terminating a loop.
  1757. * Ref.Stmt.Cont:: Statement for terminating a single loop iteration.
  1758. * Ref.Stmt.For:: Statement for looping over strings and vectors.
  1759. * Ref.Stmt.Foreach:: Statement for looping via an iterator.
  1760. * Ref.Stmt.If:: Statement for simple conditional branching.
  1761. * Ref.Stmt.Alt:: Statement for complex conditional branching.
  1762. * Ref.Stmt.Prove:: Statement for static assertion of typestate.
  1763. * Ref.Stmt.Check:: Statement for dynamic assertion of typestate.
  1764. * Ref.Stmt.IfCheck:: Statement for dynamic testing of typestate.
  1765. @end menu
  1766. @page
  1767. @node Ref.Stmt.Stat
  1768. @subsection Ref.Stmt.Stat
  1769. @c * Ref.Stmt.Stat:: The static typestate system of statement analysis.
  1770. Statements have a detailed static semantics. The static semantics determine,
  1771. on a statement-by-statement basis, the @emph{effects} the statement has on its
  1772. environment, as well the @emph{legality} of the statement in its environment.
  1773. The legality of a statement is partly governed by syntactic rules, partly by
  1774. its conformance to the types of slots it affects, and partly by a
  1775. statement-oriented static dataflow analysis. This section describes the
  1776. statement-oriented static dataflow analysis, also called the @emph{typestate}
  1777. system.
  1778. @menu
  1779. * Ref.Stmt.Stat.Point:: Inter-statement positions of logical judgements.
  1780. * Ref.Stmt.Stat.CFG:: The control flow graph formed by statements.
  1781. * Ref.Stmt.Stat.Constr:: Predicates applied to slots.
  1782. * Ref.Stmt.Stat.Cond:: Constraints required and implied by a statement.
  1783. * Ref.Stmt.Stat.Typestate:: Constraints that hold at points.
  1784. * Ref.Stmt.Stat.Check:: Relating dynamic state to static typestate.
  1785. @end menu
  1786. @page
  1787. @node Ref.Stmt.Stat.Point
  1788. @subsubsection Ref.Stmt.Stat.Point
  1789. @c * Ref.Stmt.Stat.Point:: Inter-statement positions of logical judgements.
  1790. A @dfn{point} exists before and after any statement in a Rust program.
  1791. For example, this code:
  1792. @example
  1793. s = "hello, world";
  1794. print(s);
  1795. @end example
  1796. Consists of two statements and four points:
  1797. @itemize
  1798. @item the point before the first statement
  1799. @item the point after the first statement
  1800. @item the point before the second statement
  1801. @item the point after the second statement
  1802. @end itemize
  1803. The typestate system reasons over points, rather than statements. This may
  1804. seem counter-intuitive, but points are the more primitive concept. Another way
  1805. of thinking about a point is as a set of @emph{instants in time} at which the
  1806. state of a task is fixed. By contrast, a statement represents a @emph{duration
  1807. in time}, during which the state of the task changes. The typestate system is
  1808. concerned with constraining the possible states of a task's memory at
  1809. @emph{instants}; it is meaningless to speak of the state of a task's memory
  1810. ``at'' a statement, as each statement is likely to change the contents of
  1811. memory.
  1812. @page
  1813. @node Ref.Stmt.Stat.CFG
  1814. @subsubsection Ref.Stmt.Stat.CFG
  1815. @c * Ref.Stmt.Stat.CFG:: The control flow graph formed by statements.
  1816. Each @emph{point} can be considered a vertex in a directed @emph{graph}. Each
  1817. kind of statement implies a single edge in this graph between the point before
  1818. the statement and the point after it, as well as a set of zero or more edges
  1819. from the points of the statement to points before other statements. The edges
  1820. between points represent @emph{possible} indivisible control transfers that
  1821. might occur during execution.
  1822. This implicit graph is called the @dfn{control flow graph}, or @dfn{CFG}.
  1823. @page
  1824. @node Ref.Stmt.Stat.Constr
  1825. @subsubsection Ref.Stmt.Stat.Constr
  1826. @c * Ref.Stmt.Stat.Constr:: Predicates applied to slots.
  1827. A @dfn{predicate} is any pure boolean function. @xref{Ref.Item.Fn}.
  1828. A @dfn{constraint} is a predicate applied to specific slots.
  1829. For example, consider the following code:
  1830. @example
  1831. fn is_less_than(int a, int b) -> bool @{
  1832. ret a < b;
  1833. @}
  1834. fn test() @{
  1835. let int x = 10;
  1836. let int y = 20;
  1837. check is_less_than(x,y);
  1838. @}
  1839. @end example
  1840. This example defines the predicate @code{is_less_than}, and applies it to the
  1841. slots @code{x} and @code{y}. The constraint being checked on the third line of
  1842. the function is @code{is_less_than(x,y)}.
  1843. Predicates can only apply to slots holding immutable values. The slots a
  1844. predicate applies to can themselves be mutable, but the types of values held
  1845. in those slots must be immutable.
  1846. @page
  1847. @node Ref.Stmt.Stat.Cond
  1848. @subsubsection Ref.Stmt.Stat.Cond
  1849. @c * Ref.Stmt.Stat.Cond:: Constraints required and implied by a statement.
  1850. A @dfn{condition} is a set of zero or more constraints.
  1851. Each @emph{point} has an associated @emph{condition}:
  1852. @itemize
  1853. @item The @dfn{precondition} of a statement is the condition the statement
  1854. requires in the point before the condition.
  1855. @item The @dfn{postcondition} of a statement is the condition the statement
  1856. enforces in the point after the statement.
  1857. @end itemize
  1858. Any constraint present in the precondition and @emph{absent} in the
  1859. postcondition is considered to be @emph{dropped} by the statement.
  1860. @page
  1861. @node Ref.Stmt.Stat.Typestate
  1862. @subsubsection Ref.Stmt.Stat.Typestate
  1863. @c * Ref.Stmt.Stat.Typestate:: Constraints that hold at points.
  1864. The typestate checking system @emph{calculates} an additional
  1865. condition for each point called its typestate. For a given statement,
  1866. we call the two typestates associated with its two points the prestate
  1867. and a poststate.
  1868. @itemize
  1869. @item The @dfn{prestate} of a statement is the typestate of the point
  1870. before the statement.
  1871. @item The @dfn{poststate} of a statement is the typestate of the point
  1872. after the statement.
  1873. @end itemize
  1874. A @dfn{typestate} is a condition that has @emph{been determined by the
  1875. typestate algorithm} to hold at a point. This is a subtle but important point
  1876. to understand: preconditions and postconditions are @emph{inputs} to the
  1877. typestate algorithm; prestates and poststates are @emph{outputs} from the
  1878. typestate algorithm.
  1879. The typestate algorithm analyses the preconditions and postconditions of every
  1880. statement in a block, and computes a condition for each
  1881. typestate. Specifically:
  1882. @itemize
  1883. @item Initially, every typestate is empty.
  1884. @item Each statement's poststate is given the union of the statement's
  1885. prestate, precondition, and postcondition.
  1886. @item Each statement's poststate has the difference between the statement's
  1887. precondition and postcondition removed.
  1888. @item Each statement's prestate is given the intersection of the poststates
  1889. of every parent statement in the CFG.
  1890. @item The previous three steps are repeated until no typestates in the
  1891. block change.
  1892. @end itemize
  1893. The typestate algorithm is a very conventional dataflow calculation, and can
  1894. be performed using bit-set operations, with one bit per predicate and one
  1895. bit-set per condition.
  1896. After the typestates of a block are computed, the typestate algorithm checks
  1897. that every constraint in the precondition of a statement is satisfied by its
  1898. prestate. If any preconditions are not satisfied, the mismatch is considered a
  1899. static (compile-time) error.
  1900. @page
  1901. @node Ref.Stmt.Stat.Check
  1902. @subsubsection Ref.Stmt.Stat.Check
  1903. @c * Ref.Stmt.Stat.Check:: Relating dynamic state to static typestate.
  1904. The key mechanism that connects run-time semantics and compile-time analysis
  1905. of typestates is the use of @code{check} statements. @xref{Ref.Stmt.Check}. A
  1906. @code{check} statement guarantees that @emph{if} control were to proceed past
  1907. it, the predicate associated with the @code{check} would have succeeded, so
  1908. the constraint being checked @emph{statically} holds in subsequent
  1909. statements.@footnote{A @code{check} statement is similar to an @code{assert}
  1910. call in a C program, with the significant difference that the Rust compiler
  1911. @emph{tracks} the constraint that each @code{check} statement
  1912. enforces. Naturally, @code{check} statements cannot be omitted from a
  1913. ``production build'' of a Rust program the same way @code{asserts} are
  1914. frequently disabled in deployed C programs.}
  1915. It is important to understand that the typestate system has @emph{no insight}
  1916. into the meaning of a particular predicate. Predicates and constraints are not
  1917. evaluated in any way at compile time. Predicates are treated as specific (but
  1918. unknown) functions applied to specific (also unknown) slots. All the typestate
  1919. system does is track which of those predicates -- whatever they calculate --
  1920. @emph{must have been checked already} in order for program control to reach a
  1921. particular point in the CFG. The fundamental building block, therefore, is the
  1922. @code{check} statement, which tells the typestate system ``if control passes
  1923. this statement, the checked predicate holds''.
  1924. From this building block, constraints can be propagated to function signatures
  1925. and constrained types, and the responsibility to @code{check} a constraint
  1926. pushed further and further away from the site at which the program requires it
  1927. to hold in order to execute properly.
  1928. @page
  1929. @node Ref.Stmt.Decl
  1930. @subsection Ref.Stmt.Decl
  1931. @c * Ref.Stmt.Decl:: Statement declaring an item or slot.
  1932. A @dfn{declaration statement} is one that introduces a @emph{name} into the
  1933. enclosing statement block. The declared name may denote a new slot or a new
  1934. item. The scope of the name extends to the entire containing block, both
  1935. before and after the declaration.
  1936. @menu
  1937. * Ref.Stmt.Decl.Item:: Statement declaring an item.
  1938. * Ref.Stmt.Decl.Slot:: Statement declaring a slot.
  1939. @end menu
  1940. @page
  1941. @node Ref.Stmt.Decl.Item
  1942. @subsubsection Ref.Stmt.Decl.Item
  1943. @c * Ref.Stmt.Decl.Item:: Statement declaring an item.
  1944. An @dfn{item declaration statement} has a syntactic form identical to an item
  1945. declaration within a module. Declaring an item -- a function, iterator,
  1946. object, type or module -- locally within a statement block is simply a way of
  1947. restricting its scope to a narrow region containing all of its uses; it is
  1948. otherwise identical in meaning to declaring the item outside the statement
  1949. block.
  1950. Note: there is no implicit capture of the function's dynamic environment when
  1951. declaring a function-local item.
  1952. @page
  1953. @node Ref.Stmt.Decl.Slot
  1954. @subsubsection Ref.Stmt.Decl.Slot
  1955. @c * Ref.Stmt.Decl.Slot:: Statement declaring an slot.
  1956. A @code{slot declaration statement} has one one of two forms:
  1957. @itemize
  1958. @item @code{let} @var{mode-and-type} @var{slot} @var{optional-init};
  1959. @item @code{auto} @var{slot} @var{optional-init};
  1960. @end itemize
  1961. Where @var{mode-and-type} is a slot mode and type expression, @var{slot} is
  1962. the name of the slot being declared, and @var{optional-init} is either the
  1963. empty string or an equals sign (@code{=}) followed by a primitive expression.
  1964. Both forms introduce a new slot into the containing block scope. The new slot
  1965. is visible across the entire scope, but is initialized only at the point
  1966. following the declaration statement.
  1967. The latter (@code{auto}) form of slot declaration causes the compiler to infer
  1968. the static type of the slot through unification with the types of values
  1969. assigned to the slot in the the remaining code in the block scope. Inferred
  1970. slots always have @emph{interior} mode. @xref{Ref.Mem.Slot}.
  1971. @page
  1972. @node Ref.Stmt.Copy
  1973. @subsection Ref.Stmt.Copy
  1974. @c * Ref.Stmt.Copy:: Statement for copying a value between two slots.
  1975. A @dfn{copy statement} consists of an @emph{lval} -- a name denoting a slot --
  1976. followed by an equals-sign (@code{=}) and a primitive
  1977. expression. @xref{Ref.Expr}.
  1978. Executing a copy statement causes the value denoted by the expression --
  1979. either a value in a slot or a primitive combination of values held in slots --
  1980. to be copied into the slot denoted by the @emph{lval}.
  1981. A copy may entail the formation of references, the adjustment of reference
  1982. counts, execution of destructors, or similar adjustments in order to respect
  1983. the @code{lval} slot mode and any existing value held in it. All such
  1984. adjustment is automatic and implied by the @code{=} operator.
  1985. An example of three different copy statements:
  1986. @example
  1987. x = y;
  1988. x.y = z;
  1989. x.y = z + 2;
  1990. @end example
  1991. @page
  1992. @node Ref.Stmt.Spawn
  1993. @subsection Ref.Stmt.Spawn
  1994. @c * Ref.Stmt.Spawn:: Statements creating new tasks.
  1995. A @code{spawn} statement consists of keyword @code{spawn}, followed by a
  1996. normal @emph{call} statement (@pxref{Ref.Stmt.Call}). A @code{spawn}
  1997. statement causes the runtime to construct a new task executing the called
  1998. function. The called function is referred to as the @dfn{entry function} for
  1999. the spawned task, and its arguments are copied form the spawning task to the
  2000. spawned task before the spawned task begins execution.
  2001. Only arguments of interior or exterior mode are permitted in the function
  2002. called by a spawn statement, not arguments with alias mode.
  2003. The result of a @code{spawn} statement is a @code{task} value.
  2004. An example of a @code{spawn} statement:
  2005. @example
  2006. fn helper(chan[u8] out) @{
  2007. // do some work.
  2008. out <| result;
  2009. @}
  2010. let port[u8] out;
  2011. let task p = spawn helper(chan(out));
  2012. // let task run, do other things.
  2013. auto result <- out;
  2014. @end example
  2015. @page
  2016. @node Ref.Stmt.Send
  2017. @subsection Ref.Stmt.Send
  2018. @c * Ref.Stmt.Send:: Statements for sending a value into a channel.
  2019. Sending a value through a channel can be done via two different statements.
  2020. Both statements take an @emph{lval}, denoting a channel, and a value to send
  2021. into the channel. The action of @emph{sending} varies depending on the
  2022. @dfn{send operator} employed.
  2023. The @emph{asynchronous send} operator @code{<+} adds a value to the channel's
  2024. queue, without blocking. If the queue is full, it is extended, taking memory
  2025. from the task's domain. If the task memory budget is exhausted, a signal is
  2026. sent to the task.
  2027. The @emph{semi-synchronous send} operator @code{<|} adds a value to the
  2028. channel's queue @emph{only if} the queue has room; if the queue is full, the
  2029. operation @emph{blocks} the sender until the queue has room.
  2030. An example of an asynchronous send:
  2031. @example
  2032. chan[str] c = @dots{};
  2033. c <+ "hello, world";
  2034. @end example
  2035. An example of a semi-synchronous send:
  2036. @example
  2037. chan[str] c = @dots{};
  2038. c <| "hello, world";
  2039. @end example
  2040. @page
  2041. @node Ref.Stmt.Flush
  2042. @subsection Ref.Stmt.Flush
  2043. @c * Ref.Stmt.Flush:: Statement for flushing a channel queue.
  2044. A @code{flush} statement takes a channel and blocks the flushing task until
  2045. the channel's queue has emptied. It can be used to implement a more precise
  2046. form of flow-control than with the send operators alone.
  2047. An example of the @code{flush} statement:
  2048. @example
  2049. chan[str] c = @dots{};
  2050. c <| "hello, world";
  2051. flush c;
  2052. @end example
  2053. @page
  2054. @node Ref.Stmt.Recv
  2055. @subsection Ref.Stmt.Recv
  2056. @c * Ref.Stmt.Recv:: Statement for receiving a value from a channel.
  2057. The @dfn{receive statement} takes an @var{lval} to receive into and an
  2058. expression denoting a port, and applies the @emph{receive operator}
  2059. (@code{<-}) to the pair, copying a value out of the port and into the
  2060. @var{lval}. The statement causes the receiving task to enter the @emph{blocked
  2061. reading} state until a task is sending a value to the port, at which point the
  2062. runtime pseudo-randomly selects a sending task and copies a value from the
  2063. head of one of the task queues to the receiving slot, and un-blocks the
  2064. receiving task. @xref{Ref.Run.Comm}.
  2065. An example of a @emph{receive}:
  2066. @example
  2067. port[str] p = @dots{};
  2068. let str s <- p;
  2069. @end example
  2070. @page
  2071. @node Ref.Stmt.Call
  2072. @subsection Ref.Stmt.Call
  2073. @c * Ref.Stmt.Call:: Statement for calling a function.
  2074. A @dfn{call statement} invokes a function, providing a tuple of input slots
  2075. and a reference to an output slot. If the function eventually returns, then
  2076. the statement completes.
  2077. A call statement statically requires that the precondition declared in the
  2078. callee's signature is satisfied by the statement prestate. In this way,
  2079. typestates propagate through function boundaries. @xref{Ref.Stmt.Stat}.
  2080. An example of a call statement:
  2081. @example
  2082. let int x = add(1, 2);
  2083. @end example
  2084. @page
  2085. @node Ref.Stmt.Bind
  2086. @subsection Ref.Stmt.Bind
  2087. @c * Ref.Stmt.Bind:: Statement for binding arguments to functions.
  2088. A @dfn{bind statement} constructs a new function from an existing
  2089. function.@footnote{The @code{bind} statement is analogous to the @code{bind}
  2090. expression in the Sather language.} The new function has zero or more of its
  2091. arguments @emph{bound} into a new, hidden exterior tuple that holds the
  2092. bindings. For each concrete argument passed in the @code{bind} statement, the
  2093. corresponding parameter in the existing function is @emph{omitted} as a
  2094. parameter of the new function. For each argument passed the placeholder symbol
  2095. @code{_} in the @code{bind} statement, the corresponding parameter of the
  2096. existing function is @emph{retained} as a parameter of the new function.
  2097. Any subsequent invocation of the new function with residual arguments causes
  2098. invocation of the existing function with the combination of bound arguments
  2099. and residual arguments that was specified during the binding.
  2100. An example of a @code{bind} statement:
  2101. @example
  2102. fn add(int x, int y) -> int @{
  2103. ret x + y;
  2104. @}
  2105. type single_param_fn = fn(int) -> int;
  2106. let single_param_fn add4 = bind add(4, _);
  2107. let single_param_fn add5 = bind add(_, 5);
  2108. check (add(4,5) == add4(5));
  2109. check (add(4,5) == add5(4));
  2110. @end example
  2111. A @code{bind} statement generally stores a copy of the bound arguments in the
  2112. hidden exterior tuple. For bound interior slots and alias slots in the bound
  2113. function signature, an interior slot is allocated in the hidden tuple and
  2114. populated with a copy of the bound value. For bound exterior slots in the
  2115. bound function signature, an exterior slot is allocated in the hidden tuple
  2116. and populated with a copy of the bound value, an exterior (pointer) value.
  2117. The @code{bind} statement is a lightweight mechanism for simulating the more
  2118. elaborate construct of @emph{lexical closures} that exist in other
  2119. languages. Rust has no support for lexical closures, but many realistic uses
  2120. of them can be achieved with @code{bind} statements.
  2121. @page
  2122. @node Ref.Stmt.Ret
  2123. @subsection Ref.Stmt.Ret
  2124. @c * Ref.Stmt.Ret:: Statement for stopping and producing a value.
  2125. Executing a @code{ret} statement@footnote{A @code{ret} statement is
  2126. analogous to a @code{return} statement in the C family.} copies a
  2127. value into the return slot of the current function, destroys the
  2128. current function activation frame, and transfers control to the caller
  2129. frame.
  2130. An example of a @code{ret} statement:
  2131. @example
  2132. fn max(int a, int b) -> int @{
  2133. if (a > b) @{
  2134. ret a;
  2135. @}
  2136. ret b;
  2137. @}
  2138. @end example
  2139. @page
  2140. @node Ref.Stmt.Be
  2141. @subsection Ref.Stmt.Be
  2142. @c * Ref.Stmt.Be:: Statement for stopping and executing a tail call.
  2143. Executing a @code{be} statement @footnote{A @code{be} statement in is
  2144. analogous to a @code{become} statement in Newsqueak or Alef.} destroys the
  2145. current function activation frame and replaces it with an activation frame for
  2146. the called function. In other words, @code{be} executes a tail-call. The
  2147. syntactic form of a @code{be} statement is therefore limited to @emph{tail
  2148. position}: its argument must be a @emph{call expression}, and it must be the
  2149. last statement in a block.
  2150. An example of a @code{be} statement:
  2151. @example
  2152. fn print_loop(int n) @{
  2153. if (n <= 0) @{
  2154. ret;
  2155. @} else @{
  2156. print_int(n);
  2157. be print_loop(n-1);
  2158. @}
  2159. @}
  2160. @end example
  2161. The above example executes in constant space, replacing each frame with a new
  2162. copy of itself.
  2163. @page
  2164. @node Ref.Stmt.Put
  2165. @subsection Ref.Stmt.Put
  2166. @c * Ref.Stmt.Put:: Statement for pausing and producing a value.
  2167. Executing a @code{put} statement copies a value into the put slot of the
  2168. current iterator, suspends execution of the current iterator, and transfers
  2169. control to the current put-recipient frame.
  2170. A @code{put} statement is only valid within an iterator. @footnote{A
  2171. @code{put} statement is analogous to a @code{yield} statement in the CLU,
  2172. Sather and Objective C 2.0 languages, or in more recent languages providing a
  2173. ``generator'' facility, such as Python, Javascript or C#. Like the generators
  2174. of CLU, Sather and Objective C 2.0, but @emph{unlike} these later languages,
  2175. Rust's iterators reside on the stack and obey a strict stack discipline.} The
  2176. current put-recipient will eventually resume the suspended iterator containing
  2177. the @code{put} statement, either continuing execution after the @code{put}
  2178. statement, or terminating its execution and destroying the iterator frame.
  2179. @page
  2180. @node Ref.Stmt.Fail
  2181. @subsection Ref.Stmt.Fail
  2182. @c * Ref.Stmt.Fail:: Statement for causing task failure.
  2183. Executing a @code{fail} statement causes a task to enter the @emph{failing}
  2184. state. In the @emph{failing} state, a task unwinds its stack, destroying all
  2185. frames and freeing all resources until it reaches its entry frame, at which
  2186. point it halts execution in the @emph{dead} state.
  2187. @page
  2188. @node Ref.Stmt.Log
  2189. @subsection Ref.Stmt.Log
  2190. @c * Ref.Stmt.Log:: Statement for logging values to diagnostic buffers.
  2191. Executing a @code{log} statement may, depending on runtime configuration,
  2192. cause a value to be appended to an internal diagnostic logging buffer provided
  2193. by the runtime or emitted to a system console. Log statements are enabled or
  2194. disabled dynamically at run-time on a per-task and per-item
  2195. basis. @xref{Ref.Run.Log}.
  2196. Executing a @code{log} statement not considered an @code{io} effect in the
  2197. effect system. In other words, a pure function remains pure even if it
  2198. contains a log statement.
  2199. @example
  2200. @end example
  2201. @page
  2202. @node Ref.Stmt.Note
  2203. @subsection Ref.Stmt.Note
  2204. @c * Ref.Stmt.Note:: Statement for logging values during failure.
  2205. A @code{note} statement has no effect during normal execution. The purpose of
  2206. a @code{note} statement is to provide additional diagnostic information to the
  2207. logging subsystem during task failure. @xref{Ref.Stmt.Log}. Using @code{note}
  2208. statements, normal diagnostic logging can be kept relatively sparse, while
  2209. still providing verbose diagnostic ``back-traces'' when a task fails.
  2210. When a task is failing, control frames @emph{unwind} from the innermost frame
  2211. to the outermost, and from the innermost lexical block within an unwinding
  2212. frame to the outermost. When unwinding a lexical block, the runtime processes
  2213. all the @code{note} statements in the block sequentially, from the first
  2214. statement of the block to the last. During processing, a @code{note}
  2215. statement has equivalent meaning to a @code{log} statement: it causes the
  2216. runtime to append the argument of the @code{note} to the internal logging
  2217. diagnostic buffer.
  2218. An example of a @code{note} statement:
  2219. @example
  2220. fn read_file_lines(&str path) -> vec[str] @{
  2221. note path;
  2222. vec[str] r;
  2223. file f = open_read(path);
  2224. for* (str &s = lines(f)) @{
  2225. vec.append(r,s);
  2226. @}
  2227. ret r;
  2228. @}
  2229. @end example
  2230. In this example, if the task fails while attempting to open or read a file,
  2231. the runtime will log the path name that was being read. If the function
  2232. completes normally, the runtime will not log the path.
  2233. A slot that is marked by a @code{note} statement does @emph{not} have its
  2234. value copied aside when control passes through the @code{note}. In other
  2235. words, if a @code{note} statement notes a particular slot, and code after the
  2236. @code{note} that slot, and then a subsequent failure occurs, the
  2237. @emph{mutated} value will be logged during unwinding, @emph{not} the original
  2238. value that was held in the slot at the moment control passed through the
  2239. @code{note} statement.
  2240. @page
  2241. @node Ref.Stmt.While
  2242. @subsection Ref.Stmt.While
  2243. @c * Ref.Stmt.While:: Statement for simple conditional looping.
  2244. A @code{while} statement is a loop construct. A @code{while} loop may be
  2245. either a simple @code{while} or a @code{do}-@code{while} loop.
  2246. In the case of a simple @code{while}, the loop begins by evaluating the
  2247. boolean loop conditional expression. If the loop conditional expression
  2248. evaluates to @code{true}, the loop body block executes and control returns to
  2249. the loop conditional expression. If the loop conditional expression evaluates
  2250. to @code{false}, the @code{while} statement completes.
  2251. In the case of a @code{do}-@code{while}, the loop begins with an execution of
  2252. the loop body. After the loop body executes, it evaluates the loop conditional
  2253. expression. If it evaluates to @code{true}, control returns to the beginning
  2254. of the loop body. If it evaluates to @code{false}, control exits the loop.
  2255. An example of a simple @code{while} statement:
  2256. @example
  2257. while (i < 10) @{
  2258. print("hello\n");
  2259. i = i + 1;
  2260. @}
  2261. @end example
  2262. An example of a @code{do}-@code{while} statement:
  2263. @example
  2264. do @{
  2265. print("hello\n");
  2266. i = i + 1;
  2267. @} while (i < 10);
  2268. @end example
  2269. @page
  2270. @node Ref.Stmt.Break
  2271. @subsection Ref.Stmt.Break
  2272. @c * Ref.Stmt.Break:: Statement for terminating a loop.
  2273. Executing a @code{break} statement immediately terminates the innermost loop
  2274. enclosing it. It is only permitted in the body of a loop.
  2275. @page
  2276. @node Ref.Stmt.Cont
  2277. @subsection Ref.Stmt.Cont
  2278. @c * Ref.Stmt.Cont:: Statement for terminating a single loop iteration.
  2279. Executing a @code{cont} statement immediately terminates the current iteration
  2280. of the innermost loop enclosing it, returning control to the loop
  2281. @emph{head}. In the case of a @code{while} loop, the head is the conditional
  2282. expression controlling the loop. In the case of a @code{for} or @code{for
  2283. each} loop, the head is the iterator or vector-slice increment controlling the
  2284. loop.
  2285. A @code{cont} statement is only permitted in the body of a loop.
  2286. @page
  2287. @node Ref.Stmt.For
  2288. @subsection Ref.Stmt.For
  2289. @c * Ref.Stmt.For:: Statement for looping over strings and vectors.
  2290. A @dfn{for loop} is controlled by a vector or string. The for loop
  2291. bounds-checks the underlying sequence @emph{once} when initiating the loop,
  2292. then repeatedly copies each value of the underlying sequence into the element
  2293. variable, executing the loop body once per copy. To perform a for loop on a
  2294. sub-range of a vector or string, form a temporary slice over the sub-range and
  2295. run the loop over the slice.
  2296. Example of a 4 for loops, all identical:
  2297. @example
  2298. let vec[foo] v = vec(a, b, c);
  2299. for (&foo e in v.(0, _vec.len(v))) @{
  2300. bar(e);
  2301. @}
  2302. for (&foo e in v.(0,)) @{
  2303. bar(e);
  2304. @}
  2305. for (&foo e in v.(,)) @{
  2306. bar(e);
  2307. @}
  2308. for (&foo e in v) @{
  2309. bar(e);
  2310. @}
  2311. @end example
  2312. @page
  2313. @node Ref.Stmt.Foreach
  2314. @subsection Ref.Stmt.Foreach
  2315. @c * Ref.Stmt.Foreach:: Statement for general conditional looping.
  2316. An @dfn{foreach loop} is denoted by the @code{for each} keywords, and is
  2317. controlled by an iterator. The loop executes once for each value @code{put} by
  2318. the iterator. When the iterator returns or fails, the loop terminates.
  2319. Example of a foreach loop:
  2320. @example
  2321. let str txt;
  2322. let vec[str] lines;
  2323. for each (&str s = _str.split(txt, "\n")) @{
  2324. vec.push(lines, s);
  2325. @}
  2326. @end example
  2327. @page
  2328. @node Ref.Stmt.If
  2329. @subsection Ref.Stmt.If
  2330. @c * Ref.Stmt.If:: Statement for simple conditional branching.
  2331. An @code{if} statement is a conditional branch in program control. The form of
  2332. an @code{if} statement is a parenthesized condition expression, followed by a
  2333. consequent block, and an optional trailing @code{else} block. The condition
  2334. expression must have type @code{bool}. If the condition expression evaluates
  2335. to @code{true}, the consequent block is executed and any @code{else} block is
  2336. skipped. If the condition expression evaluates to @code{false}, the consequent
  2337. block is skipped and any @code{else} block is executed.
  2338. @page
  2339. @node Ref.Stmt.Alt
  2340. @subsection Ref.Stmt.Alt
  2341. @c * Ref.Stmt.Alt:: Statement for complex conditional branching.
  2342. An @code{alt} statement is a multi-directional branch in program control.
  2343. There are three kinds of @code{alt} statement: communication @code{alt}
  2344. statements, pattern @code{alt} statements, and @code{alt type} statements.
  2345. The form of each kind of @code{alt} is similar: an initial @emph{head} that
  2346. describes the criteria for branching, followed by a sequence of zero or more
  2347. @emph{arms}, each of which describes a @emph{case} and provides a @emph{block}
  2348. of statements associated with the case. When an @code{alt} is executed,
  2349. control enters the head, determines which of the cases to branch to, branches
  2350. to the block associated with the chosen case, and then proceeds to the
  2351. statement following the @code{alt} when the case block completes.
  2352. @menu
  2353. * Ref.Stmt.Alt.Comm:: Statement for branching on communication events.
  2354. * Ref.Stmt.Alt.Pat:: Statement for branching on pattern matches.
  2355. * Ref.Stmt.Alt.Type:: Statement for branching on types.
  2356. @end menu
  2357. @page
  2358. @node Ref.Stmt.Alt.Comm
  2359. @subsubsection Ref.Stmt.Alt.Comm
  2360. @c * Ref.Stmt.Alt.Comm:: Statement for branching on communication events.
  2361. The simplest form of @code{alt} statement is the a @emph{communication}
  2362. @code{alt}. The cases of a communication @code{alt}'s arms are send, receive
  2363. and flush statements. @xref{Ref.Task.Comm}.
  2364. To execute a communication @code{alt}, the runtime checks all of the ports and
  2365. channels involved in the arms of the statement to see if any @code{case} can
  2366. execute without blocking. If no @code{case} can execute, the task blocks, and
  2367. the runtime unblocks the task when a @code{case} @emph{can} execute. The
  2368. runtime then makes a pseudo-random choice from among the set of @code{case}
  2369. statements that can execute, executes the statement of the @code{case} and
  2370. branches to the block of that arm.
  2371. An example of a communication @code{alt} statement:
  2372. @example
  2373. let chan c[int] = foo();
  2374. let port p[str] = bar();
  2375. let int x = 0;
  2376. let vec[str] strs;
  2377. alt @{
  2378. case (str s <- p) @{
  2379. vec.append(strs, s);
  2380. @}
  2381. case (c <| x) @{
  2382. x++;
  2383. @}
  2384. @}
  2385. @end example
  2386. @page
  2387. @node Ref.Stmt.Alt.Pat
  2388. @subsubsection Ref.Stmt.Alt.Pat
  2389. @c * Ref.Stmt.Alt.Pat:: Statement for branching on pattern matches.
  2390. A pattern @code{alt} statement branches on a @emph{pattern}. The exact form of
  2391. matching that occurs depends on the pattern. Patterns consist of some
  2392. combination of literals, tag constructors, variable binding specifications and
  2393. placeholders (@code{_}). A pattern @code{alt} has a parenthesized @emph{head
  2394. expression}, which is the value to compare to the patterns. The type of the
  2395. patterns must equal the type of the head expression.
  2396. To execute a pattern @code{alt} statement, first the head expression is
  2397. evaluated, then its value is sequentially compared to the patterns in the arms
  2398. until a match is found. The first arm with a matching @code{case} pattern is
  2399. chosen as the branch target of the @code{alt}, any variables bound by the
  2400. pattern are assigned to local @emph{auto} slots in the arm's block, and
  2401. control enters the block.
  2402. An example of a pattern @code{alt} statement:
  2403. @example
  2404. type list[X] = tag(nil, cons(X, @@list[X]));
  2405. let list[int] x = cons(10, cons(11, nil));
  2406. alt (x) @{
  2407. case (cons(a, cons(b, _))) @{
  2408. process_pair(a,b);
  2409. @}
  2410. case (cons(v=10, _)) @{
  2411. process_ten(v);
  2412. @}
  2413. case (_) @{
  2414. fail;
  2415. @}
  2416. @}
  2417. @end example
  2418. @page
  2419. @node Ref.Stmt.Alt.Type
  2420. @subsubsection Ref.Stmt.Alt.Type
  2421. @c * Ref.Stmt.Alt.Type:: Statement for branching on type.
  2422. An @code{alt type} statement is similar to a pattern @code{alt}, but branches
  2423. on the @emph{type} of its head expression, rather than the value. The head
  2424. expression of an @code{alt type} statement must be of type @code{any}, and the
  2425. arms of the statement are slot patterns rather than value patterns. Control
  2426. branches to the arm with a @code{case} that matches the @emph{actual type} of
  2427. the value in the @code{any}.
  2428. An example of an @code{alt type} statement:
  2429. @example
  2430. let any x = foo();
  2431. alt type (x) @{
  2432. case (int i) @{
  2433. ret i;
  2434. @}
  2435. case (list[int] li) @{
  2436. ret int_list_sum(li);
  2437. @}
  2438. case (list[X] lx) @{
  2439. ret list_len(lx);
  2440. @}
  2441. case (_) @{
  2442. ret 0;
  2443. @}
  2444. @}
  2445. @end example
  2446. @page
  2447. @node Ref.Stmt.Prove
  2448. @subsection Ref.Stmt.Prove
  2449. @c * Ref.Stmt.Prove:: Statement for static assertion of typestate.
  2450. A @code{prove} statement has no run-time effect. Its purpose is to statically
  2451. check (and document) that its argument constraint holds at its statement entry
  2452. point. If its argument typestate does not hold, under the typestate algorithm,
  2453. the program containing it will fail to compile.
  2454. @page
  2455. @node Ref.Stmt.Check
  2456. @subsection Ref.Stmt.Check
  2457. @c * Ref.Stmt.Check:: Statement for dynamic assertion of typestate.
  2458. A @code{check} statement connects dynamic assertions made at run-time to the
  2459. static typestate system. A @code{check} statement takes a constraint to check
  2460. at run-time. If the constraint holds at run-time, control passes through the
  2461. @code{check} and on to the next statement in the enclosing block. If the
  2462. condition fails to hold at run-time, the @code{check} statement behaves as a
  2463. @code{fail} statement.
  2464. The typestate algorithm is built around @code{check} statements, and in
  2465. particular the fact that control @emph{will not pass} a check statement with a
  2466. condition that fails to hold. The typestate algorithm can therefore assume
  2467. that the (static) postcondition of a @code{check} statement includes the
  2468. checked constraint itself. From there, the typestate algorithm can perform
  2469. dataflow calculations on subsequent statements, propagating conditions forward
  2470. and statically comparing implied states and their
  2471. specifications. @xref{Ref.Stmt.Stat}.
  2472. @example
  2473. fn even(&int x) -> bool @{
  2474. ret x & 1 == 0;
  2475. @}
  2476. fn print_even(int x) : even(x) @{
  2477. print(x);
  2478. @}
  2479. fn test() @{
  2480. let int y = 8;
  2481. // Cannot call print_even(y) here.
  2482. check even(y);
  2483. // Can call print_even(y) here, since even(y) now holds.
  2484. print_even(y);
  2485. @}
  2486. @end example
  2487. @page
  2488. @node Ref.Stmt.IfCheck
  2489. @subsection Ref.Stmt.IfCheck
  2490. @c * Ref.Stmt.IfCheck:: Statement for dynamic testing of typestate.
  2491. An @code{if check} statement combines a @code{if} statement and a @code{check}
  2492. statement in an indivisible unit that can be used to build more complex
  2493. conditional control flow than the @code{check} statement affords.
  2494. In fact, @code{if check} is a ``more primitive'' statement @code{check};
  2495. instances of the latter can be rewritten as instances of the former. The
  2496. following two examples are equivalent:
  2497. @sp 1
  2498. Example using @code{check}:
  2499. @example
  2500. check even(x);
  2501. print_even(x);
  2502. @end example
  2503. @sp 1
  2504. Equivalent example using @code{if check}:
  2505. @example
  2506. if check even(x) @{
  2507. print_even(x);
  2508. @} else @{
  2509. fail;
  2510. @}
  2511. @end example
  2512. @page
  2513. @node Ref.Run
  2514. @section Ref.Run
  2515. @c * Ref.Run:: Organization of runtime services.
  2516. The Rust @dfn{runtime} is a relatively compact collection of C and Rust code
  2517. that provides fundamental services and datatypes to all Rust tasks at
  2518. run-time. It is smaller and simpler than many modern language runtimes. It is
  2519. tightly integrated into the language's execution model of slots, tasks,
  2520. communication, reflection, logging and signal handling.
  2521. @menu
  2522. * Ref.Run.Mem:: Runtime memory management service.
  2523. * Ref.Run.Type:: Runtime built-in type services.
  2524. * Ref.Run.Comm:: Runtime communication service.
  2525. * Ref.Run.Refl:: Runtime reflection system.
  2526. * Ref.Run.Log:: Runtime logging system.
  2527. * Ref.Run.Sig:: Runtime signal handler.
  2528. @end menu
  2529. @page
  2530. @node Ref.Run.Mem
  2531. @subsection Ref.Run.Mem
  2532. @c * Ref.Run.Mem:: Runtime memory management service.
  2533. The runtime memory-management system is based on a @emph{service-provider
  2534. interface}, through which the runtime requests blocks of memory from its
  2535. environment and releases them back to its environment when they are no longer
  2536. in use. The default implementation of the service-provider interface consists
  2537. of the C runtime functions @code{malloc} and @code{free}.
  2538. The runtime memory-management system in turn supplies Rust tasks with
  2539. facilities for allocating, extending and releasing stacks, as well as
  2540. allocating and freeing exterior values.
  2541. @page
  2542. @node Ref.Run.Type
  2543. @subsection Ref.Run.Type
  2544. @c * Ref.Run.Mem:: Runtime built-in type services.
  2545. The runtime provides C and Rust code to manage several built-in types:
  2546. @itemize
  2547. @item @code{vec}, the type of vectors.
  2548. @item @code{str}, the type of UTF-8 strings.
  2549. @item @code{big}, the type of arbitrary-precision integers.
  2550. @item @code{chan}, the type of communication channels.
  2551. @item @code{port}, the type of communication ports.
  2552. @item @code{task}, the type of tasks.
  2553. @end itemize
  2554. Support for other built-in types such as simple types, tuples,
  2555. records, and tags is open-coded by the Rust compiler.
  2556. @page
  2557. @node Ref.Run.Comm
  2558. @subsection Ref.Run.Comm
  2559. @c * Ref.Run.Comm:: Runtime communication service.
  2560. The runtime provides code to manage inter-task communication. This includes
  2561. the system of task-lifecycle state transitions depending on the contents of
  2562. queues, as well as code to copy values between queues and their recipients and
  2563. to serialize values for transmission over operating-system inter-process
  2564. communication facilities.
  2565. @page
  2566. @node Ref.Run.Refl
  2567. @subsection Ref.Run.Refl
  2568. @c * Ref.Run.Refl:: Runtime reflection system.
  2569. The runtime reflection system is driven by the DWARF tables emitted into a
  2570. crate at compile-time. Reflecting on a slot or item allocates a Rust data
  2571. structure corresponding to the DWARF DIE for that slot or item.
  2572. @page
  2573. @node Ref.Run.Log
  2574. @subsection Ref.Run.Log
  2575. @c * Ref.Run.Log:: Runtime logging system.
  2576. The runtime contains a system for directing logging statements to a logging
  2577. console and/or internal logging buffers. @xref{Ref.Stmt.Log}. Logging
  2578. statements can be enabled or disabled via a two-dimensional filtering process:
  2579. @itemize
  2580. @sp 1
  2581. @item
  2582. By Item
  2583. Each @emph{item} (module, function, iterator, object, type) in Rust has a
  2584. static name-path within its crate module, and can have logging enabled or
  2585. disabled on a name-path-prefix basis.
  2586. @sp 1
  2587. @item
  2588. By Task
  2589. Each @emph{task} in a running Rust program has a unique ownership-path through
  2590. the task ownership tree, and can have logging enabled or disabled on an
  2591. ownership-path-prefix basis.
  2592. @end itemize
  2593. Logging is integrated into the language for efficiency reasons, as well as the
  2594. need to filter logs based on these two built-in dimensions.
  2595. @page
  2596. @node Ref.Run.Sig
  2597. @subsection Ref.Run.Sig
  2598. @c * Ref.Run.Sig:: Runtime signal handler.
  2599. The runtime signal-handling system is driven by a signal-dispatch table and a
  2600. signal queue associated with each task. Sending a signal to a task inserts the
  2601. signal into the task's signal queue and marks the task as having a pending
  2602. signal. At the next scheduling opportunity, the runtime processes signals in
  2603. the task's queue using its dispatch table. The signal queue memory is charged
  2604. to the task's domain; if the queue grows too big, the task will fail.
  2605. @c ############################################################
  2606. @c end main body of nodes
  2607. @c ############################################################
  2608. @page
  2609. @node Index
  2610. @chapter Index
  2611. @printindex cp
  2612. @bye