urlstyle = "rm"
documentclass = "book"
fontsize = "11pt"
-geometry = "papersize={8in,10in},top=2cm,bottom=2cm,left=2.4cm,right=2.4cm"
+geometry = "papersize={8in,10in},top=2cm,bottom=2cm,left=2.8cm,right=2.5cm"
header-includes = [
# Reduce font size of code blocks
"\\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\\\\{\\},fontsize=\\small}",
## Formats
-You can browse the course material in the browser, at [rust-exercises.com/100-exercises/](https://rust-exercises.com/100-exercises/).\
-You can also [download the course material as a PDF file](https://rust-exercises.com/100-exercises-to-learn-rust.pdf), for offline reading.
+You can go through the course material [in the browser](https://rust-exercises.com/100-exercises/).\
+You can also [download it as a PDF file](https://rust-exercises.com/100-exercises-to-learn-rust.pdf), for offline reading.
## Structure
# If you have an SSH key set up with GitHub
git clone git@github.com:mainmatter/100-exercises-to-learn-rust.git
# Otherwise, use the HTTPS URL:
-#
-# git clone https://github.com/mainmatter/100-exercises-to-learn-rust.git
+# https://github.com/mainmatter/100-exercises-to-learn-rust.git
```
We also recommend you work on a branch, so you can easily track your progress and pull
In previous exercise, you saw the `greeting` function:
```rust
-// `fn` <function_name> ( <input parameters> ) -> <return_type> { <body> }
+// `fn` <function_name> ( <input params> ) -> <return_type> { <body> }
fn greeting() -> &'static str {
// TODO: fix me 👇
"I'm ready to __!"
If you own a value, for example, you can transfer ownership to another variable:
```rust
-let a = "hello, world".to_string(); // <--- `a` is the owner of the String
-let b = a; // <--- `b` is now the owner of the String
+let a = "hello, world".to_string(); // <- `a` is the owner of the String
+let b = a; // <- `b` is now the owner of the String
```
Rust's ownership system is baked into the type system: each function has to declare in its signature
impl ::core::cmp::PartialEq for Ticket {
#[inline]
fn eq(&self, other: &Ticket) -> bool {
- self.title == other.title && self.description == other.description
+ self.title == other.title
+ && self.description == other.description
&& self.status == other.status
}
}
--> src/lib.rs:2:10
|
1 | fn print_if_even<T>(n: T) {
- | - method `is_even` not found for this type parameter
+ | - method `is_even` not found
+ | for this type parameter
2 | if n.is_even() {
| ^^^^^^^ method not found in `T`
The compiler is not happy with this code:
```text
-error[E0277]: `MutexGuard<'_, i32>` cannot be sent between threads safely
+error[E0277]: `MutexGuard<'_, i32>` cannot be sent between
+ threads safely
--> src/main.rs:10:7
|
10 | spawn(move || {
12 | | });
| |_^ `MutexGuard<'_, i32>` cannot be sent between threads safely
|
- = help: the trait `Send` is not implemented for `MutexGuard<'_, i32>`,
- which is required by `{closure@src/main.rs:10:7: 10:14}: Send`
- = note: required for `std::sync::mpsc::Receiver<MutexGuard<'_, i32>>`
+ = help: the trait `Send` is not implemented for
+ `MutexGuard<'_, i32>`, which is required by
+ `{closure@src/main.rs:10:7: 10:14}: Send`
+ = note: required for `Receiver<MutexGuard<'_, i32>>`
to implement `Send`
note: required because it's used within this closure
```
error: future cannot be sent between threads safely
|
5 | tokio::spawn(example());
- | ^^^^^^^^^ future returned by `example` is not `Send`
+ | ^^^^^^^^^
+ | future returned by `example` is not `Send`
|
note: future is not `Send` as this value is used across an await
|