Skip to content

Convert all inline literals to the "rust" role #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Rust is a new programming language which had its `1.0 release in

- Rust is a statically compiled language in a similar role as C++

- ``rustc`` uses LLVM as its backend.
- :command:`rustc` uses LLVM as its backend.

- Rust supports many `platforms and
architectures <https://doc.rust-lang.org/nightly/rustc/platform-support.html>`__:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Some unique selling points of Rust:
- No uninitialized variables.
- No double-frees.
- No use-after-free.
- No ``NULL`` pointers.
- No :rust:`NULL` pointers.
- No forgotten locked mutexes.
- No data races between threads.
- No iterator invalidation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ way to run short Rust programs, and is the basis for the examples and
exercises in this course. Try running the "hello-world" program it
starts with. It comes with a few handy features:

- Under "Tools", use the ``rustfmt`` option to format your code in the
- Under "Tools", use the :rust:`rustfmt` option to format your code in the
"standard" way.

- Rust has two main "profiles" for generating code: Debug (extra
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ Hello, World
Let us jump into the simplest possible Rust program, a classic Hello
World program:

.. code:: rust,editable
.. code:: rust

fn main() {
println!("Hello World!");
}

What you see:

- Functions are introduced with ``fn``.
- Functions are introduced with :rust:`fn`.
- Blocks are delimited by curly braces like in C and C++.
- The ``main`` function is the entry point of the program.
- Rust has hygienic macros, ``println!`` is an example of this.
- The :rust:`main` function is the entry point of the program.
- Rust has hygienic macros, :rust:`println!` is an example of this.
- Rust strings are UTF-8 encoded and can contain any Unicode character.

.. raw:: html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Variables
-----------

Rust provides type safety via static typing. Variable bindings are made
with ``let``:
with :rust:`let`:

.. code:: rust,editable,warnunused
.. code:: rust

fn main() {
let x: i32 = 10;
Expand All @@ -24,15 +24,15 @@ with ``let``:
Details
---------

- Uncomment the ``x = 20`` to demonstrate that variables are immutable
by default. Add the ``mut`` keyword to allow changes.
- Uncomment the :rust:`x = 20` to demonstrate that variables are immutable
by default. Add the :rust:`mut` keyword to allow changes.

- Warnings are enabled for this slide, such as for unused variables or
unnecessary ``mut``. These are omitted in most slides to avoid
unnecessary :rust:`mut`. These are omitted in most slides to avoid
distracting warnings. Try removing the mutation but leaving the
``mut`` keyword in place.
:rust:`mut` keyword in place.

- The ``i32`` here is the type of the variable. This must be known at
- The :rust:`i32` here is the type of the variable. This must be known at
compile time, but type inference (covered later) allows the
programmer to omit it in many cases.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ each type.

The types have widths as follows:

- ``iN``, ``uN``, and ``fN`` are *N* bits wide,
- ``isize`` and ``usize`` are the width of a pointer,
- ``char`` is 32 bits wide,
- ``bool`` is 8 bits wide.
- :rust:`iN`, :rust:`uN`, and :rust:`fN` are *N* bits wide,
- :rust:`isize` and :rust:`usize` are the width of a pointer,
- :rust:`char` is 32 bits wide,
- :rust:`bool` is 8 bits wide.

.. raw:: html

Expand All @@ -46,8 +46,8 @@ Details
There are a few syntaxes which are not shown above:

- All underscores in numbers can be left out, they are for legibility
only. So ``1_000`` can be written as ``1000`` (or ``10_00``), and
``123_i64`` can be written as ``123i64``.
only. So :rust:`1_000` can be written as :rust:`1000` (or :rust:`10_00`), and
:rust:`123_i64` can be written as :rust:`123i64`.

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Arithmetic
Arithmetic
------------

.. code:: rust,editable
.. code:: rust

fn interproduct(a: i32, b: i32, c: i32) -> i32 {
return a * b + b * c + c * a;
Expand All @@ -22,7 +22,7 @@ Arithmetic
Details
---------

This is the first time we've seen a function other than ``main``, but
This is the first time we've seen a function other than :rust:`main`, but
the meaning should be clear: it takes three integers, and returns an
integer. Functions will be covered in more detail later.

Expand All @@ -32,11 +32,11 @@ What about integer overflow? In C and C++ overflow of *signed* integers
is actually undefined, and might do unknown things at runtime. In Rust,
it's defined.

Change the ``i32``\ 's to ``i16`` to see an integer overflow, which
Change the :rust:`i32`\ 's to :rust:`i16` to see an integer overflow, which
panics (checked) in a debug build and wraps in a release build. There
are other options, such as overflowing, saturating, and carrying. These
are accessed with method syntax, e.g.,
``(a * b).saturating_add(b * c).saturating_add(c * a)``.
:rust:`(a * b).saturating_add(b * c).saturating_add(c * a)`.

In fact, the compiler will detect overflow of constant expressions,
which is why the example requires a separate function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Rust will look at how the variable is *used* to determine the type:

<!-- mdbook-xgettext: skip -->

.. code:: rust,editable
.. code:: rust

fn takes_u32(x: u32) {
println!("u32: {x}");
Expand Down Expand Up @@ -47,10 +47,10 @@ declaration of a type. The compiler does the job for us and helps us
write more concise code.

When nothing constrains the type of an integer literal, Rust defaults to
``i32``. This sometimes appears as ``{integer}`` in error messages.
Similarly, floating-point literals default to ``f64``.
:rust:`i32`. This sometimes appears as :rust:`{integer}` in error messages.
Similarly, floating-point literals default to :rust:`f64`.

.. code:: rust,compile_fail
.. code:: rust

fn main() {
let x = 3.14;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Exercise: Fibonacci
Exercise: Fibonacci
---------------------

The Fibonacci sequence begins with ``[0,1]``. For n>1, the n'th
The Fibonacci sequence begins with :rust:`[0,1]`. For n>1, the n'th
Fibonacci number is calculated recursively as the sum of the n-1'th and
n-2'th Fibonacci numbers.

Write a function ``fib(n)`` that calculates the n'th Fibonacci number.
Write a function :rust:`fib(n)` that calculates the n'th Fibonacci number.
When will this function panic?

.. code:: rust,editable,should_panic
.. code:: rust

{{#include exercise.rs:fib}}
if n < 2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
====================
``if`` expressions
"if" expressions
====================

--------------------
``if`` expressions
"if" expressions
--------------------

You use
:url:`if expressions <https://doc.rust-lang.org/reference/expressions/if-expr.html#if-expressions>`
exactly like ``if`` statements in other languages:
exactly like :rust:`if` statements in other languages:

.. code:: rust,editable
.. code:: rust

fn main() {
let x = 10;
Expand All @@ -23,10 +23,10 @@ exactly like ``if`` statements in other languages:
}
}

In addition, you can use ``if`` as an expression. The last expression of
each block becomes the value of the ``if`` expression:
In addition, you can use :rust:`if` as an expression. The last expression of
each block becomes the value of the :rust:`if` expression:

.. code:: rust,editable
.. code:: rust

fn main() {
let x = 10;
Expand All @@ -40,14 +40,14 @@ each block becomes the value of the ``if`` expression:
Details
---------

Because ``if`` is an expression and must have a particular type, both of
Because :rust:`if` is an expression and must have a particular type, both of
its branch blocks must have the same type. Show what happens if you add
``;`` after ``"small"`` in the second example.
:rust:`;` after :rust:`"small"` in the second example.

An ``if`` expression should be used in the same way as the other
expressions. For example, when it is used in a ``let`` statement, the
statement must be terminated with a ``;`` as well. Remove the ``;``
before ``println!`` to see the compiler error.
An :rust:`if` expression should be used in the same way as the other
expressions. For example, when it is used in a :rust:`let` statement, the
statement must be terminated with a :rust:`;` as well. Remove the :rust:`;`
before :rust:`println!` to see the compiler error.

.. raw:: html

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
=======================
``match`` Expressions
"match" Expressions
=======================

-----------------------
``match`` Expressions
"match" Expressions
-----------------------

``match`` can be used to check a value against one or more options:
:rust:`match` can be used to check a value against one or more options:

.. code:: rust,editable
.. code:: rust

fn main() {
let val = 1;
Expand All @@ -22,9 +22,9 @@
}
}

Like ``if`` expressions, ``match`` can also return a value;
Like :rust:`if` expressions, :rust:`match` can also return a value;

.. code:: rust,editable
.. code:: rust

fn main() {
let flag = true;
Expand All @@ -41,24 +41,24 @@ Like ``if`` expressions, ``match`` can also return a value;
Details
---------

- ``match`` arms are evaluated from top to bottom, and the first one
- :rust:`match` arms are evaluated from top to bottom, and the first one
that matches has its corresponding body executed.

- There is no fall-through between cases the way that ``switch`` works
- There is no fall-through between cases the way that :rust:`switch` works
in other languages.

- The body of a ``match`` arm can be a single expression or a block.
- The body of a :rust:`match` arm can be a single expression or a block.
Technically this is the same thing, since blocks are also
expressions, but students may not fully understand that symmetry at
this point.

- ``match`` expressions need to be exhaustive, meaning they either need
- :rust:`match` expressions need to be exhaustive, meaning they either need
to cover all possible values or they need to have a default case such
as ``_``. Exhaustiveness is easiest to demonstrate with enums, but
as :rust:`_`. Exhaustiveness is easiest to demonstrate with enums, but
enums haven't been introduced yet. Instead we demonstrate matching on
a ``bool``, which is the simplest primitive type.
a :rust:`bool`, which is the simplest primitive type.

- This slide introduces ``match`` without talking about pattern
- This slide introduces :rust:`match` without talking about pattern
matching, giving students a chance to get familiar with the syntax
without front-loading too much information. We'll be talking about
pattern matching in more detail tomorrow, so try not to go into too
Expand All @@ -68,14 +68,14 @@ Details
More to Explore
-----------------

- To further motivate the usage of ``match``, you can compare the
examples to their equivalents written with ``if``. In the second case
matching on a ``bool`` an ``if {} else {}`` block is pretty similar.
But in the first example that checks multiple cases, a ``match``
- To further motivate the usage of :rust:`match`, you can compare the
examples to their equivalents written with :rust:`if`. In the second case
matching on a :rust:`bool` an :rust:`if {} else {}` block is pretty similar.
But in the first example that checks multiple cases, a :rust:`match`
expression can be more concise than
``if {} else if {} else if {} else``.
:rust:`if {} else if {} else if {} else`.

- ``match`` also supports match guards, which allow you to add an
- :rust:`match` also supports match guards, which allow you to add an
arbitrary logical condition that will get evaluated to determine if
the match arm should be taken. However talking about match guards
requires explaining about pattern matching, which we're trying to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ Loops
Loops
-------

There are three looping keywords in Rust: ``while``, ``loop``, and
``for``:
There are three looping keywords in Rust: :rust:`while`, :rust:`loop`, and
:rust:`for`:

-----------
``while``
"while"
-----------

The
:url:`while keyword <https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-loops>`
works much like in other languages, executing the loop body as long as
the condition is true.

.. code:: rust,editable
.. code:: rust

fn main() {
let mut x = 200;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
============================
``break`` and ``continue``
"break" and "continue"
============================

----------------------------
``break`` and ``continue``
"break" and "continue"
----------------------------

If you want to immediately start the next iteration use
:url:`continue <https://doc.rust-lang.org/reference/expressions/loop-expr.html#continue-expressions>`.

If you want to exit any kind of loop early, use
:url:`break <https://doc.rust-lang.org/reference/expressions/loop-expr.html#break-expressions>`.
With ``loop``, this can take an optional expression that becomes the
value of the ``loop`` expression.
With :rust:`loop`, this can take an optional expression that becomes the
value of the :rust:`loop` expression.

.. code:: rust,editable
.. code:: rust

fn main() {
let mut i = 0;
Expand All @@ -36,9 +36,9 @@ value of the ``loop`` expression.
Details
---------

Note that ``loop`` is the only looping construct which can return a
Note that :rust:`loop` is the only looping construct which can return a
non-trivial value. This is because it's guaranteed to only return at a
``break`` statement (unlike ``while`` and ``for`` loops, which can also
:rust:`break` statement (unlike :rust:`while` and :rust:`for` loops, which can also
return when the condition fails).

.. raw:: html
Expand Down
Loading