-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrust.html
More file actions
482 lines (477 loc) · 27.9 KB
/
rust.html
File metadata and controls
482 lines (477 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Codebraid with Rust</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; }
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.at { color: #7d9029; }
code span.bn { color: #40a070; }
code span.bu { color: #008000; }
code span.cf { color: #007020; font-weight: bold; }
code span.ch { color: #4070a0; }
code span.cn { color: #880000; }
code span.co { color: #60a0b0; font-style: italic; }
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.do { color: #ba2121; font-style: italic; }
code span.dt { color: #902000; }
code span.dv { color: #40a070; }
code span.er { color: #ff0000; font-weight: bold; }
code span.ex { }
code span.fl { color: #40a070; }
code span.fu { color: #06287e; }
code span.im { color: #008000; font-weight: bold; }
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.kw { color: #007020; font-weight: bold; }
code span.op { color: #666666; }
code span.ot { color: #007020; }
code span.pp { color: #bc7a00; }
code span.sc { color: #4070a0; }
code span.ss { color: #bb6688; }
code span.st { color: #4070a0; }
code span.va { color: #19177c; }
code span.vs { color: #4070a0; }
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
</style>
<style type="text/css">body {display: block;text-align: left;max-width: 40rem;padding-left: 0.5rem;padding-right: 0.5rem;margin: auto;}pre.numberSource a.sourceLine {position: relative;left: -3em;}pre.numberSource a.sourceLine::before{content: attr(title);position: relative;left: -1em;text-align: right;vertical-align: baseline;border: none;pointer-events: all;display: inline-block;-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;padding: 0 4px;width: 3em;color: #aaaaaa;}pre.numberSource {margin-left: 2em;border-left: 1px solid #aaaaaa;padding-left: 4px;}div.exampleMarkup:before{content: "Markdown";font-family: monospace;display: block;height: 2em;font-size: 150%;}div.exampleMarkup {padding-top: 0.5em;padding-left: 0.5em;padding-right: 0.5em;margin-top: 2em;margin-bottom: 1em;background-color: honeydew;border-style: solid;border-width: 1px;border-color: limegreen;border-radius: 0.2em;}div.exampleOutput:before{content: "Output";font-family: monospace;display: block;font-size: 150%;}div.exampleOutput {color: black;padding-top: 0.5em;padding-left: 0.5em;padding-right: 0.5em;margin-top: 1em;margin-bottom: 2em;background-color: whitesmoke;border-style: solid;border-width: 1px;border-color: lightgray;border-radius: 0.2em;}div.sourceCode {padding: 0.5em;background-color: aliceblue;border-style: solid;border-width: 1px;border-color: lightskyblue;border-radius: 0.2em;}div.exampleOutput div.sourceCode:before{content: "code";font-family: monospace;display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.stdout:before{content: "stdout";display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.stdout {color: black;padding: 0.5em;background-color: azure;border-style: solid;border-width: 1px;border-color: turquoise;border-radius: 0.2em;padding-left: 0.5em;}code.stdout {color: black;background-color: azure;border-style: solid;border-width: 1px;border-color: turquoise;}pre.stderr:before{content: "stderr";display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.stderr {color: red;padding: 0.5em;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;border-radius: 0.2em;padding-left: 0.5em;}code.stderr {color: red;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;}pre.error:before{content: "Error";display: block;height: 2em;font-weight: bold;font-size: 120%;}pre.error {color: red;padding: 0.5em;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;border-radius: 0.2em;padding-left: 0.5em;}code.error {color: red;background-color: lavenderblush;border-style: solid;border-width: 1px;border-color: pink;}</style>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title">Codebraid with Rust</h1>
</header>
<h2 id="introduction">Introduction</h2>
<p>Codebraid with Rust is slightly different than using some other
languages. With a language like Python or Julia, it makes sense that you
can just run the code in code blocks or inline code. But with Rust, what
about <code>main()</code>?</p>
<p>By default, all executed code is inserted into a <code>main()</code>
template like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="at">#![</span>allow<span class="at">(</span>unused<span class="at">)]</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">use</span> <span class="pp">std::fmt::</span><span class="bu">Write</span> <span class="kw">as</span> FmtWrite<span class="op">;</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">use</span> <span class="pp">std::io::</span><span class="bu">Write</span> <span class="kw">as</span> IoWrite<span class="op">;</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="op"><</span>code<span class="op">></span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<p>The template allows you to start running code without explicitly
defining <code>main()</code>. In some situations, though, it will be
useful to define <code>main()</code> explicitly or otherwise have
complete control over what is executed. For those situations, see <a href="#advanced">Advanced features</a>.</p>
<h2 id="inline-code">Inline code</h2>
<h3 id="run">Run</h3>
<p>Inline code with <code>.cb-run</code> gives raw stdout.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`println!("{}", 1 + 2);`{.rust .cb-run}</code></pre>
</div>
<div class="exampleOutput">
<p>3</p>
</div>
</div>
<h3 id="expression-and-inline-notebook">Expression and inline
notebook</h3>
<p>Inline code with <code>.cb-expr</code> evaluates an expression and
then inserts the raw output into the document, where it is interpreted
as Markdown. Inline code with <code>.cb-nb</code> (<code>nb</code> is
short for <code>notebook</code>) is similar, except output is shown
verbatim. Notice that since these are expressions, a trailing semicolon
must not be used. This is only compatible with expressions that return
Rust types implementing the <code>Display</code> trait.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`format!("*{}*", (1..1000).sum::<i32>())`{.rust .cb-expr}</code></pre>
</div>
<div class="exampleOutput">
<p><em>499500</em></p>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>`(1..1000).map(|x| x*x).sum::<i32>()`{.rust .cb-nb}</code></pre>
</div>
<div class="exampleOutput">
<p><code class="expr">332833500</code></p>
</div>
</div>
<p>If you need to work with an expression that returns a type without
the <code>Display</code> trait, simply use <code>format!</code> with the
<code>Debug</code> trait (<code>"{:?}"</code>, or <code>"{:#?}"</code>
for pretty-print) to convert to a string manually. Since the string
implements <code>Display</code>, everything works:</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`format!("{:?}", ())`{.rust .cb-expr}</code></pre>
</div>
<div class="exampleOutput">
<p>()</p>
</div>
</div>
<h3 id="stderr">Stderr</h3>
<p>In the event of a compilation error, inline code automatically shows
stderr by default. This code is compiled in its own session,
<code>inline_error</code>, so that it does not impact other
examples.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`1 + "a";`{.rust .cb-run session=inline_error}</code></pre>
</div>
<div class="exampleOutput">
<p><code class="stderr error">error[E0277]: cannot add `&str` to `{integer}` --> <string>:1:7 | 1 | 1 + "a"; | ^ no implementation for `{integer} + &str` | = help: the trait `Add<&str>` is not implemented for `{integer}` = help: the following other types implement trait `Add<Rhs>`: <isize as Add> <isize as Add<&isize>> <i8 as Add> <i8 as Add<&i8>> <i16 as Add> <i16 as Add<&i16>> <i32 as Add> <i32 as Add<&i32>> and 48 others error: aborting due to previous error For more information about this error, try `rustc --explain E0277`.</code></p>
</div>
</div>
<h3 id="source-errors">Source errors</h3>
<p>A message is also displayed for errors in the Markdown source. This
usually includes the name of the document source and the approximate
line number.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>`println!("{}", 1 + 2);`{.rus .cb-run session=inline_source_error}</code></pre>
</div>
<div class="exampleOutput">
<p><code class="error sysConfigError">SYS CONFIG ERROR in "rust.cbmd" near line 73: Language definition for "rus" does not exist</code></p>
</div>
</div>
<h2 id="block-code">Block code</h2>
<h3 id="run-1">Run</h3>
<p>Code blocks with <code>.cb-run</code> give raw stdout. There is
continuity between code blocks so long as they are in the same session;
variables persist.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-run session=hello}
let x = "Hello from *Rust!*";
```</code></pre>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-run session=hello}
println!("{}", x);
```</code></pre>
</div>
<div class="exampleOutput">
<p>Hello from <em>Rust!</em></p>
</div>
</div>
<h3 id="notebook">Notebook</h3>
<p>Code blocks with <code>.cb-nb</code> show the code and also the
verbatim stdout.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb session=loop}
fn pows_of_two(start: u32, end: u32) {
let n: i32 = 2;
for x in start..end {
if x == end - 1 {
println!("{}", n.pow(x));
}
else {
print!("{}, ", n.pow(x));
}
}
}
pows_of_two(1, 9);
pows_of_two(1, 17);
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb11" data-startFrom="1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb11-1"><a href="#cb11-1"></a><span class="kw">fn</span> pows_of_two(start<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> end<span class="op">:</span> <span class="dt">u32</span>) <span class="op">{</span></span>
<span id="cb11-2"><a href="#cb11-2"></a> <span class="kw">let</span> n<span class="op">:</span> <span class="dt">i32</span> <span class="op">=</span> <span class="dv">2</span><span class="op">;</span></span>
<span id="cb11-3"><a href="#cb11-3"></a> <span class="cf">for</span> x <span class="kw">in</span> start<span class="op">..</span>end <span class="op">{</span></span>
<span id="cb11-4"><a href="#cb11-4"></a> <span class="cf">if</span> x <span class="op">==</span> end <span class="op">-</span> <span class="dv">1</span> <span class="op">{</span></span>
<span id="cb11-5"><a href="#cb11-5"></a> <span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> n<span class="op">.</span>pow(x))<span class="op">;</span></span>
<span id="cb11-6"><a href="#cb11-6"></a> <span class="op">}</span></span>
<span id="cb11-7"><a href="#cb11-7"></a> <span class="cf">else</span> <span class="op">{</span></span>
<span id="cb11-8"><a href="#cb11-8"></a> <span class="pp">print!</span>(<span class="st">"{}, "</span><span class="op">,</span> n<span class="op">.</span>pow(x))<span class="op">;</span></span>
<span id="cb11-9"><a href="#cb11-9"></a> <span class="op">}</span></span>
<span id="cb11-10"><a href="#cb11-10"></a> <span class="op">}</span></span>
<span id="cb11-11"><a href="#cb11-11"></a><span class="op">}</span></span>
<span id="cb11-12"><a href="#cb11-12"></a>pows_of_two(<span class="dv">1</span><span class="op">,</span> <span class="dv">9</span>)<span class="op">;</span></span>
<span id="cb11-13"><a href="#cb11-13"></a>pows_of_two(<span class="dv">1</span><span class="op">,</span> <span class="dv">17</span>)<span class="op">;</span></span></code></pre></div>
<pre class="stdout"><code>2, 4, 8, 16, 32, 64, 128, 256
2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536</code></pre>
</div>
</div>
<h3 id="stderr-1">Stderr</h3>
<p>Code blocks automatically show compilation errors by default. Errors
are synchronized with code blocks so that they appear next to the
correct code blocks.</p>
<p>The first code block in this session is valid, so no compilation
error is shown.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb session=block_error}
let number = 123;
let letter = "a";
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb14" data-startFrom="1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb14-1"><a href="#cb14-1"></a><span class="kw">let</span> number <span class="op">=</span> <span class="dv">123</span><span class="op">;</span></span>
<span id="cb14-2"><a href="#cb14-2"></a><span class="kw">let</span> letter <span class="op">=</span> <span class="st">"a"</span><span class="op">;</span></span></code></pre></div>
</div>
</div>
<p>The next code block in this sesssion produces a compilation error.
The error message appears automatically, with line numbers that
correctly correspond to the code. This last point is important. Remember
that by default, Codebraid inserts Rust code into an implicit
<code>main()</code> function, so the line numbers of what is compiled
typically do not correspond to those of the code entered by the
user.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb session=block_error}
number += 1;
number += letter;
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb16" data-startFrom="3"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust" style="counter-reset: source-line 2;"><span id="cb16-3"><a href="#cb16-3"></a>number <span class="op">+=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb16-4"><a href="#cb16-4"></a>number <span class="op">+=</span> letter<span class="op">;</span></span></code></pre></div>
<pre class="stderr error"><code>error[E0277]: cannot add-assign `&str` to `{integer}`
--> source.rs:4:12
|
4 | number += letter;
| ^^ no implementation for `{integer} += &str`
|
= help: the trait `AddAssign<&str>` is not implemented for `{integer}`
= help: the following other types implement trait `AddAssign<Rhs>`:
<isize as AddAssign>
<isize as AddAssign<&isize>>
<i8 as AddAssign>
<i8 as AddAssign<&i8>>
<i16 as AddAssign>
<i16 as AddAssign<&i16>>
<i32 as AddAssign>
<i32 as AddAssign<&i32>>
and 20 others
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.</code></pre>
</div>
</div>
<h3 id="source-errors-1">Source errors</h3>
<p>A message is also displayed for errors in the Markdown source. This
usually includes the name of the document source and the approximate
line number.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-ruuun session=block_source_error}
println!("{}", 1 + 2);
```</code></pre>
</div>
<div class="exampleOutput">
<pre class="error sourceError"><code>SOURCE ERROR in "rust.cbmd" near line 151:
Unknown or unsupported Codebraid command "cb-ruuun"
SOURCE ERROR in "rust.cbmd" near line 151:
Missing valid Codebraid command</code></pre>
</div>
</div>
<h2 id="other-options">Other options</h2>
<p>By default, stdout and stderr are only shown if they are non-empty.
In some situations, it may be useful to represent empty output visually
as confirmation that there indeed was none.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-run show=code+stdout+stderr:verbatim_or_empty}
let x = 1 + 2;
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb21" data-startFrom="1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb21-1"><a href="#cb21-1"></a><span class="kw">let</span> x <span class="op">=</span> <span class="dv">1</span> <span class="op">+</span> <span class="dv">2</span><span class="op">;</span></span></code></pre></div>
<pre class="stderr"><code> </code></pre>
</div>
</div>
<p>It is also possible to selectively hide output from a code chunk.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb hide=stdout}
println!("{}", x);
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb24" data-startFrom="2"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust" style="counter-reset: source-line 1;"><span id="cb24-2"><a href="#cb24-2"></a><span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> x)<span class="op">;</span></span></code></pre></div>
</div>
</div>
<p><code>hide</code> takes any combination of <code>code</code>,
<code>stderr</code>, and <code>stdout</code>, or simply
<code>all</code>.</p>
<h2 id="advanced">Advanced features</h2>
<h3 id="outside_main"><code>outside_main</code></h3>
<p>By default, all executed code is inserted into a <code>main()</code>
template. It is possible to create your own <code>main()</code> or
functions outside <code>main()</code> using the code chunk keyword
<code>outside_main</code>. If a session <em>starts</em> with one or more
code chunks with <code>outside_main=true</code>, these are used instead
of the beginning of the <code>main()</code> template. Similarly, if a
session <em>ends</em> with one or more code chunks with
<code>outside_main=true</code>, these are used instead of the end of the
<code>main()</code> template. If there are any code chunks in between
that lack <code>outside_main</code> (that is, default
<code>outside_main=false</code>), then these will have their stdout
collected on a per-chunk basis like normal. Having code chunks that lack
<code>outside_main</code> is not required; if there are none, the total
accumulated stdout for a session belongs to the last code chunk in the
session and can be controlled by modifying the <code>show</code>
settings for that chunk.</p>
<p>For example, it is possible to overwrite the <code>main()</code>
template with a single, self-contained code block. This is useful when
the code is short enough that splitting it up into separate chunks, each
with its associated stdout, is not necessary.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb outside_main=true session=no_main_template_single_chunk}
fn main() {
use std::fmt::Write as FmtWrite;
use std::io::Write as IoWrite;
println!("Hello from Rust!");
}
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb26" data-startFrom="1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb26-1"><a href="#cb26-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span>
<span id="cb26-2"><a href="#cb26-2"></a> <span class="kw">use</span> <span class="pp">std::fmt::</span><span class="bu">Write</span> <span class="kw">as</span> FmtWrite<span class="op">;</span></span>
<span id="cb26-3"><a href="#cb26-3"></a> <span class="kw">use</span> <span class="pp">std::io::</span><span class="bu">Write</span> <span class="kw">as</span> IoWrite<span class="op">;</span></span>
<span id="cb26-4"><a href="#cb26-4"></a> <span class="pp">println!</span>(<span class="st">"Hello from Rust!"</span>)<span class="op">;</span></span>
<span id="cb26-5"><a href="#cb26-5"></a><span class="op">}</span></span></code></pre></div>
<pre class="stdout"><code>Hello from Rust!</code></pre>
</div>
</div>
<p>Here is the same example, but broken up into multiple code chunks so
that the stdout is more closely associated with the code that produced
it.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb outside_main=true session=no_main_template}
fn main() {
use std::fmt::Write as FmtWrite;
use std::io::Write as IoWrite;
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb29" data-startFrom="1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb29-1"><a href="#cb29-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span>
<span id="cb29-2"><a href="#cb29-2"></a> <span class="kw">use</span> <span class="pp">std::fmt::</span><span class="bu">Write</span> <span class="kw">as</span> FmtWrite<span class="op">;</span></span>
<span id="cb29-3"><a href="#cb29-3"></a> <span class="kw">use</span> <span class="pp">std::io::</span><span class="bu">Write</span> <span class="kw">as</span> IoWrite<span class="op">;</span></span></code></pre></div>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb session=no_main_template}
println!("Hello from Rust!");
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb31" data-startFrom="4"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust" style="counter-reset: source-line 3;"><span id="cb31-4"><a href="#cb31-4"></a> <span class="pp">println!</span>(<span class="st">"Hello from Rust!"</span>)<span class="op">;</span></span></code></pre></div>
<pre class="stdout"><code>Hello from Rust!</code></pre>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb outside_main=true session=no_main_template}
}
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb34" data-startFrom="5"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust" style="counter-reset: source-line 4;"><span id="cb34-5"><a href="#cb34-5"></a><span class="op">}</span></span></code></pre></div>
</div>
</div>
<h3 id="complete"><code>complete</code></h3>
<p>By default, a code chunk must contain a complete unit of code. A
function definition, loop, or expression cannot be split between
multiple chunks (with the exception of code chunks with
<code>outside_main=True</code>). The code chunk keyword
<code>complete</code> allows code chunks that do not contain a complete
unit of code.</p>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb session=split complete=false}
fn pows_of_two(start: u32, end: u32) {
let n: i32 = 2;
for x in start..end {
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb36" data-startFrom="1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb36-1"><a href="#cb36-1"></a><span class="kw">fn</span> pows_of_two(start<span class="op">:</span> <span class="dt">u32</span><span class="op">,</span> end<span class="op">:</span> <span class="dt">u32</span>) <span class="op">{</span></span>
<span id="cb36-2"><a href="#cb36-2"></a> <span class="kw">let</span> n<span class="op">:</span> <span class="dt">i32</span> <span class="op">=</span> <span class="dv">2</span><span class="op">;</span></span>
<span id="cb36-3"><a href="#cb36-3"></a> <span class="cf">for</span> x <span class="kw">in</span> start<span class="op">..</span>end <span class="op">{</span></span></code></pre></div>
</div>
</div>
<div class="example">
<div class="exampleMarkup">
<pre><code>```{.rust .cb-nb session=split}
if x == end - 1 {
println!("{}", n.pow(x));
}
else {
print!("{}, ", n.pow(x));
}
}
}
pows_of_two(1, 9);
```</code></pre>
</div>
<div class="exampleOutput">
<div class="sourceCode" id="cb38" data-startFrom="4"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust" style="counter-reset: source-line 3;"><span id="cb38-4"><a href="#cb38-4"></a> <span class="cf">if</span> x <span class="op">==</span> end <span class="op">-</span> <span class="dv">1</span> <span class="op">{</span></span>
<span id="cb38-5"><a href="#cb38-5"></a> <span class="pp">println!</span>(<span class="st">"{}"</span><span class="op">,</span> n<span class="op">.</span>pow(x))<span class="op">;</span></span>
<span id="cb38-6"><a href="#cb38-6"></a> <span class="op">}</span></span>
<span id="cb38-7"><a href="#cb38-7"></a> <span class="cf">else</span> <span class="op">{</span></span>
<span id="cb38-8"><a href="#cb38-8"></a> <span class="pp">print!</span>(<span class="st">"{}, "</span><span class="op">,</span> n<span class="op">.</span>pow(x))<span class="op">;</span></span>
<span id="cb38-9"><a href="#cb38-9"></a> <span class="op">}</span></span>
<span id="cb38-10"><a href="#cb38-10"></a> <span class="op">}</span></span>
<span id="cb38-11"><a href="#cb38-11"></a><span class="op">}</span></span>
<span id="cb38-12"><a href="#cb38-12"></a>pows_of_two(<span class="dv">1</span><span class="op">,</span> <span class="dv">9</span>)<span class="op">;</span></span></code></pre></div>
<pre class="stdout"><code>2, 4, 8, 16, 32, 64, 128, 256</code></pre>
</div>
</div>
</body>
</html>