diff --git a/src/basic-syntax/compound-types.md b/src/basic-syntax/compound-types.md
index 9b66f5920b26..5502fabe4533 100644
--- a/src/basic-syntax/compound-types.md
+++ b/src/basic-syntax/compound-types.md
@@ -7,6 +7,7 @@
Array assignment and access:
+
```rust,editable
fn main() {
let mut a: [i8; 10] = [42; 10];
@@ -17,11 +18,12 @@ fn main() {
Tuple assignment and access:
+
```rust,editable
fn main() {
let t: (i8, bool) = (7, true);
- println!("1st index: {}", t.0);
- println!("2nd index: {}", t.1);
+ println!("t.0: {}", t.0);
+ println!("t.1: {}", t.1);
}
```
diff --git a/src/basic-syntax/functions.md b/src/basic-syntax/functions.md
index 3858f5e92a78..603c9fb56f16 100644
--- a/src/basic-syntax/functions.md
+++ b/src/basic-syntax/functions.md
@@ -2,6 +2,7 @@
A Rust version of the famous [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) interview question:
+
```rust,editable
fn main() {
print_fizzbuzz_to(20);
diff --git a/src/basic-syntax/references-dangling.md b/src/basic-syntax/references-dangling.md
index deb8c18b8a78..a6115c1b90c4 100644
--- a/src/basic-syntax/references-dangling.md
+++ b/src/basic-syntax/references-dangling.md
@@ -2,6 +2,7 @@
Rust will statically forbid dangling references:
+
```rust,editable,compile_fail
fn main() {
let ref_x: &i32;
diff --git a/src/basic-syntax/references.md b/src/basic-syntax/references.md
index b03fd6316f4f..727dedec40fc 100644
--- a/src/basic-syntax/references.md
+++ b/src/basic-syntax/references.md
@@ -2,6 +2,7 @@
Like C++, Rust has references:
+
```rust,editable
fn main() {
let mut x: i32 = 10;
diff --git a/src/basic-syntax/scalar-types.md b/src/basic-syntax/scalar-types.md
index 059de2bc40e4..65bc2f45881f 100644
--- a/src/basic-syntax/scalar-types.md
+++ b/src/basic-syntax/scalar-types.md
@@ -24,6 +24,7 @@ There are a few syntaxes which are not shown above:
== "\\\\n"`. You can embed double-quotes by using an equal amount of `#` on
either side of the quotes:
+
```rust,editable
fn main() {
println!(r#"link"#);
@@ -33,6 +34,7 @@ There are a few syntaxes which are not shown above:
- Byte strings allow you to create a `&[u8]` value directly:
+
```rust,editable
fn main() {
println!("{:?}", b"abc");
diff --git a/src/basic-syntax/scopes-shadowing.md b/src/basic-syntax/scopes-shadowing.md
index 2009178b11e2..ba9c7e3a3147 100644
--- a/src/basic-syntax/scopes-shadowing.md
+++ b/src/basic-syntax/scopes-shadowing.md
@@ -27,6 +27,7 @@ fn main() {
* Shadowing looks obscure at first, but is convenient for holding on to values after `.unwrap()`.
* The following code demonstrates why the compiler can't simply reuse memory locations when shadowing an immutable variable in a scope, even if the type does not change.
+
```rust,editable
fn main() {
let a = 1;
diff --git a/src/basic-syntax/slices.md b/src/basic-syntax/slices.md
index edab4573bb78..48d63d6c3eb8 100644
--- a/src/basic-syntax/slices.md
+++ b/src/basic-syntax/slices.md
@@ -2,6 +2,7 @@
A slice gives you a view into a larger collection:
+
```rust,editable
fn main() {
let mut a: [i32; 6] = [10, 20, 30, 40, 50, 60];
diff --git a/src/basic-syntax/static-and-const.md b/src/basic-syntax/static-and-const.md
index 2d437302d22b..2fbd3f5c0bda 100644
--- a/src/basic-syntax/static-and-const.md
+++ b/src/basic-syntax/static-and-const.md
@@ -8,6 +8,7 @@ cannot be moved or reallocated during the execution of the program.
Constant variables are evaluated at compile time and their values are inlined
wherever they are used:
+
```rust,editable
const DIGEST_SIZE: usize = 3;
const ZERO: Option = Some(42);
@@ -22,7 +23,7 @@ fn compute_digest(text: &str) -> [u8; DIGEST_SIZE] {
fn main() {
let digest = compute_digest("Hello");
- println!("Digest: {digest:?}");
+ println!("digest: {digest:?}");
}
```
diff --git a/src/basic-syntax/type-inference.md b/src/basic-syntax/type-inference.md
index 526e3b21a661..30fa3566d5b0 100644
--- a/src/basic-syntax/type-inference.md
+++ b/src/basic-syntax/type-inference.md
@@ -2,6 +2,7 @@
Rust will look at how the variable is _used_ to determine the type:
+
```rust,editable
fn takes_u32(x: u32) {
println!("u32: {x}");
@@ -31,6 +32,7 @@ The compiler does the job for us and helps us write more concise code.
The following code tells the compiler to copy into a certain generic container without the code ever explicitly specifying the contained type, using `_` as a placeholder:
+
```rust,editable
fn main() {
let mut v = Vec::new();
diff --git a/src/basic-syntax/variables.md b/src/basic-syntax/variables.md
index 82ec653a0529..f2fd2f7ebb63 100644
--- a/src/basic-syntax/variables.md
+++ b/src/basic-syntax/variables.md
@@ -3,6 +3,7 @@
Rust provides type safety via static typing. Variable bindings are immutable by
default:
+
```rust,editable
fn main() {
let x: i32 = 10;
diff --git a/src/control-flow/blocks.md b/src/control-flow/blocks.md
index c734837d7d9b..c9a19d0c3cf3 100644
--- a/src/control-flow/blocks.md
+++ b/src/control-flow/blocks.md
@@ -4,6 +4,7 @@ A block in Rust contains a sequence of expressions.
Each block has a value and a type,
which are those of the last expression of the block:
+
```rust,editable
fn main() {
let x = {
@@ -28,13 +29,14 @@ If the last expression ends with `;`, then the resulting value and type is `()`.
The same rule is used for functions: the value of the function body is the
return value:
+
```rust,editable
fn double(x: i32) -> i32 {
x + x
}
fn main() {
- println!("doubled: {}", double(7));
+ println!("double: {}", double(7));
}
```
diff --git a/src/control-flow/break-continue.md b/src/control-flow/break-continue.md
index aac48a4ca05d..1b562dc3f0ac 100644
--- a/src/control-flow/break-continue.md
+++ b/src/control-flow/break-continue.md
@@ -7,6 +7,7 @@ the next iteration use [`continue`](https://doc.rust-lang.org/reference/expressi
Both `continue` and `break` can optionally take a label argument which is used
to break out of nested loops:
+
```rust,editable
fn main() {
let v = vec![10, 20, 30];
diff --git a/src/control-flow/for-expressions.md b/src/control-flow/for-expressions.md
index e6ae73eae743..61a42a2f4bfa 100644
--- a/src/control-flow/for-expressions.md
+++ b/src/control-flow/for-expressions.md
@@ -4,6 +4,7 @@ The [`for` loop](https://doc.rust-lang.org/std/keyword.for.html) is closely
related to the [`while let` loop](while-let-expressions.md). It will
automatically call `into_iter()` on the expression and then iterate over it:
+
```rust,editable
fn main() {
let v = vec![10, 20, 30];
diff --git a/src/control-flow/if-let-expressions.md b/src/control-flow/if-let-expressions.md
index c84cc38c21cb..a03a6207dc8c 100644
--- a/src/control-flow/if-let-expressions.md
+++ b/src/control-flow/if-let-expressions.md
@@ -25,6 +25,7 @@ Rust.
* Unlike `match`, `if let` does not support guard clauses for pattern matching.
* Since 1.65, a similar [let-else](https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html) construct allows to do a destructuring assignment, or if it fails, execute a block which is required to abort normal control flow (with `panic`/`return`/`break`/`continue`):
+
```rust,editable
fn main() {
println!("{:?}", second_word_to_upper("foo bar"));
diff --git a/src/control-flow/loop-expressions.md b/src/control-flow/loop-expressions.md
index faaecdb4f951..3a92b1dd6d06 100644
--- a/src/control-flow/loop-expressions.md
+++ b/src/control-flow/loop-expressions.md
@@ -5,6 +5,7 @@ which creates an endless loop.
Here you must either `break` or `return` to stop the loop:
+
```rust,editable
fn main() {
let mut x = 10;
@@ -18,7 +19,7 @@ fn main() {
break;
}
}
- println!("Final x: {x}");
+ println!("x: {x}");
}
```
diff --git a/src/control-flow/while-expressions.md b/src/control-flow/while-expressions.md
index 1ad351e4cbc0..fc3f267f9606 100644
--- a/src/control-flow/while-expressions.md
+++ b/src/control-flow/while-expressions.md
@@ -3,6 +3,7 @@
The [`while` keyword](https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-loops)
works very similar to other languages:
+
```rust,editable
fn main() {
let mut x = 10;
@@ -13,7 +14,7 @@ fn main() {
3 * x + 1
};
}
- println!("Final x: {x}");
+ println!("x: {x}");
}
```
diff --git a/src/control-flow/while-let-expressions.md b/src/control-flow/while-let-expressions.md
index a93883c9b134..98b17687197c 100644
--- a/src/control-flow/while-let-expressions.md
+++ b/src/control-flow/while-let-expressions.md
@@ -3,6 +3,7 @@
Like with `if let`, there is a [`while let`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-pattern-loops)
variant which repeatedly tests a value against a pattern:
+
```rust,editable
fn main() {
let v = vec![10, 20, 30];
diff --git a/src/enums/sizes.md b/src/enums/sizes.md
index 1c94b0bd68f4..ae04c08af85b 100644
--- a/src/enums/sizes.md
+++ b/src/enums/sizes.md
@@ -31,6 +31,7 @@ Key Points:
* You can control the discriminant if needed (e.g., for compatibility with C):
+
```rust,editable
#[repr(u32)]
enum Bar {
@@ -67,6 +68,7 @@ Key Points:
Example code if you want to show how the bitwise representation *may* look like in practice.
It's important to note that the compiler provides no guarantees regarding this representation, therefore this is totally unsafe.
+
```rust,editable
use std::mem::transmute;
@@ -77,25 +79,23 @@ Key Points:
}
fn main() {
- // TOTALLY UNSAFE. Rust provides no guarantees about the bitwise
- // representation of types.
unsafe {
- println!("Bitwise representation of bool");
+ println!("bool:");
dbg_bits!(false, u8);
dbg_bits!(true, u8);
- println!("Bitwise representation of Option");
+ println!("Option:");
dbg_bits!(None::, u8);
dbg_bits!(Some(false), u8);
dbg_bits!(Some(true), u8);
- println!("Bitwise representation of Option