Rust Tutorial
Rust Tutorial
html
This book was designed for easily diving into and getting skilled with Rust, and it's very
easy to use: All you need to do is to make each exercise compile without ERRORS and
Panics !
Reading online
• English
• 简体中⽂
Running locally
We use mdbook building our exercises. You can run locally with below steps:
Features
Part of our examples and exercises are borrowed from Rust By Example, thanks for your
great works!
• There are three parts in each chapter: examples, exercises and practices
• Besides examples, we have a lot of exercises , you can Read, Edit and Run them
ONLINE
• Covering nearly all aspects of Rust, such as async/await, threads, sync primitives,
optimizing, standard libraries, tool chain, data structures and algorithms etc.
• The overall difficulties are a bit higher and from easy to super hard: easy � medium
�� hard ��� super hard ����
What we want to do is fill in the gap between learning and getting started with real
projects.
The answers to these questions are always Practice: doing some exercises, and then
reading some small and excellent Rust projects.
This is precisely the goal of this book, so, collecting relative resourses and representing in
Rust By Practice seems not a bad idea.
1. Ripgrep
Answers for above questions usually came with ripgrep , though I don't think it is a small
project, but yes, go for it if you are not afraid to delve deep a bit.
3. Ncspot
Ncspot, a terminal Spotify client. Small, simple, well organized and async, it's good for
learning.
This project is for the book Command-Line Rust(O'Reily) , it will show you how to write
small CLIs (clones of head , cat , ls ).
5. pngme book
This book will guide you to make a command line program that lets you hide secret
messages in PNG files. The primary goal here is to get you writing code. The secondary
6. Writing an OS in Rust
This blog series creates a small operating system in the Rust programming language. Each
post is a small tutorial and includes all needed code, so you can follow along if you like.
The source code is also available in the corresponding Github repository.
On CodeCrafters, you can recreate your favorite developer tools from scratch. It's a
hands-on, minimally-guided approach to master Rust, while appreciating the internals
and documentation of popular technology that we use every day.
8. mini-redis
mini-redis is an incomplete Redis client and server implementation using tokio, it has
decent code base and detail explanations, very suitable for learning Rust and
asynchronous programming.
This online book will walk through the basics of interpreted language implementation in
Rust with a focus on the challenges that are specific to using Rust.
To be continued...
Variables
// Fix the error below with least amount of modification to the code
fn main() {
let x: i32; // Uninitialized but used, ERROR !
let y: i32; // Uninitialized but also unused, only a Warning !
assert_eq!(x, 5);
println!("Success!");
}
assert_eq!(x, 3);
println!("Success!");
}
Scope
A scope is the range within the program for which the item is valid.
3. �
4. ��
fn define_x() {
let x = "hello";
}
Shadowing
You can declare a new variable with the same name as a previous variable, here we can
say the first one is shadowed by the second one.
5. ��
assert_eq!(x, 12);
let x = 42;
println!("{}", x); // Prints "42".
}
6. ��
let y = 4;
// Shadowing
let y = "I can also be bound to text!";
println!("Success!");
}
Unused variables
fn main() {
let x = 1;
}
Destructuring
assert_eq!(x, 3);
assert_eq!(y, 2);
println!("Success!");
}
Destructuring assignments
Introduced in Rust 1.59: You can now use tuple, slice, and struct patterns as the left-hand
side of an assignment.
9. ��
Note: the feature Destructuring assignments need 1.59 or higher Rust version
fn main() {
let (x, y);
(x,..) = (3, 4);
[.., y] = [1, 2];
// Fill the blank to make the code work
assert_eq!([x,y], __);
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Basic Types
Learning resources:
Numbers
Integer
1. �
Tips: If we don't explicitly assign a type to a variable, then the compiler will infer one
for us.
y = x;
println!("Success!");
}
2. �
println!("Success!");
}
3. ���
Tips: If we don't explicitly assign a type to a variable, then the compiler will infer one
for us.
println!("Success!");
}
// Get the type of given variable, return a string representation of the type , e.g "i
fn type_of<T>(_: &T) -> String {
format!("{}", std::any::type_name::<T>())
}
4. ��
println!("Success!");
}
5. ��
6. ��
println!("Success!");
}
Floating-Point
7. �
assert_eq!(type_of(&x), "__".to_string());
println!("Success!");
}
fn main() {
assert!(0.1+0.2==0.3);
println!("Success!");
}
Range
fn main() {
let mut sum = 0;
for i in -3..2 {
sum += i
}
assert!(sum == -3);
for c in 'a'..='z' {
println!("{}",c);
}
}
10. ��
println!("Success!");
}
Computations
11. �
// Integer subtraction
assert!(1i32 - 2 == __);
assert!(1u8 - 2 == -1);
assert!(3 * 50 == __);
assert!(24 % 5 == __);
// Short-circuiting boolean logic
assert!(true && false == __);
assert!(true || false == __);
assert!(!true == __);
// Bitwise operations
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
println!("1 << 5 is {}", 1u32 << 5);
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Char
1. �
// Make it work
use std::mem::size_of_val;
fn main() {
let c1 = 'a';
assert_eq!(size_of_val(&c1),1);
println!("Success!");
}
2. �
// Make it work
fn main() {
let c1 = "中 ";
print_char(c1);
}
fn print_char(c : char) {
println!("{}", c);
}
Bool
3. �
let t = true;
if !t {
println!("Success!");
}
}
4. �
// Make it work
fn main() {
let f = true;
let t = true && false;
assert_eq!(t, f);
println!("Success!");
}
Unit type
5. ��
println!("Success!");
}
fn implicitly_ret_unit() {
println!("I will return a ()");
}
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Examples
fn main() {
let x = 5u32;
let y = {
let x_squared = x * x;
let x_cube = x_squared * x;
let z = {
// The semicolon suppresses this expression and `()` is assigned to `z`
2 * x;
};
Exercises
1. ��
assert_eq!(v, 3);
println!("Success!");
}
2. �
fn main() {
let v = (let x = 3);
assert!(v == 3);
println!("Success!");
}
3. �
fn main() {
let s = sum(1 , 2);
assert_eq!(s, 3);
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Functions
1. ���
fn main() {
// Don't modify the following two lines!
let (x, y) = (1, 2);
let s = sum(x, y);
assert_eq!(s, 3);
println!("Success!");
}
fn sum(x, y: i32) {
x + y;
}
2. �
fn main() {
print();
}
3. ���
println!("Failed!");
}
fn never_return() -> ! {
// Implement this function, don't modify the fn signatures
Diverging functions
Diverging functions never return to the caller, so they may be used in places where a
value of any type is expected.
4. ��
fn main() {
println!("Success!");
}
5. ��
fn main() {
// FILL in the blank
let b = __;
let _v = match b {
true => 1,
// Diverging functions can also be used in match expression to replace a value
false => {
println!("Success!");
panic!("we have no value for `false`, but we can panic");
}
};
You can find the solutions here(under the solutions path), but only use it when you
need it
Ownership
1. ��
fn main() {
// Use as many approaches as you can to make it work
let x = String::from("Hello world");
let y = x;
println!("{}, {}",x, y);
}
2. ��
println!("{}", s2);
}
3. ��
fn main() {
let s = give_ownership();
println!("{}", s);
}
4. ��
print_str(s);
println!("{}", s);
}
fn print_str(s: String) {
println!("{}",s)
}
5. ��
Mutability
6. �
let s1 = s;
s1.push_str("World!");
println!("Success!");
}
7. ���
fn main() {
let x = Box::new(5);
*y = 4;
assert_eq!(*x, 5);
println!("Success!");
}
Partial move
Within the destructuring of a single variable, both by-move and by-reference pattern
bindings can be used at the same time. Doing this will result in a partial move of the
variable, which means that parts of the variable will be moved while other parts stay. In
such a case, the parent variable cannot be used afterwards as a whole, however the parts
that are only referenced (and not moved) can still be used.
Example
fn main() {
#[derive(Debug)]
struct Person {
name: String,
age: Box<u8>,
}
Exercises
8. �
fn main() {
let t = (String::from("hello"), String::from("world"));
let _s = t.0;
9. ��
fn main() {
let t = (String::from("hello"), String::from("world"));
println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world")
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Reference
1. �
fn main() {
let x = 5;
// Fill the blank
let p = __;
2. �
fn main() {
let x = 5;
let y = &x;
println!("Success!");
}
3. �
// Fix error
fn main() {
let mut s = String::from("hello, ");
borrow_object(s);
println!("Success!");
}
fn borrow_object(s: &String) {}
4. �
// Fix error
fn main() {
let mut s = String::from("hello, ");
push_str(s);
println!("Success!");
}
5. ��
fn main() {
let mut s = String::from("hello, ");
p.push_str("world");
println!("Success!");
}
Ref
6. ���
fn main() {
let c = '中 ';
let r1 = &c;
// Fill the blank ,dont change other code
let __ r2 = c;
assert_eq!(*r1, *r2);
println!("Success!");
}
Borrowing rules
7. �
let r1 = &mut s;
let r2 = &mut s;
println!("Success!");
}
Mutability
fn main() {
// Fix error by modifying this line
let s = String::from("hello, ");
borrow_object(&mut s);
println!("Success!");
}
borrow_object(&s);
s.push_str("world");
println!("Success!");
}
fn borrow_object(s: &String) {}
NLL
10. ��
let r1 = &mut s;
r1.push_str("world");
let r2 = &mut s;
r2.push_str("!");
println!("{}",r1);
}
11. ��
fn main() {
let mut s = String::from("hello, ");
let r1 = &mut s;
let r2 = &mut s;
// Add one line below to make a compiler error: cannot borrow `s` as mutable more t
// You can't use r1 and r2 at the same time
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Compound Types
Learning resources:
String
The type of string literal "hello, world" is &str , e.g let s: &str = "hello, world" .
1. � We can't use str type in normal ways, but we can use &str .
println!("Success!");
}
2. �� We can only use str by boxing it, & can be used to convert Box<str> to &str
fn greetings(s: &str) {
println!("{}",s)
}
String
String type is defined in std and stored as a vector of bytes (Vec), but guaranteed to
always be a valid UTF-8 sequence. String is heap allocated, growable and not null
terminated.
3. �
println!("Success!");
}
4. ���
println!("{}", s);
}
println!("Success!");
}
6. �� You can only concat a String with &str , and String 's ownership can be
moved to another variable.
Opposite to the seldom using of str , &str and String are used everywhere!
fn greetings(s: String) {
println!("{}", s)
}
// Use two approaches to fix the error and without adding a new line
fn main() {
let s = "hello, world".to_string();
let s1: &str = s;
println!("Success!");
}
String escapes
9. �
fn main() {
// You can use escapes to write bytes by their hexadecimal values
// Fill the blank below to show "I'm writing Rust"
let byte_escape = "I'm writing Ru\x73__!";
println!("What are you doing\x3F (\\x3F means ?) {}", byte_escape);
10. ��� Sometimes there are just too many characters that need to be escaped or it's
just much more convenient to write a string out as-is. This is where raw string
literals come into play.
// If you need "# in your string, just use more #s in the delimiter.
// You can use up to 65535 #s.
let delimiter = r###"A string with "# in it. And even "##!"###;
println!("{}", delimiter);
println!("Success!");
}
Byte string
Want a string that's not UTF-8? (Remember, str and String must be valid UTF-8). Or maybe
you want an array of bytes that's mostly text? Byte strings to the rescue!
Example:
use std::str;
fn main() {
// Note that this is not actually a `&str`
let bytestring: &[u8; 21] = b"this is a byte string";
// Byte arrays don't have the `Display` trait, so printing them is a bit limited
println!("A byte string: {:?}", bytestring);
A more detailed listing of the ways to write string literals and escape characters is given in
the 'Tokens' chapter of the Rust Reference.
String index
11. ��� You can't use index to access a char in a string, but you can use slice
&s1[start..end] .
fn main() {
let s1 = String::from("hi,中 国 ");
let h = s1[0]; // Modify this line to fix the error, tips: `h` only takes 1 byte in
assert_eq!(h, "h");
let h1 = &s1[3..5]; // Modify this line to fix the error, tips: ` 中` takes 3 bytes
assert_eq!(h1, " 中 ");
println!("Success!");
}
12. �
fn main() {
// Fill the blank to print each char in "你 好 , 世 界"
for c in " 你 好 , 世 界 ".__ {
println!("{}", c)
}
}
utf8_slice
You can use utf8_slice to slice UTF8 string, it can index chars instead of bytes.
Example
use utf8_slice;
fn main() {
let s = "The � goes to the �!";
You can find the solutions here(under the solutions path), but only use it when you
need it
Array
The type of array is [T; Length] , as you can see, array's length is part of their type
signature. So their length must be known at compile time.
fn init_arr(n: i32) {
let arr = [1; n];
}
This will cause an error, because the compiler has no idea of the exact size of the array at
compile time.
1. �
fn main() {
// Fill the blank with proper array type
let arr: __ = [1, 2, 3, 4, 5];
println!("Success!");
}
2. ��
fn main() {
// We can ignore parts of the array type or even the whole type, let the compiler i
let arr0 = [1, 2, 3];
let arr: [_; 3] = ['a', 'b', 'c'];
println!("Success!");
}
fn main() {
// Fill the blank
let list: [i32; 100] = __ ;
assert!(list[0] == 1);
assert!(list.len() == 100);
println!("Success!");
}
fn main() {
// Fix the error
let _arr = [1, 2, '3'];
println!("Success!");
}
5. � Indexing starts at 0.
fn main() {
let arr = ['a', 'b', 'c'];
let ele = arr[1]; // Only modify this line to make the code work!
assert!(ele == 'a');
println!("Success!");
}
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Slice
Slices are similar to arrays, but their length is not known at compile time, so you can't use
slice directly.
1. �� Here, both [i32] and str are slice types, but directly using it will cause
errors. You have to use the reference of the slice instead: &[i32] , &str .
println!("Success!");
}
A slice reference is a two-word object, for simplicity reasons, from now on we will use slice
instead of slice reference . The first word is a pointer to the data, and the second word
is the length of the slice. The word size is the same as usize, determined by the processor
architecture, e.g. 64 bits on an x86-64. Slices can be used to borrow a section of an array,
and have the type signature &[T] .
2. ���
fn main() {
let arr: [char; 3] = [' 中 ', ' 国 ', ' ⼈ '];
println!("Success!");
}
3. ��
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
// Fill the blanks to make the code work
let slice: __ = __;
assert_eq!(slice, &[2, 3, 4]);
println!("Success!");
}
String slices
4. �
fn main() {
let s = String::from("hello");
assert_eq!(slice1, slice2);
println!("Success!");
}
5. �
fn main() {
let s = "你 好 , 世 界";
// Modify this line to make the code work
let slice = &s[0..2];
println!("Success!");
}
// Fix errors
fn main() {
let mut s = String::from("hello world");
s.clear(); // error!
You can find the solutions here(under the solutions path), but only use it when you
need it
Tuple
1. � Elements in a tuple can have different types. Tuple's type signature is (T1, T2,
...) , where T1 , T2 are the types of tuple's members.
fn main() {
let _t0: (u8,i16) = (0, -1);
// Tuples can be tuple's members
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// Fill the blanks to make the code work
let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"
println!("Success!");
}
// Make it work
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.1, "sunface");
println!("Success!");
}
fn main() {
let tup = (1, 6.4, "hello");
assert_eq!(x, 1);
assert_eq!(y, "hello");
assert_eq!(z, 6.4);
println!("Success!");
}
5. �� Destructure assignments.
fn main() {
let (x, y, z);
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
println!("Success!");
}
fn main() {
// Fill the blank, need a few computations here.
let (x, y) = sum_multiply(__);
assert_eq!(x, 5);
assert_eq!(y, 6);
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Struct
println!("Success!");
}
2. � Unit struct don't have any fields. It can be useful when you need to implement a
trait on some type but don’t have any data that you want to store in the type itself.
struct Unit;
trait SomeTrait {
// ...Some behaviors defined here.
}
// We don't care about what fields are in the Unit, but we care about its behaviors.
// So we use a struct with no fields and implement some behaviors for it
impl SomeTrait for Unit { }
fn main() {
let u = Unit;
do_something_with_unit(u);
println!("Success!");
}
3. ��� Tuple struct looks similar to tuples, it has added meaning the struct name
provides but has no named fields. It's useful when you want to give the whole tuple
a name, but don't care about the fields's names.
println!("Success!");
}
fn check_color(p: Color) {
let (x, _, _) = p;
assert_eq!(x, 0);
assert_eq!(p.1, 127);
assert_eq!(__, 255);
}
Operating on structs
4. � You can make a whole struct mutable when instantiating it, but Rust doesn't allow
us to mark only certain fields as mutable.
// Fill the blank and fix the error without adding/removing new line
struct Person {
name: String,
age: u8,
}
fn main() {
let age = 18;
let p = Person {
name: String::from("sunface"),
age,
};
println!("Success!");
}
6. � You can create instance from other instance with struct update syntax
let u2 = set_email(u1);
println!("Success!");
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale), // Print debug info to stderr and assign the value of
height: 50,
};
Partial move
Within the destructuring of a single variable, both by-move and by-reference pattern
bindings can be used at the same time. Doing this will result in a partial move of the
variable, which means that parts of the variable will be moved while other parts stay. In
such a case, the parent variable cannot be used afterwards as a whole, however the parts
that are only referenced (and not moved) can still be used.
Example
fn main() {
#[derive(Debug)]
struct Person {
name: String,
age: Box<u8>,
}
Exercises
8. ��
You can find the solutions here(under the solutions path), but only use it when you
need it
Enum
1. �� Enums can be created with explicit discriminator.
enum Number1 {
Zero = 0,
One,
Two,
}
// C-like enum
enum Number2 {
Zero = 0.0,
One = 1.0,
Two = 2.0,
}
fn main() {
// An enum variant can be converted to a integer by `as`
assert_eq!(Number::One, Number1::One);
assert_eq!(Number1::One, Number2::One);
println!("Success!");
}
fn main() {
let msg1 = Message::Move{__}; // Instantiating with x = 1, y = 2
let msg2 = Message::Write(__); // Instantiating with "hello, world!"
println!("Success!");
}
3. �� We can get the data which an enum variant is holding by pattern match.
fn main() {
let msg = Message::Move{x: 1, y: 2};
println!("Success!");
}
4. ��
fn main() {
let msgs: __ = [
Message::Quit,
Message::Move{x:1, y:3},
Message::ChangeColor(255,255,0)
];
fn show_message(msg: Message) {
println!("{}", msg);
}
5. �� Since there is no null in Rust, we have to use enum Option<T> to deal with
the cases when the value is absent.
if let __ = six {
println!("{}", n);
println!("Success!");
}
use crate::List::*;
enum List {
// Cons: Tuple struct that wraps an element and a pointer to the next node
Cons(u32, Box<List>),
// Nil: A node that signifies the end of the linked list
Nil,
}
// Consume a list, and return the same list with a new element at its front
fn prepend(self, elem: u32) -> __ {
// `Cons` also has type List
Cons(elem, Box::new(self))
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Flow control
If/else
1. �
if n < 0 {
println!("{} is negative", n);
} __ n > 0 {
println!("{} is positive", n);
} __ {
println!("{} is zero", n);
}
}
let big_n =
if n < 10 && n > -10 {
println!(", and is a small number, increase ten-fold");
10 * n
} else {
println!(", and is a big number, halve the number");
n / 2.0 ;
}
For
3. � The for in construct can be used to iterate through an Iterator, e.g a range
a..b .
fn main() {
for n in 1..=100 { // modify this line to make the code work
if n == 100 {
panic!("NEVER LET THIS RUN")
}
}
println!("Success!");
}
4. ��
println!("{:?}", names);
println!("{:?}", numbers);
}
5. �
fn main() {
let a = [4, 3, 2, 1];
While
6. �� The while keyword can be used to run a loop when a condition is true.
__;
}
assert_eq!(n, 66);
println!("Success!");
}
8. �� continue will skip over the remaining code in current iteration and go to the
next iteration.
__
}
assert_eq!(n, 66);
println!("Success!");
}
Loop
// Infinite loop
loop {
count += 1;
if count == 3 {
println!("three");
println!("{}", count);
if count == 5 {
println!("OK, that's enough");
__;
}
}
assert_eq!(count, 5);
println!("Success!");
}
if counter == 10 {
__;
}
};
assert_eq!(result, 20);
println!("Success!");
}
11. ��� It's possible to break or continue outer loops when dealing with nested
loops. In these cases, the loops must be annotated with some 'label, and the label
must be passed to the break/continue statement.
count += 5;
'inner2: loop {
if count >= 30 {
// This breaks the outer loop
break 'outer;
}
assert!(count == __);
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Pattern Match
Learning resources:
Match, if let
Match
1. ��
fn main() {
let dire = Direction::South;
match dire {
Direction::East => println!("East"),
__ => { // Matching South or North here
println!("South or North");
},
_ => println!(__),
};
}
fn main() {
let boolean = true;
assert_eq!(binary, 1);
println!("Success!");
}
fn main() {
let msgs = [
Message::Quit,
Message::Move{x:1, y:3},
Message::ChangeColor(255,255,0)
];
println!("Success!");
}
fn show_message(msg: Message) {
match msg {
__ => { // match Message::Move
assert_eq!(a, 1);
assert_eq!(b, 3);
},
Message::ChangeColor(_, g, b) => {
assert_eq!(g, __);
assert_eq!(b, __);
}
__ => println!("no data in these variants")
}
}
matches!
4. ��
fn main() {
let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];
println!("Success!");
}
5. ��
enum MyEnum {
Foo,
Bar
}
fn main() {
let mut count = 0;
let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
for e in v {
if e == MyEnum::Foo { // Fix the error by changing only this line
count += 1;
}
}
assert_eq!(count, 2);
println!("Success!");
}
If let
For some cases, when matching enums, match is too heavy. We can use if let instead.
6. �
fn main() {
let o = Some(7);
println!("Success!");
}
_ => {}
};
}
7. ��
fn main() {
let a = Foo::Bar(1);
__ {
println!("foobar holds the value: {}", i);
println!("Success!");
}
}
8. ��
enum Foo {
Bar,
Baz,
Qux(u32)
}
fn main() {
let a = Foo::Qux(10);
Shadowing
9. ��
match age {
// Match can also introduce a new shadowed variable
Some(age) => println!("age is a new variable, it's value is {}",age),
_ => ()
}
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Patterns
1. �� Use | to match several values, use ..= to match an inclusive range.
fn main() {}
fn match_number(n: i32) {
match n {
// Match a single value
1 => println!("One!"),
// Fill in the blank with `|`, DON'T use `..` or `..=`
__ => println!("match 2 -> 5"),
// Match an inclusive range
6..=10 => {
println!("match 6 -> 10")
},
_ => {
println!("match -infinite -> 0 or 11 -> +infinite")
}
}
}
2. ��� The @ operator lets us create a variable that holds a value, at the same time
we are testing that value to see whether it matches a pattern.
struct Point {
x: i32,
y: i32,
}
fn main() {
// Fill in the blank to let p match the second arm
let p = Point { x: __, y: __ };
match p {
Point { x, y: 0 } => println!("On the x axis at {}", x),
// Second arm
Point { x: 0..=5, y: y@ (10 | 20 | 30) } => println!("On the y axis at {}"
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
}
}
3. ���
fn main() {
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello {
id: 3..=7,
} => println!("Found an id in range [3, 7]: {}", id),
Message::Hello { id: newid@10 | 11 | 12 } => {
println!("Found an id in another range [10, 12]: {}", newid)
}
Message::Hello { id } => println!("Found some other id: {}", id),
}
}
// Fill in the blank to make the code work, `split` MUST be used
fn main() {
let num = Some(4);
let split = 5;
match num {
Some(x) __ => assert!(x < split),
Some(x) => assert!(x >= split),
None => (),
}
println!("Success!");
}
match numbers {
__ => {
assert_eq!(first, 2);
assert_eq!(last, 2048);
}
}
println!("Success!");
}
match r {
&mut value => value.push_str(" world!")
}
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Examples
struct Point {
x: f64,
y: f64,
}
struct Rectangle {
p1: Point,
p2: Point,
}
impl Rectangle {
// This is a method.
// `&self` is sugar for `self: &Self`, where `Self` is the type of the
// caller object. In this case `Self` = `Rectangle`
fn area(&self) -> f64 {
// `self` gives access to the struct fields via the dot operator.
let Point { x: x1, y: y1 } = self.p1;
let Point { x: x2, y: y2 } = self.p2;
self.p1.y += y;
self.p2.y += y;
}
Exercises
Method
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
// Complete the area method which return the area of a Rectangle.
fn area
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
assert_eq!(rect1.area(), 1500);
println!("Success!");
}
2. �� self will take the ownership of current struct instance, however, &self will
only borrow a reference from the instance.
impl TrafficLight {
pub fn show_state(__) {
println!("the current state is {}", __.color);
}
}
fn main() {
let light = TrafficLight{
color: "red".to_owned(),
};
// Don't take the ownership of `light` here.
light.show_state();
// ... Otherwise, there will be an error below
println!("{:?}", light);
}
3. �� The &self is actually short for self: &Self . Within an impl block, the type
Self is an alias for the type that the impl block is for. Methods must have a
parameter named self of type Self for their first parameter, so Rust lets you
abbreviate this with only the name self in the first parameter spot.
struct TrafficLight {
color: String,
}
impl TrafficLight {
// Using `Self` to fill in the blank.
pub fn show_state(__) {
println!("the current state is {}", self.color);
}
Associated functions
4. �� All functions defined within an impl block are called associated functions
because they’re associated with the type named after the impl . We can define
associated functions that don’t have self as their first parameter (and thus are not
methods) because they don’t need an instance of the type to work with.
#[derive(Debug)]
struct TrafficLight {
color: String,
}
impl TrafficLight {
// 1. Implement an associated function `new`,
// 2. It will return a TrafficLight contains color "red"
// 3. Must use `Self`, DONT use `TrafficLight` in fn signatures or body
pub fn new()
fn main() {
let light = TrafficLight::new();
assert_eq!(light.get_state(), "red");
println!("Success!");
}
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
println!("Success!");
}
Enums
#[derive(Debug)]
enum TrafficLightColor {
Red,
Yellow,
Green,
}
fn main() {
let c = TrafficLightColor::Yellow;
assert_eq!(c.color(), "yellow");
println!("{:?}",c);
}
Practice
@todo
You can find the solutions here(under the solutions path), but only use it when you
need it
Generics
Functions
1. ���
fn reg_fn(_s: S) {}
fn gen_spec_t(_s: SGen<A>) {}
fn gen_spec_i32(_s: SGen<i32>) {}
fn generic<T>(_s: SGen<T>) {}
fn main() {
// Using the non-generic functions
reg_fn(__); // Concrete type.
gen_spec_t(__); // Implicitly specified type parameter `A`.
gen_spec_i32(__); // Implicitly specified type parameter `i32`.
println!("Success!");
}
2. �� A function call with explicitly specified type parameters looks like: fun::<A, B,
...>() .
fn main() {
assert_eq!(5, sum(2i8, 3i8));
assert_eq!(50, sum(20, 30));
assert_eq!(2.46, sum(1.23, 1.23));
println!("Success!");
}
3. �
fn main() {
let integer = Point { x: 5, y: 10 };
let float = Point { x: 1.0, y: 4.0 };
println!("Success!");
}
4. ��
fn main() {
// DON'T modify this code.
let p = Point{x: 5, y : "hello".to_string()};
println!("Success!");
}
5. ��
// Add generic for Val to make the code work, DON'T modify the code in `main`.
struct Val {
val: f64,
}
impl Val {
fn value(&self) -> &f64 {
&self.val
}
}
fn main() {
let x = Val{ val: 3.0 };
let y = Val{ val: "hello".to_string()};
println!("{}, {}", x.value(), y.value());
}
Method
6. ���
fn main() {
let p1 = Point { x: 5, y: 10 };
let p2 = Point { x: "Hello", y: ' 中 '};
let p3 = p1.mixup(p2);
assert_eq!(p3.x, 5);
assert_eq!(p3.y, '中 ');
println!("Success!");
}
7. ��
impl Point<f32> {
fn distance_from_origin(&self) -> f32 {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
}
fn main() {
let p = Point{x: 5, y: 10};
println!("{}",p.distance_from_origin());
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Const Generics
Const generics are generic arguments that range over constant values, rather than types
or lifetimes. This allows, for instance, types to be parameterized by integers. In fact, there
has been one example of const generic types since early on in Rust's development: the
array types [T; N], for some type T and N: usize. However, there has previously been no
way to abstract over arrays of an arbitrary size: if you wanted to implement a trait for
arrays of any size, you would have to do so manually for each possible value. For a long
time, even the standard library methods for arrays were limited to arrays of length at
most 32 due to this problem.
Examples
1. Here's an example of a type and implementation making use of const generics: a
type wrapping a pair of arrays of the same size.
fn foo<const N: usize>() {}
foo::<{ M + 1 }>(); // Error: const expression contains the generic parameter `M`
foo::<{ std::mem::size_of::<T>() }>(); // Error: const expression contains the gene
fn main() {}
fn main() {
let slice: &[u8] = b"Hello, world";
let reference: Option<&u8> = slice.get(6);
// We know this value is `Some(b' ')`,
// but the compiler can't know that.
assert!(reference.is_some());
Exercises
1. �� <T, const N: usize> is part of the struct type, it means Array<i32, 3> and
Array<i32, 4> are different types.
fn main() {
let arrays = [
Array{
data: [1, 2, 3],
},
Array {
data: [1.0, 2.0, 3.0],
},
Array {
data: [1, 2]
}
];
println!("Success!");
}
2. ��
3. ��� Sometimes we want to limit the size of a variable, e.g when using in
embedding environments, then const expressions will fit your needs.
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
fn check_size<T>(val: T)
where
Assert<{ core::mem::size_of::<T>() < 768 }>: IsTrue,
{
//...
}
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Traits
A trait tells the Rust compiler about functionality a particular type has and can share with
other types. We can use traits to define shared behavior in an abstract way. We can use
trait bounds to specify that a generic type can be any type that has certain behavior.
Note: Traits are similar to interfaces in other languages, although with some
differences.
Examples
trait Animal {
// Associated function signature; `Self` refers to the implementor type.
fn new(name: String) -> Self;
impl Sheep {
fn is_naked(&self) -> bool {
self.naked
}
fn shear(&mut self) {
if self.is_naked() {
// Implementor methods can use the implementor's trait methods.
println!("{} is already naked...", self.name());
} else {
println!("{} gets a haircut!", self.name);
self.naked = true;
}
}
}
Exercises
1. ��
struct Student {}
impl Hello for Student {
}
struct Teacher {}
impl Hello for Teacher {
}
fn main() {
let s = Student {};
assert_eq!(s.say_hi(), "hi");
assert_eq!(s.say_something(), "I'm a good student");
println!("Success!");
}
Derive
The compiler is capable of providing basic implementations for some traits via the
2. ��
impl Inches {
fn to_centimeters(&self) -> Centimeters {
let &Inches(inches) = self;
fn main() {
let _one_second = Seconds(1);
let cmp =
if foot.to_centimeters() < meter {
"smaller"
} else {
"bigger"
};
Operator
In Rust, many of the operators can be overloaded via traits. That is, some operators can
be used to accomplish different tasks based on their input arguments. This is possible
because operators are syntactic sugar for method calls. For example, the + operator in a +
b calls the add method (as in a.add(b)). This add method is part of the Add trait. Hence,
the + operator can be used by any implementor of the Add trait.
3. ��
use std::ops;
fn main() {
assert_eq!(6, multiply(2u8, 3u8));
assert_eq!(5.0, multiply(1.0, 5.0));
println!("Success!");
}
4. ���
struct Foo;
struct Bar;
struct FooBar;
struct BarFoo;
fn main() {
// DON'T modify the code below.
// You need to derive some trait for FooBar to make it comparable.
assert_eq!(Foo + Bar, FooBar);
assert_eq!(Foo - Bar, BarFoo);
println!("Success!");
}
Instead of a concrete type for the item parameter, we specify the impl keyword and the
trait name. This parameter accepts any type that implements the specified trait.
5. ���
#[derive(Debug)]
struct Post {
title: String,
author: String,
content: String,
}
#[derive(Debug)]
struct Weibo {
username: String,
content: String,
}
fn main() {
let post = Post {
title: "Popular Rust".to_string(),
author: "Sunface".to_string(),
content: "Rust is awesome!".to_string(),
};
let weibo = Weibo {
username: "sunface".to_string(),
content: "Weibo seems to be worse than Tweet".to_string(),
};
summary(post);
summary(weibo);
println!("{:?}", post);
println!("{:?}", weibo);
}
We can also use the impl Trait syntax in the return position to return a value of some type
that implements a trait.
However, you can only use impl Trait if you’re returning a single type, use Trait Objects
instead when you really need to return several types.
6. ��
struct Sheep {}
struct Cow {}
trait Animal {
fn noise(&self) -> String;
}
// Returns some struct that implements Animal, but we don't know which one at compile t
// FIX the errors here, you can make a fake random, or you can use trait object.
fn random_animal(random_number: f64) -> impl Animal {
if random_number < 0.5 {
Sheep {}
} else {
Cow {}
}
}
fn main() {
let random_number = 0.234;
let animal = random_animal(random_number);
println!("You've randomly chosen an animal, and it says {}", animal.noise())
}
Trait bound
The impl Trait syntax works for straightforward cases but is actually syntax sugar for a
longer form, which is called a trait bound.
When working with generics, the type parameters often must use traits as bounds to
stipulate what functionality a type implements.
7. ��
fn main() {
assert_eq!(sum(1, 2), 3);
}
8. ��
impl<T> Pair<T> {
fn new(x: T, y: T) -> Self {
Self {
x,
y,
}
}
}
struct Unit(i32);
fn main() {
let pair = Pair{
x: Unit(1),
y: Unit(3)
};
pair.cmp_display();
}
9. ���
fn example2() {
// We can also use `where` to construct `T`
struct Cacher<T>
where T: Fn(u32) -> u32,
{
calculation: T,
value: Option<u32>,
}
impl<T> Cacher<T>
where T: Fn(u32) -> u32,
{
fn new(calculation: T) -> Cacher<T> {
Cacher {
calculation,
value: None,
}
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Trait Object
In traits chapter we have seen that we can't use impl Trait when returning multiple
types.
Another limitation of arrays is that they can only store elements of one type. Using enums
is not a bad solution when we have a fixed set of types at compile time, but trait objects
would be more flexible and powerful.
1. ���
trait Bird {
fn quack(&self) -> String;
}
struct Duck;
impl Duck {
fn swim(&self) {
println!("Look, the duck is swimming")
}
}
struct Swan;
impl Swan {
fn fly(&self) {
println!("Look, the duck.. oh sorry, the swan is flying")
}
}
fn main() {
// FILL in the blank.
let duck = __;
duck.swim();
println!("Success!");
}
trait Bird {
fn quack(&self);
}
struct Duck;
impl Duck {
fn fly(&self) {
println!("Look, the duck is flying")
}
}
struct Swan;
impl Swan {
fn fly(&self) {
println!("Look, the duck.. oh sorry, the swan is flying")
}
}
fn main() {
// FILL in the blank to make the code work.
let birds __;
fn main() {
let x = 1.1f64;
let y = 8u8;
// Draw x.
draw_with_box(__);
// Draw y.
draw_with_ref(&y);
println!("Success!");
}
fn draw_with_ref(x: __) {
x.draw();
}
When we use trait objects, Rust must use dynamic dispatch. The compiler doesn’t know
all the types that might be used with the code that is using trait objects, so it doesn’t know
which method implemented on which type to call. Instead, at runtime, Rust uses the
pointers inside the trait object to know which method to call. There is a runtime cost
when this lookup happens that doesn’t occur with static dispatch. Dynamic dispatch also
prevents the compiler from choosing to inline a method’s code, which in turn prevents
some optimizations.
4. ��
trait Foo {
fn method(&self) -> String;
}
fn main() {
let x = 5u8;
let y = "Hello".to_string();
static_dispatch(x);
dynamic_dispatch(&y);
println!("Success!");
}
Object safe
You can only make object-safe traits into trait objects. A trait is object safe if all the
methods defined in the trait have the following properties:
5. ����
fn main() {
my_function(Box::new(13_u32));
my_function(Box::new(String::from("abc")));
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Advance Traits
Associated types
The use of "Associated types" improves the overall readability of code by moving inner
types locally into a trait as output types. For example :
Using of Address is much more clearer and convenient than AsRef<[u8]> + Clone +
fmt::Debug + Eq + Hash .
1. ���
fn main() {
let number_1 = 3;
let number_2 = 10;
2. ��
use std::ops::Sub;
#[derive(Debug, PartialEq)]
struct Point<T> {
x: T,
y: T,
}
// FILL in the blank in three ways: two of them use the default generic parameters, th
// Notice that the implementation uses the associated type `Output`.
impl __ {
type Output = Self;
fn main() {
assert_eq!(Point { x: 2, y: 3 } - Point { x: 1, y: 0 },
Point { x: 1, y: 3 });
println!("Success!");
}
When calling methods with the same name, we have to use Fully Qualified Syntax.
Example
trait UsernameWidget {
// Get the selected username out of this widget
fn get(&self) -> String;
}
trait AgeWidget {
// Get the selected age out of this widget
fn get(&self) -> u8;
}
fn main() {
let form = Form{
username: "rustacean".to_owned(),
age: 28,
};
println!("Success!");
}
Exercise
3. ��
trait Pilot {
fn fly(&self) -> String;
}
trait Wizard {
fn fly(&self) -> String;
}
struct Human;
impl Human {
fn fly(&self) -> String {
String::from("*waving arms furiously*")
}
}
fn main() {
let person = Human;
println!("Success!");
}
Supertraits
Sometimes, you might need one trait to use another trait’s functionality( like the
"inheritance" in other languages ). In this case, you need to rely on the dependent trait
also being implemented. The trait you rely on is a supertrait of the trait you’re
implementing.
4. ���
trait Person {
fn name(&self) -> String;
}
trait Programmer {
fn fav_language(&self) -> String;
}
struct CSStudent {
name: String,
university: String,
fav_language: String,
git_username: String
}
// IMPLEMENT the necessary traits for CSStudent to make the code work
impl ...
fn main() {
let student = CSStudent {
name: "Sunfei".to_string(),
university: "XXX".to_string(),
fav_language: "Rust".to_string(),
git_username: "sunface".to_string()
};
Orphan Rules
We can’t implement external traits on external types. For example, we can’t implement
the Display trait on Vec<T> within our own crate, because Display and Vec<T> are
defined in the standard library and aren’t local to our crate.
This restriction is often called the orphan rule, so named because the parent type is not
present. This rule ensures that other people’s code can’t break your code and vice versa.
It’s possible to get around this restriction using the newtype pattern, which involves
creating a new type in a tuple struct.
5. ��
use std::fmt;
fn main() {
let w = Pretty("hello".to_string());
println!("w = {}", w);
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Collection Types
Learning resources:
String
std::string::String is a UTF-8 encoded, growable string. It is the most common string
type we used in daily development, it also has ownership over the string contents.
Basic operations
1. ��
move_ownership(s);
println!("Success!");
}
fn move_ownership(s: String) {
println!("ownership of \"{}\" is moved here!", s)
}
&str is a slice ( &[u8] ) that always points to a valid UTF-8 sequence, and can be used to
view into a String, just like &[T] is a view into Vec<T> .
2. ��
println!("Success!");
}
3. ��
println!("Success!");
}
• The first of which is that if you need a non-UTF-8 string, consider OsString. It is
similar, but without the UTF-8 constraint.
• The second implication is that you cannot index into a String.
Indexing is intended to be a constant-time operation, but UTF-8 encoding does not allow
us to do this. Furthermore, it’s not clear what sort of thing the index should return: a byte,
a codepoint, or a grapheme cluster. The bytes and chars methods return iterators over
the first two, respectively.
4. ��� You can't use index to access a char in a string, but you can use slice
&s1[start..end] .
println!("Success!");
}
UTF8_slice
You can use utf8_slice to slice UTF8 string, it can index chars instead of bytes.
Example
use utf8_slice;
fn main() {
let s = "The � goes to the �!";
5. ���
assert_eq!(s, s1);
println!("Success!");
}
Representation
The pointer points to an internal buffer String uses to store its data. The length is the
number of bytes currently stored in the buffer( always stored on the heap ), and the
capacity is the size of the buffer in bytes. As such, the length will always be less than or
equal to the capacity.
println!("{}", s.capacity());
for _ in 0..2 {
s.push_str("hello");
println!("{}", s.capacity());
}
println!("Success!");
}
7. ���
fn main() {
let story = String::from("Rust By Practice");
assert_eq!(16, len);
// We can rebuild a String out of ptr, len, and capacity. This is all
// unsafe because we are responsible for making sure the components are
// valid:
let s = unsafe { String::from_raw_parts(ptr, len, capacity) };
assert_eq!(*story, s);
println!("Success!");
}
Common methods
You can find the solutions here(under the solutions path), but only use it when you
need it
Vector
Vectors are resizable arrays. Like slices, their size is not known at compile time, but they
can grow or shrink at any time.
Basic Operations
1. ���
fn main() {
let arr: [u8; 3] = [1, 2, 3];
let v = Vec::from(arr);
is_vec(&v);
assert_eq!(v, v1);
println!("Success!");
}
fn is_vec(v: &Vec<u8>) {}
assert_eq!(v1, v2);
println!("Success!");
}
3. ���
assert_eq!(v1, v2);
let s = "hello".to_string();
let v2 = s.into_bytes();
assert_eq!(v1, v2);
println!("Success!");
}
Indexing
4. ���
for i in 0..5 {
// IMPLEMENT the code here...
}
println!("Success!");
}
Slicing
A Vec can be mutable. On the other hand, slices are read-only objects. To get a slice, use
&.
In Rust, it’s more common to pass slices as arguments rather than vectors when you just
want to provide read access. The same goes for String and &str .
5. ��
assert_eq!(slice1, slice2);
println!("Success!");
}
Capacity
The capacity of a vector is the amount of space allocated for any future elements that will
be added onto the vector. This is not to be confused with the length of a vector, which
specifies the number of actual elements within the vector. If a vector’s length exceeds its
capacity, its capacity will automatically be increased, but its elements will have to be
reallocated.
For example, a vector with capacity 10 and length 0 would be an empty vector with space
for 10 more elements. Pushing 10 or fewer elements onto the vector will not change its
capacity or cause reallocation to occur. However, if the vector’s length is increased to 11,
it will have to reallocate, which can be slow. For this reason, it is recommended to use
Vec::with_capacity whenever possible to specify how big the vector is expected to get.
6. ��
// The vector contains no items, even though it has capacity for more
assert_eq!(vec.len(), __);
assert_eq!(vec.capacity(), 10);
assert_eq!(vec.len(), __);
assert_eq!(vec.capacity(), __);
println!("Success!");
}
The elements in a vector must be the same type, for example , the code below will cause
an error:
fn main() {
let v = vec![1, 2.0, 3];
}
7. ��
#[derive(Debug)]
enum IpAddr {
V4(String),
V6(String),
}
fn main() {
// FILL in the blank
let v : Vec<IpAddr>= __;
println!("Success!");
}
8. ��
trait IpAddr {
fn display(&self);
}
struct V4(String);
impl IpAddr for V4 {
fn display(&self) {
println!("ipv4: {:?}",self.0)
}
}
struct V6(String);
impl IpAddr for V6 {
fn display(&self) {
println!("ipv6: {:?}",self.0)
}
}
fn main() {
// FILL in the blank
let v: __= vec![
Box::new(V4("127.0.0.1".to_string())),
Box::new(V6("::1".to_string())),
];
for ip in v {
ip.display();
}
}
HashMap
Where vectors store values by an integer index, HashMaps store values by key. It is a hash
map implemented with quadratic probing and SIMD lookup. By default, HashMap uses a
hashing algorithm selected to provide resistance against HashDoS attacks.
The default hashing algorithm is currently SipHash 1-3 , though this is subject to change
at any point in the future. While its performance is very competitive for medium sized
keys, other hashing algorithms will outperform it for small keys such as integers as well as
large keys such as long strings, though those algorithms will typically not protect against
attacks such as HashDoS.
The hash table implementation is a Rust port of Google’s SwissTable. The original C++
version of SwissTable can be found here, and this CppCon talk gives an overview of how
the algorithm works.
Basic Operations
1. ��
if scores.contains_key("Daniel") {
// Indexing returns a value V
let score = scores["Daniel"];
assert_eq!(score, __);
scores.remove("Daniel");
}
assert_eq!(scores.len(), __);
2. ��
use std::collections::HashMap;
fn main() {
let teams = [
("Chinese Team", 100),
("American Team", 10),
("France Team", 50),
];
assert_eq!(teams_map1, teams_map2);
println!("Success!");
}
3. ��
assert_eq!(player_stats["health"], __);
// Ensures a value is in the entry by inserting the default if empty, and returns
// a mutable reference to the value in the entry.
let health = player_stats.entry("health").or_insert(50);
assert_eq!(health, __);
*health -= 50;
assert_eq!(*health, __);
println!("Success!");
}
fn random_stat_buff() -> u8 {
// Could actually return some random value here - let's just return
// some fixed value for now
42
}
Any type that implements the Eq and Hash traits can be a key in HashMap . This includes:
• bool (though not very useful since there is only two possible keys)
• int , uint , and all variations thereof
• String and &str (tips: you can have a HashMap keyed by String and call .get()
with an &str )
Note that f32 and f64 do not implement Hash , likely because floating-point precision
errors would make using them as hashmap keys horribly error-prone.
All collection classes implement Eq and Hash if their contained type also respectively
implements Eq and Hash . For example, Vec<T> will implement Hash if T implements
Hash .
4. ��
struct Viking {
name: String,
country: String,
}
impl Viking {
/// Creates a new Viking.
fn new(name: &str, country: &str) -> Viking {
Viking {
name: name.to_string(),
country: country.to_string(),
}
}
}
fn main() {
// Use a HashMap to store the vikings' health points.
let vikings = HashMap::from([
(Viking::new("Einar", "Norway"), 25),
(Viking::new("Olaf", "Denmark"), 24),
(Viking::new("Harald", "Iceland"), 12),
]);
Capacity
Like vectors, HashMaps are growable, but HashMaps can also shrink themselves when
they have excess space. You can create a HashMap with a certain starting capacity using
HashMap::with_capacity(uint) , or use HashMap::new() to get a HashMap with a
default initial capacity (recommended).
Example
use std::collections::HashMap;
fn main() {
let mut map: HashMap<i32, i32> = HashMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
// Indeed ,the capacity of HashMap is not 100, so we can't compare the equality her
assert!(map.capacity() >= 100);
// Shrinks the capacity of the map with a lower limit. It will drop
// down no lower than the supplied limit while maintaining the internal rules
// and possibly leaving some space in accordance with the resize policy.
map.shrink_to(50);
assert!(map.capacity() >= 50);
Ownership
For types that implement the Copy trait, like i32 , the values are copied into HashMap .
For owned values like String , the values will be moved and HashMap will be the owner of
those values.
5. ��
let v2 = "hello".to_string();
let mut m2 = HashMap::new();
// Ownership moved here
m2.insert(v2, v1);
assert_eq!(v2, "hello");
println!("Success!");
}
If the performance of SipHash 1-3 doesn't meet your requirements, you can find
replacements in crates.io or github.com.
use std::hash::BuildHasherDefault;
use std::collections::HashMap;
// Introduce a third party hash function
use twox_hash::XxHash64;
Type Conversion
Learning resources:
Convert by as
Rust provides no implicit type conversion(coercion) between primitive types. But explicit
type conversions can be performed using the as keyword.
1. �
println!("Success!");
}
2. �� By default, overflow will cause compile errors, but we can add an global
annotation to suppress these errors.
fn main() {
assert_eq!(u8::MAX, 255);
// The max of `u8` is 255 as shown above.
// so the below code will cause an overflow error: literal out of range for `u8`.
// PLEASE looking for clues within compile errors to FIX it.
// DON'T modify any code in main.
let v = 1000 as u8;
println!("Success!");
}
fn main() {
assert_eq!(1000 as u16, __);
4. ��� Raw pointers can be converted to memory address (integer) and vice versa.
assert_eq!(values[1], 3);
println!("Success!");
}
5. ���
fn main() {
let arr :[u64; 13] = [0; 13];
assert_eq!(std::mem::size_of_val(&arr), 8 * 13);
let a: *const [u64] = &arr;
let b = a as *const [u8];
unsafe {
assert_eq!(std::mem::size_of_val(&*b), __)
}
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
From/Into
The From trait allows for a type to define how to create itself from another type, hence
providing a very simple mechanism for converting between several types.
The From and Into traits are inherently linked, and this is actually part of its
implementation. It means if we write something like this: impl From<T> for U , then we
can use let u: U = U::from(T) or let u:U = T.into() .
The Into trait is simply the reciprocal of the From trait. That is, if you have implemented
the From trait for your type, then the Into trait will be automatically implemented for
the same type.
Using the Into trait will typically require the type annotations as the compiler is unable
to determine this most of the time.
fn main() {
let my_str = "hello";
Because the standard library has already implemented this for us : impl From<&'_ str>
for String .
1. ���
fn main() {
// impl From<bool> for i32
let i1: i32 = false.into();
let i2: i32 = i32::from(false);
assert_eq!(i1, i2);
assert_eq!(i1, 0);
println!("Success!");
}
2. ��
#[derive(Debug)]
struct Number {
value: i32,
}
println!("Success!");
}
3. ��� When performing error handling it is often useful to implement From trait
for our own error type. Then we can use ? to automatically convert the underlying
error type to our own error type.
use std::fs;
use std::io;
use std::num;
enum CliError {
IoError(io::Error),
ParseError(num::ParseIntError),
}
fn main() {
println!("Success!");
}
TryFrom/TryInto
Similar to From and Into , TryFrom and TryInto are generic traits for converting
between types.
Unlike From/Into , TryFrom and TryInto are used for fallible conversions and return a
Result instead of a plain value.
4. ��
fn main() {
let n: i16 = 256;
assert_eq!(n, __);
println!("Success!");
}
5. ���
#[derive(Debug, PartialEq)]
struct EvenNum(i32);
// IMPLEMENT `try_from`
fn try_from(value: i32) -> Result<Self, Self::Error> {
if value % 2 == 0 {
Ok(EvenNum(value))
} else {
Err(())
}
}
}
fn main() {
assert_eq!(EvenNum::try_from(8), Ok(EvenNum(8)));
assert_eq!(EvenNum::try_from(5), Err(()));
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it
Others
To convert any type to String , you can simply use the ToString trait for that type.
Rather than doing that directly, you should implement the fmt::Display trait which will
automatically provides ToString and also allows you to print the type with println! .
1. ��
use std::fmt;
struct Point {
x: i32,
y: i32,
}
fn main() {
let origin = Point { x: 0, y: 0 };
// FILL in the blanks
assert_eq!(origin.__, "The point is (0, 0)");
assert_eq!(format!(__), "The point is (0, 0)");
println!("Success!");
}
Parse a String
2. ��� We can use parse method to convert a String into a i32 number, this is
because FromStr is implemented for i32 type in standard library: impl FromStr
for i32
// To use `from_str` method, you need to introduce this trait into the current scope.
use std::str::FromStr;
fn main() {
let parsed: i32 = "5".__.unwrap();
let turbo_parsed = "10".__.unwrap();
let from_str = __.unwrap();
let sum = parsed + turbo_parsed + from_str;
assert_eq!(sum, 35);
println!("Success!");
}
3. �� We can also implement the FromStr trait for our custom types
use std::str::FromStr;
use std::num::ParseIntError;
#[derive(Debug, PartialEq)]
struct Point {
x: i32,
y: i32
}
println!("Success!");
}
Deref
You can find all the examples and exercises of the Deref trait here.
Transmute
So, transmute is incredibly unsafe ! The caller has to ensure all the safes himself!
Examples
1. transmute can be used to turn a pointer into a function pointer, this is not portable
on machines where function pointer and data pointer have different sizes.
fn main() {
let pointer = foo as *const ();
let function = unsafe {
std::mem::transmute::<*const (), fn() -> i32>(pointer)
};
assert_eq!(function(), 0);
}
3. Rather than using transmute , you can use some alternatives instead.
fn main() {
/*Turning raw bytes(&[u8]) to u32, f64, etc.: */
let raw_bytes = [0x78, 0x56, 0x34, 0x12];
// Now, put together `as` and reborrowing - note the chaining of `as`
// `as` is not transitive
let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
// Or, just use a byte string, if you have control over the string
// literal
assert_eq!(b"Rust", &[82, 117, 115, 116]);
}
You can find the solutions here(under the solutions path), but only use it when you
need it
panic!
The simplest error handling mechanism is to use panic . It just prints an error message
and starts unwinding the stack, finally exit the current thread:
1. ��
fn main() {
drink(__);
// Sometimes, the compiler is unable to find the overflow errors for you in compile
let v = production_rate_per_hour(2);
println!("Success!")
}
fn divide(x:u8, y:u8) {
println!("{}", x / y)
}
By default the stack unwinding will only give something like this:
thread 'main' panicked at 'index out of bounds: the len is 3 but the index is
99', src/main.rs:4:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Though there is the reason of panic and the line of the code is showing where the panic
has occured, sometimes we want to get more info about the call stack.
3. �
By default, when a panic occurs, the program starts unwinding, which means Rust walks
back up the stack and cleans up the data from each function it encounters.
But this walk back and clean up is a lot of work. The alternative is to immediately abort
the program without cleaning up.
If in your project you need to make the resulting binary as small as possible, you can
switch from unwinding to aborting by adding below content to Cargo.toml :
[profile.release]
panic = 'abort'
You can find the solutions here(under the solutions path), but only use it when you
need it :)
result and ?
Result<T> is an enum to describe possible errors. It has two variants:
In short words, the expected outcome is Ok , while the unexpected outcome is Err .
1. ��
fn main() {
let result = multiply("10", "2");
assert_eq!(result, __);
println!("Success!");
}
2. ��
use std::num::ParseIntError;
fn main() {
assert_eq!(multiply("3", "4").unwrap(), 12);
println!("Success!");
}
3. ��
use std::fs::File;
use std::io::{self, Read};
__;
Ok(s)
}
fn main() {
assert_eq!(read_file1().unwrap_err().to_string(), read_file2().unwrap_err()
println!("Success!");
}
map and and_then are two common combinators for Result<T, E> (also for
Option<T> ).
4. ��
use std::num::ParseIntError;
fn main() {
assert_eq!(add_two("4").unwrap(), 6);
println!("Success!");
}
5. ���
use std::num::ParseIntError;
// With the return type rewritten, we use pattern matching without `unwrap()`.
// But it's so Verbose...
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
match n1_str.parse::<i32>() {
Ok(n1) => {
match n2_str.parse::<i32>() {
Ok(n2) => {
Ok(n1 * n2)
},
Err(e) => Err(e),
}
},
Err(e) => Err(e),
}
}
fn main() {
// This still presents a reasonable answer.
let twenty = multiply1("10", "2");
print(twenty);
println!("Success!");
}
Type alias
At a module level, creating aliases can be particularly helpful. Errors found in a specific
module often has the same Err type, so a single alias can succinctly defined all
associated Results . This is so useful even the std library supplies one: io::Result .
6. �
use std::num::ParseIntError;
fn main() {
print(multiply("10", "2"));
print(multiply("t", "2"));
println!("Success!");
}
fn main() {
println!("Hello World!");
}
However main is also able to have a return type of Result . If an error occurs within the
main function it will return an error code and print a debug representation of the error(
Debug trait ).
use std::num::ParseIntError;
You can find the solutions here(under the solutions path), but only use it when you
need it :)
.
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files
# in Cargo.toml
[package]
name = "hello-package"
version = "0.1.0"
edition = "2021"
Note! We will use this package across the whole chapter as a practice project.
.
├── Cargo.toml
└── src
└── lib.rs
1 directory, 2 files
# in Cargo.toml
[package]
name = "hello-package1"
version = "0.1.0"
edition = "2021"
Note! This package could be safely removed due to the first one's existence.
3. �
Crate
A crate is a binary or library. The crate root is a source file that the Rust compiler starts
from and makes up the root module of the crate.
In package hello-package , there is binary crate with the same name as the package :
hello-package , and src/main.rs is the crate root of this binary crate.
Similar to hello-package , hello-package1 also has a crate in it, however, this package
doesn't contain a binary crate but a library crate, and src/lib.rs is the crate root.
4. �
5. �� Add a library crate for hello-package and describe it's files tree below:
After this step, there should be two crates in package hello-package : a binary crate
and a library crate, both with the same name as the package.
6. ��� A package can contain at most one library crate, but it can contain as many
binary crates as you would like by placing files in src/bin directory: each file will
be a separate binary crate with the same name as the file.
Yep, as you can see, the above package structure is very standard and is widely used in
many Rust projects.
You can find the solutions here (under the solutions path), but only use it when you
need it :)
Module
Modules let us organize the code within a crate into groups for readability and ease of
reuse. Module also controls the privacy of items, which is whether an item can be seen by
outside code( public ), or is just an internal implementation and not available for outside
code( private ).
We have created a package named hello-package in previous chapter, and it looks like
this:
.
├── Cargo.toml
├── src
│ ├── lib.rs
│ └── main.rs
Now it's time to create some modules in the library crate and use them in the binary
crate, let's start.
mod front_of_house {
// IMPLEMENT this module..
}
// In lib.rs
pub fn eat_at_restaurant() {
// Call add_to_waitlist with **absolute path**:
__.add_to_waitlist();
3. �� You can use super to import items within the parent module
// In lib.rs
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
// FILL in the blank in three ways
//1. using keyword `super`
//2. using absolute path
__.serve_order();
}
fn cook_order() {}
}
// In lib.rs
pub mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
pub fn serve_order() {}
pub fn take_payment() {}
// Maybe you don't want the guest hearing the your complaining about them
// So just make it private
fn complain() {}
}
}
back_of_house::cook_order();
String::from("yummy yummy!")
}
pub fn cook_order() {}
}
4. ���� Please separate the modules and codes above into files resident in below
dir tree :
.
├── Cargo.toml
├── src
│ ├── back_of_house.rs
│ ├── front_of_house
│ │ ├── hosting.rs
│ │ ├── mod.rs
│ │ └── serving.rs
│ ├── lib.rs
│ └── main.rs
// In src/lib.rs
// IMPLEMENT...
// In src/back_of_house.rs
// IMPLEMENT...
// In src/front_of_house/mod.rs
// IMPLEMENT...
// In src/front_of_house/hosting.rs
// IMPLEMENT...
// In src/front_of_house/serving.rs
// IMPLEMENT...
Please ensure you have completed the 4th exercise before making further progress.
You should have below structures and the corresponding codes in them when reaching
here:
.
├── Cargo.toml
├── src
│ ├── back_of_house.rs
│ ├── front_of_house
│ │ ├── hosting.rs
│ │ ├── mod.rs
│ │ └── serving.rs
│ ├── lib.rs
│ └── main.rs
5. ��� Now we will call a few library functions from the binary crate.
// In src/main.rs
You can find the solutions here (under the solutions path), but only use it when you
need it :)
use std::fmt::Result;
use std::io::Result;
fn main() {}
2. �� If we are using multiple items defined in the same crate or module, then listing
each item on its own line will take up too much vertical space.
fn main() {
let _c1:HashMap<&str, i32> = HashMap::new();
let mut c2 = BTreeMap::new();
c2.insert(1, "a");
let _c3: HashSet<i32> = HashSet::new();
}
3. ��� In our recently created package hello-package , add something to make the
below code work
fn main() {
assert_eq!(hello_package::hosting::seat_at_table(), "sit down please");
assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!");
}
Pub(in Crate)
Sometimes we want an item only be public to a certain crate. For this we can use the
pub(in Crate) syntax.
Example
pub mod a {
pub const I: i32 = 3;
mod b {
pub(in crate::a) mod c {
pub(in crate::a) const J: i32 = 4;
}
}
}
Full Code
You can find the solutions here (under the solutions path), but only use it when you
need it :)
Comments
• Regular comments which are ignored by the compiler:
◦ // Line comment, which goes to the end of the line
◦ /* Block comment, which goes to the end of the closing delimiter */
Examples
fn main() {
// This is an example of a line comment
// There are two slashes at the beginning of the line
// And nothing written inside these will be read by the compiler
// println!("Hello, world!");
// Run it. See? Now try deleting the two slashes, and run it again.
/*
* This is another type of comment, a block comment. In general,
* line comments are the recommended comment style. But
* block comments are extremely useful for temporarily disabling
* chunks of code. /* Block comments can be /* nested, */ */
* so it takes only a few keystrokes to comment out everything
* in this main() function. /*/*/* Try it yourself! */*/*/
*/
/*
Note: The previous column of `*` was entirely for style. There's
no actual need for it.
*/
}
Exercises
1. ��
assert_eq!(6, 5 + 3 + 2 + 1 )
}
Doc Comments
• Doc comments which are parsed into HTML and supported Markdown
◦ /// Generate library docs for the following item
◦ //! Generate library docs for the eclosing item
Before starting, we need to create a new package for practice: cargo new --lib doc-
comments .
// in lib.rs
/// Add one to the given value and return the value
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
Cargo doc
We can use cargo doc --open to generate html files and open them in the browser.
/** Add two to the given value and return a new value
let arg = 5;
let answer = my_crate::add_two(arg);
assert_eq!(7, answer);
*/
pub fn add_two(x: i32) -> i32 {
x + 2
}
We can also add doc comments for our crates and modules.
Firstly, let's add some doc comments for our library crate:
Note: We must place crates and module comments at the top of crate root or
module file.
// in lib.rs
pub mod compute;
Next, create a new module file src/compute.rs , and add following comments to it:
// in compute.rs
Doc tests
The doc comments of add_one and add_two contain two example code blocks.
The examples can not only demonstrate how to use your library, but also running as test
with cargo test command.
2. �� But there are errors in the two examples, please fix them, and running with
cargo test to get following result:
running 0 tests
Doc-tests doc-comments
running 2 tests
test src/lib.rs - add_one (line 11) ... ok
test src/lib.rs - add_two (line 26) ... ok
// in src/compute.rs
/// # Panics
///
/// The function panics if the second argument is zero.
///
/// ```rust,should_panic
/// // panics on division by zero
/// doc_comments::compute::div(10, 0);
/// ```
pub fn div(a: i32, b: i32) -> i32 {
if b == 0 {
panic!("Divide-by-zero error");
}
a / b
}
4. �� Sometimes we want to hide the doc comments, but keep the doc tests.
// in src/compute.rs
/// ```
/// # fn try_main() -> Result<(), String> {
/// # let res = doc_comments::compute::try_div(10, 0)?;
/// # Ok(()) // returning from try_main
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// #
/// # }
/// ```
pub fn try_div(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err(String::from("Divide-by-zero"))
} else {
Ok(a / b)
}
}
• The doc comments must not be presented in html files generated by cargo doc
--open
• run the tests, you should see results as below:
running 0 tests
Doc-tests doc-comments
running 4 tests
test src/compute.rs - compute::div (line 7) ... ok
test src/lib.rs - add_two (line 27) ... ok
test src/lib.rs - add_one (line 11) ... ok
test src/compute.rs - compute::try_div (line 20) ... ok
Code navigation
Rust provide a very powerful feature for us, that is code navigation in doc comments.
// in lib.rs
/// Add three to the given value and return a [`Option`] type
pub fn add_three(x: i32) -> Option<i32> {
Some(x + 3)
}
Besides jump into the standard library, you can also jump to another module in the
package.
// in lib.rs
mod a {
/// Add four to the given value and return a [`Option`] type
/// [`crate::MySpecialFormatter`]
pub fn add_four(x: i32) -> Option<i32> {
Some(x + 4)
}
}
struct MySpecialFormatter;
Doc attributes
Below are a few examples of the most common #[doc] attributes used with rustdoc .
inline
#[doc(inline)]
pub use bar::Bar;
no_inline
hidden
For documentation, rustdoc is widely used by the community. It's what is used to
generate the std library docs.
Full Code
Formatted output
fn main() {
// In general, the `{}` will be automatically replaced with any
// arguments. These will be stringified.
println!("{} days", 31);
// You can right-align text with a specified width. This will output
// " 1". 5 white spaces and a "1".
println!("{number:>width$}", number=1, width=6);
// You can pad numbers with extra zeroes. This will output "000001".
println!("{number:0>width$}", number=1, width=6);
// Rust even checks to make sure the correct number of arguments are
// used.
println!("My name is {0}, {1} {0}", "Bond");
// FIXME ^ Add the missing argument: "James"
// For Rust 1.58 and above, you can directly capture the argument from
// surrounding variable. Just like the above, this will output
// " 1". 5 white spaces and a "1".
let number: f64 = 1.0;
let width: usize = 6;
println!("{number:>width$}");
}
[ std::fmt ][fmt] contains many [ traits ][traits] which govern the display of text. The
base form of two important ones are listed below:
• fmt::Debug : Uses the {:?} marker. Format text for debugging purposes.
• fmt::Display : Uses the {} marker. Format text in a more elegant, user friendly
fashion.
Here, we used fmt::Display because the std library provides implementations for these
types. To print text for custom types, more steps are required.
All parse text in the same fashion. As a plus, Rust checks format correctness at compile
time.
format!
�
1.�
fn main() {
let s1 = "hello";
/* Fill in the blank */
let s = format!(__);
assert_eq!(s, "hello, world!");
}
print!, println!
�
2.�
fn main() {
/* Fill in the blanks to make it print:
Hello world, I am
Sunface!
*/
__("hello world, ");
__("I am");
__("Sunface!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Automatic implementations are only provided for types such as in the std library. All
others have to be manually implemented.
Debug
The implementation of Debug is very straightforward: All types can derive the
std::fmt::Debug implementation. This is not true for std::fmt::Display which must
be manually implemented.
{:?} must be used to print out the type which has implemented the Debug trait.
1. �
fn main() {
// Types in std and Rust have implemented the fmt::Debug trait
println!("__ months in a year.", 12);
#[derive(Debug)]
struct Person {
name: String,
age: u8
}
fn main() {
let person = Person { name: "Sunface".to_string(), age: 18 };
/* Make it output:
Person {
name: "Sunface",
age: 18,
}
*/
println!("{:?}", person);
}
#[derive(Debug)]
struct Structure(i32);
#[derive(Debug)]
struct Deep(Structure);
fn main() {
// The problem with `derive` is there is no control over how
// the results look. What if I want this to just show a `7`?
Display
Yeah, Debug is simple and easy to use. But sometimes we want to customize the output
appearance of our type. This is where Display really shines.
Unlike Debug , there is no way to derive the implementation of the Display trait, we have
to manually implement it.
4. ��
/* Make it work*/
use std::fmt;
struct Point2D {
x: f64,
y: f64,
}
fn main() {
let point = Point2D { x: 3.3, y: 7.2 };
assert_eq!(format!("{}",point), "Display: 3.3 + 7.2i");
assert_eq!(format!("{:?}",point), "Debug: Complex { real: 3.3, imag: 7.2 }"
println!("Success!");
}
? operator
Fortunately, Rust provides the ? operator to help us eliminate some unnecessary codes
for dealing with fmt::Result .
5. ��
/* Make it work */
use std::fmt;
struct List(Vec<i32>);
write!(f, "[")?;
fn main() {
let v = List(vec![1, 2, 3]);
assert_eq!(format!("{}",v), "[0: 1, 1: 2, 2: 3]");
println!("Success!");
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Formatting
Positional arguments
��
1.�
Named arguments
��
2.�
fn main() {
println!("{argument}", argument = "test"); // => "test"
println!("Success!");
}
fn main() {
// The following two are padding with 5 spaces
println!("Hello {:5}!", "x"); // => "Hello x !"
println!("Hello {:1$}!", "x", 5); // => "Hello x !"
println!("Success!");
}
fn main() {
// Left align
println!("Hello {:<5}!", "x"); // => Hello x !
// Right align
assert_eq!(format!("Hello __!", "x"), "Hello x!");
// Center align
assert_eq!(format!("Hello __!", "x"), "Hello x !");
println!("Success!");
}
fn main() {
println!("Hello {:5}!", 5); // => Hello 5!
println!("Hello {:+}!", 5); // => Hello +5!
println!("Hello {:05}!", 5); // => Hello 00005!
println!("Hello {:05}!", -5); // => Hello -0005!
println!("Success!")
;}
Precision
�� Floating point precision
6.�
println!("Success!");
}
fn main() {
let s = "Hello, world!";
println!("Success!");
}
��
8.�
fn main() {
assert_eq!(format!("__", 27), "0b11011");
assert_eq!(format!("__", 27), "0o33");
assert_eq!(format!("__", 27), "0x1b");
assert_eq!(format!("__", 27), "0x1B");
println!("Success!");
}
���
9.�
fn main() {
let person = get_person();
println!("Hello, {person}!");
Others
Example
fn main() {
// Exponent
println!("{:2e}", 1000000000); // => 1e9
println!("{:2E}", 1000000000); // => 1E9
// Pointer address
let v= vec![1, 2, 3];
println!("{:p}", v.as_ptr()); // => 0x600002324050
// Escape
println!("Hello {{}}"); // => Hello {}
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Lifetime
Learning resources:
Lifetime
The compiler uses lifetime to ensure all borrows are valid. Typically, a variable's lifetime
begins when it is created and ends when it is destroyed.
2. ��
Example
{
let x = 5; // ----------+-- 'b
// |
let r = &x; // --+-- 'a |
// | |
println!("r: {}", r); // | |
// --+ |
} // ----------+
/* Annotate `r` and `x` as above, and explain why this code fails to compile, in the li
fn main() {
{
let r; // ---------+-- 'a
// |
{ // |
let x = 5; // -+-- 'b |
r = &x; // | |
} // -+ |
// |
println!("r: {}", r); // |
} // ---------+
}
Lifetime annotating
The borrow checker uses explicit lifetime annotations to determine how long a
reference should be valid.
But for us users, in most cases, there is no need to annotate the lifetime, because there
are several elision rules, before learning these rules, we need to know how to annotate
lifetime manually.
Function
Example
fn main() {
let x = 7;
let y = 9;
print_one(&x);
print_multi(&x, &y);
let mut t = 3;
add_one(&mut t);
print_one(&t);
}
3. �
fn main() {}
4. ���
fn main() {
}
5. ��
/* Make it work */
// A function which takes no arguments, but has a lifetime parameter `'a`.
fn failed_borrow<'a>() {
let _x = 12;
fn main() {
let (four, nine) = (4, 9);
failed_borrow();
// `failed_borrow` contains no references to force `'a` to be
// longer than the lifetime of the function, but `'a` is longer.
// Because the lifetime is never constrained, it defaults to `'static`.
}
Structs
6. �
fn main() {
let x = 18;
let y = 15;
7. ��
/* Make it work */
#[derive(Debug)]
struct NoCopyType {}
#[derive(Debug)]
struct Example<'a, 'b> {
a: &'a u32,
b: &'b NoCopyType
}
fn main()
{
/* 'a tied to fn-main stackframe */
let var_a = 35;
let example: Example;
{
/* Lifetime 'b tied to new stackframe/scope */
let var_b = NoCopyType {};
/* fixme */
example = Example { a: &var_a, b: &var_b };
}
8. ��
#[derive(Debug)]
struct NoCopyType {}
#[derive(Debug)]
#[allow(dead_code)]
struct Example<'a, 'b> {
a: &'a u32,
b: &'b NoCopyType
}
fn main()
{
let no_copy = NoCopyType {};
let example = Example { a: &1, b: &no_copy };
fix_me(&example);
println!("Success!")
}
Method
Methods are annotated similarly to functions.
Example
struct Owner(i32);
impl Owner {
// Annotate lifetimes as in a standalone function.
fn add_one<'a>(&'a mut self) { self.0 += 1; }
fn print<'a>(&'a self) {
println!("`print`: {}", self.0);
}
}
fn main() {
let mut owner = Owner(18);
owner.add_one();
owner.print();
}
9. ��
impl ImportantExcerpt {
fn level(&'a self) -> i32 {
3
}
}
fn main() {}
Elision
Some lifetime patterns are so common that borrow checker will allow you to omit them to
save typing and improve readability.
This is known as Elision. Elision exist in Rust only because these patterns are common.
For a more comprehensive understanding of elision, please see lifetime elision in the
official book.
10. ��
struct Owner(i32);
impl Owner {
// Annotate lifetimes as in a standalone function.
fn add_one<'a>(&'a mut self) { self.0 += 1; }
fn print<'a>(&'a self) {
println!("`print`: {}", self.0);
}
}
struct Person<'a> {
age: u8,
name: &'a str,
}
enum Either<'a> {
Num(i32),
Ref(&'a i32),
}
fn main() {}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
&'static
As a reference lifetime, &'static indicates the data pointed to by the reference lives as
long as the running program. But it can still be coerced to a shorter lifetime.
1. �� There are several ways to make a variable with 'static lifetime, two of them
are stored in the read-only memory of the binary.
println!("Success!")
}
#[derive(Debug)]
struct Config {
a: String,
b: String,
}
static mut config: Option<&mut Config> = None;
fn main() {
unsafe {
config = init();
println!("{:?}",config)
}
}
3. � &'static only indicates that the data can live forever, not the reference. The
latter one will be constrained by its scope.
fn main() {
{
// Make a `string` literal and print it:
let static_string = "I'm in read-only memory";
println!("static_string: {}", static_string);
Example
fn main() {
{
// Make an integer to use for `coerce_static`:
let lifetime_num = 9;
T: 'static
As a trait bound, it means the type does not contain any non-static references. Eg. the
receiver can hold on to the type for as long as they want and it will never become invalid
until they drop it.
It's important to understand this means that any owned data always passes a 'static
lifetime bound, but a reference to that owned data generally does not.
5. ��
/* Make it work */
use std::fmt::Debug;
fn main() {
// i is owned and contains no references, thus it's 'static:
let i = 5;
print_it(i);
print_it1(&i);
6. ���
use std::fmt::Display;
fn main() {
let mut string = "First".to_owned();
string.push_str(string.to_uppercase().as_str());
print_a(&string);
print_b(&string);
print_c(&string); // Compilation error
print_d(&string); // Compilation error
print_e(&string);
print_f(&string);
print_g(&string); // Compilation error
}
fn print_b<T>(t: &T)
where
T: Display + 'static,
{
println!("{}", t);
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Advance lifetime
Trait Bounds
Just like generic types can be bounded, lifetimes can also be bounded as below:
Example
#[derive(Debug)]
struct Ref<'a, T: 'a>(&'a T);
// `Ref` contains a reference to a generic type `T` that has
// an unknown lifetime `'a`. `T` is bounded such that any
// *references* in `T` must outlive `'a`. Additionally, the lifetime
// of `Ref` may not exceed `'a`.
fn main() {
let x = 7;
let ref_x = Ref(&x);
print_ref(&ref_x);
print(ref_x);
}
1. �
2. ��
fn main() {
println!("Success!")
}
3. ��
fn main() {
println!("Success!")
}
and could then be used to compare a &'a T with any lifetime to an i32 .
Only a higher-ranked bound can be used here, because the lifetime of the reference is
shorter than any possible lifetime parameter on the function.
4. ���
fn main() {
println!("Success!");
}
fn main() {
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2);
let r3 = &mut s;
println!("{}", r3);
}
Based on our current knowledge, this code will cause en error due to violating the
borrowing rules in Rust.
But if you cargo run it, then everything will be ok, so what's going on here?
The ability of the compiler to tell that a reference is no longer used at a point before the
end of the scope, is called Non-Lexical Lifetimes (NLL for short).
With this ability the compiler knows when is the last time that a reference is used and
optimizing the borrowing rules based on this knowledge.
// lifetime of `a` = α ∪ β ∪ γ
let mut a = &mut u; // --+ α. lifetime of `&mut u` --+ lexical
"lifetime" of `&mut u`,`&mut u`, `&mut w` and `a`
use(a); // | |
*a = 3; // <-----------------+ |
... // |
a = &mut v; // --+ β. lifetime of `&mut v` |
use(a); // | |
*a = 4; // <-----------------+ |
... // |
a = &mut w; // --+ γ. lifetime of `&mut w` |
use(a); // | |
*a = 5; // <-----------------+ <--------------------------+
Reborrow
After learning NLL, we can easily understand reborrow now.
Example
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
impl Point {
fn move_to(&mut self, x: i32, y: i32) {
self.x = x;
self.y = y;
}
}
fn main() {
let mut p = Point { x: 0, y: 0 };
let r = &mut p;
// Here comes the reborrow
let rr: &Point = &*r;
5. ��
*ref1 += 1;
*ref2 += 2;
println!("{}", data);
}
Unbound lifetime
See more info in Nomicon - Unbounded Lifetimes.
// can be written as :
impl Reader for BufReader<'_> {
// Rust 2015
struct Ref<'a, T: 'a> {
field: &'a T
}
// Rust 2018
struct Ref<'a, T> {
field: &'a T
}
A difficult exercise
6. ����
/* Make it work */
struct Interface<'a> {
manager: &'a mut Manager<'a>
}
impl<'a> Interface<'a> {
pub fn noop(self) {
println!("interface consumed");
}
}
struct Manager<'a> {
text: &'a str
}
struct List<'a> {
manager: Manager<'a>,
}
impl<'a> List<'a> {
pub fn get_interface(&'a mut self) -> Interface {
Interface {
manager: &mut self.manager
}
}
}
fn main() {
let mut list = List {
manager: Manager {
text: "hello"
}
};
list.get_interface().noop();
use_list(&list);
}
fn use_list(list: &List) {
println!("{}", list.manager.text);
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Functional programing
Learning resources:
Closure
Closures can capture the enclosing environments. For example we can capture the x
variable :
fn main() {
let x = 1;
let closure = |val| val + x;
assert_eq!(closure(2), 3);
}
From the syntax, we can see that closures are very convenient for on the fly usage. Unlike
functions, both the input and return types of a closure can be inferred by the compiler.
fn main() {
// Increment via closures and functions.
fn function(i: i32) -> i32 { i + 1 }
let i = 1;
// Call the function and closures.
println!("function: {}", function(i));
println!("closure_annotated: {}", closure_annotated(i));
println!("closure_inferred: {}", closure_inferred(i));
Capturing
Closures can capture variables by borrowing or moving. But they prefer to capture by
borrowing and only go lower when required:
• By reference: &T
• By mutable reference: &mut T
• By value: T
1. �
print();
print();
// `color` can be borrowed immutably again, because the closure only holds
// an immutable reference to `color`.
let _reborrow = &color;
println!("{}",color);
}
2. ��
/* Make it work
- Dont use `_reborrow` and `_count_reborrowed`
- Dont modify `assert_eq`
*/
fn main() {
let mut count = 0;
inc();
inc();
assert_eq!(count, 0);
}
3. ��
/* Make it work in two ways, none of them is to remove `take(movable)` away from the co
*/
fn main() {
let movable = Box::new(3);
let consume = || {
println!("`movable`: {:?}", movable);
take(movable);
};
consume();
consume();
}
fn take<T>(_v: T) {}
fn main() {
let movable = Box::new(3);
consume();
consume();
}
Type inferred
The following four closures has no difference in input and return types.
4. �
fn main() {
let example_closure = |x| x;
let s = example_closure(String::from("hello"));
5. ��
fn main() {
let x = vec![1, 2, 3];
fn_once(|z|{z == x.len()})
}
6. ��
fn main() {
let mut s = String::new();
exec(update_string);
println!("{:?}",s);
}
On a variable-by-variable basis, the compiler will capture variables in the least restrictive
manner possible.
For instance, consider a parameter annotated as FnOnce. This specifies that the closure
may capture by &T , &mut T , or T , but the compiler will ultimately choose based on how
the captured variables are used in the closure. Which trait to use is determined by what
the closure does with captured value.
This is because if a move is possible, then any type of borrow should also be possible.
Note that the reverse is not true. If the parameter is annotated as Fn , then capturing
variables by &mut T or T are not allowed.
7. ��
f();
}
f(3)
}
fn main() {
use std::mem;
Move closures may still implement Fn or FnMut , even though they capture variables by
move. This is because the traits implemented by a closure type are determined by what
the closure does with captured values, not how it captures them. The move keyword only
fn main() {
let s = String::new();
exec(update_string);
}
fn exec<F: FnOnce()>(f: F) {
f()
}
fn main() {
let s = String::new();
exec(update_string);
}
fn exec<F: Fn()>(f: F) {
f()
}
8. ��
exec(update_string);
}
fn exec<'a, F: __>(mut f: F) {
f("hello");
}
Input functions
Since closure can be used as arguments, you might wonder can we use functions as
arguments too? And indeed we can.
9. ��
fn function() {
println!("I'm a function!");
}
fn main() {
let closure = || println!("I'm a closure!");
call_me(closure);
call_me(function);
}
10. ��
// How does the following closure capture the environment variable `num`
// &T, &mut T, T ?
|x| x + num
}
fn main() {
let fn_plain = create_fn();
fn_plain(1);
}
11. ��
let num = 5;
if x > 1{
move |x| x + num
} else {
move |x| x + num
}
}
Closure in structs
Example
struct Cacher<T,E>
where
T: Fn(E) -> E,
E: Copy
{
query: T,
value: Option<E>,
}
impl<T,E> Cacher<T,E>
where
T: Fn(E) -> E,
E: Copy
{
fn new(query: T) -> Cacher<T,E> {
Cacher {
query,
value: None,
}
}
#[test]
fn call_with_different_values() {
let mut c = Cacher::new(|a| a);
let v1 = c.value(1);
let v2 = c.value(2);
assert_eq!(v2, 1);
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Iterator
The iterator pattern allows us to perform some tasks on a sequence of items in turn. An
iterator is responsible for the logic of iterating over each item and determining when the
sequence has finished.
fn main() {
let v = vec![1, 2, 3];
for x in v {
println!("{}",x)
}
}
In the code above, You may consider for as a simple loop, but actually it is iterating over
a iterator.
By default for will apply the into_iter to the collection, and change it into a iterator. As
a result, the following code is equivalent to previous one:
fn main() {
let v = vec![1, 2, 3];
for x in v.into_iter() {
println!("{}",x)
}
}
1. �
2. � One of the easiest ways to create an iterator is to use the range notion: a..b .
assert_eq!(v.len(), 100);
}
next method
All iterators implement a trait named Iterator that is defined in the standard library:
3. ��
assert_eq!(v1.next(), __);
assert_eq!(v1.next(), __);
assert_eq!(v1.next(), __);
}
into_iter , iter , iter_mut , all of them can convert a collection into iterator, but in
different ways.
• into_iter consumes the collection, once the collection has been consumed, it is no
longer available for reuse, because its ownership has been moved within the loop.
• iter , this borrows each element of the collection through each iteration, thus
leaving the collection untouched and available for reuse after the loop
• iter_mut , this mutably borrows each element of the collection, allowing for the
collection to be modified in place.
4. �
/* Make it work */
fn main() {
let arr = vec![0; 10];
for i in arr {
println!("{}", i);
}
println!("{:?}",arr);
}
5. �
6. ��
Example
struct Counter {
count: u32,
}
impl Counter {
fn new() -> Counter {
Counter { count: 0 }
}
}
fn main() {
let mut counter = Counter::new();
assert_eq!(counter.next(), Some(1));
assert_eq!(counter.next(), Some(2));
assert_eq!(counter.next(), Some(3));
assert_eq!(counter.next(), Some(4));
assert_eq!(counter.next(), Some(5));
assert_eq!(counter.next(), None);
}
7. ���
struct Fibonacci {
curr: u32,
next: u32,
}
fn main() {
let mut fib = fibonacci();
assert_eq!(fib.next(), Some(1));
assert_eq!(fib.next(), Some(1));
assert_eq!(fib.next(), Some(2));
assert_eq!(fib.next(), Some(3));
assert_eq!(fib.next(), Some(5));
}
Consuming adaptors
Some of these methods call the method next to use up the iterator, so they are called
consuming adaptors.
8. ��
// The sum method will take the ownership of the iterator and iterates
through the items by repeatedly calling next method
let total = v1_iter.sum();
assert_eq!(total, __);
Collect
Other than converting a collection into an iterator, we can also collect the result values
into a collection, collect will consume the iterator.
9. ��
/* Make it work */
use std::collections::HashMap;
fn main() {
let names = [("sunface",18), ("sunfei",18)];
let folks: HashMap<_, _> = names.into_iter().collect();
println!("{:?}",folks);
let v2 = v1.iter().collect();
Iterator adaptors
Methods allowing you to change one iterator into another iterator are known as iterator
adaptors. You can chain multiple iterator adaptors to perform complex actions in a
readable way.
But because all iterators are lazy, you have to call one of the consuming adapters to get
results from calls to iterator adapters.
10. ��
11. ��
println!("{:?}",folks);
}
12. ��
fn main() {
let shoes = vec![
Shoe {
size: 10,
style: String::from("sneaker"),
},
Shoe {
size: 13,
style: String::from("sandal"),
},
Shoe {
size: 10,
style: String::from("boot"),
},
];
assert_eq!(
in_my_size,
vec![
Shoe {
size: 10,
style: String::from("sneaker")
},
Shoe {
size: 10,
style: String::from("boot")
},
]
);
}
You can find the solutions here(under the solutions path), but only use it when you
need it :)
Newtype
The orphan rule tells us that we are allowed to implement a trait on a type as long as
either the trait or the type are local to our crate.
The newtype pattern can help us get around this restriction, which involves creating a
new type in a tuple struct.
1. �
use std::fmt;
fn main() {
// Vec is an external type, so you cannot implement Display trait on Vec type
let w = Wrapper(vec![String::from("hello"), String::from("world")]);
println!("w = {}", w);
}
/* Make it workd */
struct Meters(u32);
fn main() {
let i: u32 = 2;
assert_eq!(i.pow(2), 4);
let n = Meters(i);
// The `pow` method is defined on `u32` type, we can't directly call it
assert_eq!(n.pow(2), 4);
}
3. �� The newtype idiom gives compile time guarantees that the right type of value
is supplied to a program.
/* Make it work */
struct Years(i64);
struct Days(i64);
impl Years {
pub fn to_days(&self) -> Days {
Days(self.0 * 365)
}
}
impl Days {
pub fn to_years(&self) -> Years {
Years(self.0 / 365)
}
}
// An age verification function that checks age in years, must be given a value of type
fn old_enough(age: &Years) -> bool {
age.0 >= 18
}
fn main() {
let age = Years(5);
let age_days = age.to_days();
println!("Old enough {}", old_enough(&age));
println!("Old enough {}", old_enough(&age_days));
}
4. ��
use std::ops::Add;
use std::fmt::{self, format};
struct Meters(u32);
impl fmt::Display for Meters {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "There are still {} meters left", self.0)
}
}
/* Implement calculate_distance */
fn calculate_distance
Type alias
Type alias is important to improve the readability of our code.
fn takes_long_type(f: Thunk) {
// --snip--
}
And Unlike newtype, type alias don't create new types, so the following code is valid:
let x: u32 = 5;
let y: Meters = 5;
5. �
enum VeryVerboseEnumOfThingsToDoWithNumbers {
Add,
Subtract,
}
fn main() {
// We can refer to each variant via its alias, not its long and inconvenient
// name.
let x = Operations::Add;
}
6. �� There are a few preserved aliases in Rust, one of which can be used in impl
blocks.
enum VeryVerboseEnumOfThingsToDoWithNumbers {
Add,
Subtract,
}
impl VeryVerboseEnumOfThingsToDoWithNumbers {
fn run(&self, x: i32, y: i32) -> i32 {
match self {
__::Add => x + y,
__::Subtract => x - y,
}
}
}
7. ��� Array with dynamic length is a Dynamic Sized Type ( DST ), we can't directly
use it
fn main() {
let arr = my_function();
println!("{:?}",arr);
}
fn main() {
}
Smart pointers
Box
Deref
Drop
Rc and Arc
Self referential
Threads
Basic using
Message passing
Sync
Atomic
Global variables
Errors
Unsafe todo
Inline assembly
Rust provides support for inline assembly via the asm! macro. It can be used to embed
handwritten assembly in the assembly output generated by the compiler. Generally this
should not be necessary, but might be where the required performance or timing cannot
be otherwise achieved. Accessing low level hardware primitives, e.g. in kernel code, may
also demand this functionality.
Note: the examples here are given in x86/x86-64 assembly, but other architectures
are also supported.
Basic usage
Let us start with the simplest possible example:
use std::arch::asm;
unsafe {
asm!("nop");
}
This will insert a NOP (no operation) instruction into the assembly generated by the
compiler. Note that all asm! invocations have to be inside an unsafe block, as they could
insert arbitrary instructions and break various invariants. The instructions to be inserted
are listed in the first argument of the asm! macro as a string literal.
use std::arch::asm;
let x: u64;
unsafe {
asm!("mov {}, 5", out(reg) x);
}
assert_eq!(x, 5);
This will write the value 5 into the u64 variable x . You can see that the string literal we
use to specify instructions is actually a template string. It is governed by the same rules as
Rust format strings. The arguments that are inserted into the template however look a bit
different than you may be familiar with. First we need to specify if the variable is an input
or an output of the inline assembly. In this case it is an output. We declared this by writing
out . We also need to specify in what kind of register the assembly expects the variable.
In this case we put it in an arbitrary general purpose register by specifying reg . The
compiler will choose an appropriate register to insert into the template and will read the
variable from there after the inline assembly finishes executing.
use std::arch::asm;
let i: u64 = 3;
let o: u64;
unsafe {
asm!(
"mov {0}, {1}",
"add {0}, 5",
out(reg) o,
in(reg) i,
);
}
assert_eq!(o, 8);
This will add 5 to the input in variable i and write the result to variable o . The
particular way this assembly does this is first copying the value from i to the output, and
then adding 5 to it.
First, we can see that asm! allows multiple template string arguments; each one is
treated as a separate line of assembly code, as if they were all joined together with
newlines between them. This makes it easy to format assembly code.
Second, we can see that inputs are declared by writing in instead of out .
Third, we can see that we can specify an argument number, or name as in any format
string. For inline assembly templates this is particularly useful as arguments are often
used more than once. For more complex inline assembly using this facility is generally
We can further refine the above example to avoid the mov instruction:
use std::arch::asm;
We can see that inout is used to specify an argument that is both input and output. This
is different from specifying an input and output separately in that it is guaranteed to
assign both to the same register.
It is also possible to specify different variables for the input and output parts of an inout
operand:
use std::arch::asm;
let x: u64 = 3;
let y: u64;
unsafe {
asm!("add {0}, 5", inout(reg) x => y);
}
assert_eq!(y, 8);
use std::arch::asm;
Here the compiler is free to allocate the same register for inputs b and c since it knows
they have the same value. However it must allocate a separate register for a since it uses
inout and not inlateout . If inlateout was used, then a and c could be allocated to
the same register, in which case the first instruction to overwrite the value of c and
cause the assembly code to produce the wrong result.
However the following example can use inlateout since the output is only modified
after all input registers have been read:
use std::arch::asm;
As you can see, this assembly fragment will still work correctly if a and b are assigned to
the same register.
use std::arch::asm;
In this example we call the out instruction to output the content of the cmd variable to
port 0x64 . Since the out instruction only accepts eax (and its sub registers) as operand
we had to use the eax constraint specifier.
Note: unlike other operand types, explicit register operands cannot be used in the
template string: you can't use {} and should write the register name directly
instead. Also, they must appear at the end of the operand list after all other operand
types.
use std::arch::asm;
unsafe {
asm!(
// The x86 mul instruction takes rax as an implicit input and
writes
// the 128-bit result of the multiplication to rax:rdx.
"mul {}",
in(reg) a,
inlateout("rax") b => lo,
lateout("rdx") hi
);
}
This uses the mul instruction to multiply two 64-bit inputs with a 128-bit result. The only
explicit operand is a register, that we fill from the variable a . The second operand is
implicit, and must be the rax register, which we fill from the variable b . The lower 64
bits of the result are stored in rax from which we fill the variable lo . The higher 64 bits
are stored in rdx from which we fill the variable hi .
Clobbered registers
In many cases inline assembly will modify state that is not needed as an output. Usually
this is either because we have to use a scratch register in the assembly or because
instructions modify state that we don't need to further examine. This state is generally
referred to as being "clobbered". We need to tell the compiler about this since it may
need to save and restore this state around the inline assembly block.
use core::arch::asm;
fn main() {
// three entries of four bytes each
let mut name_buf = [0_u8; 12];
// String is stored as ascii in ebx, edx, ecx in order
// Because ebx is reserved, we get a scratch register and move from
// ebx into it in the asm. The asm needs to preserve the value of
// that register though, so it is pushed and popped around the main asm
// (in 64 bit mode for 64 bit processors, 32 bit processors would use
ebx)
unsafe {
asm!(
"push rbx",
"cpuid",
"mov [{0}], ebx",
"mov [{0} + 4], edx",
"mov [{0} + 8], ecx",
"pop rbx",
// We use a pointer to an array for storing the values to
simplify
// the Rust code at the cost of a couple more asm instructions
// This is more explicit with how the asm works however, as
opposed
// to explicit register outputs such as `out("ecx") val`
// The *pointer itself* is only an input even though it's written
behind
in(reg) name_buf.as_mut_ptr(),
// select cpuid 0, also specify eax as clobbered
inout("eax") 0 => _,
// cpuid clobbers these registers too
out("ecx") _,
out("edx") _,
);
}
In the example above we use the cpuid instruction to read the CPU manufacturer ID.
This instruction writes to eax with the maximum supported cpuid argument and ebx ,
esx , and ecx with the CPU manufacturer ID as ASCII bytes in that order.
Even though eax is never read we still need to tell the compiler that the register has
been modified so that the compiler can save any values that were in these registers
before the asm. This is done by declaring it as an output but with _ instead of a variable
name, which indicates that the output value is to be discarded.
This code also works around the limitation that ebx is a reserved register by LLVM. That
means that LLVM assumes that it has full control over the register and it must be restored
to its original state before exiting the asm block, so it cannot be used as an output. To
work around this we save the register via push , read from ebx inside the asm block into
a temporary register allocated with out(reg) and then restoring ebx to its original state
via pop . The push and pop use the full 64-bit rbx version of the register to ensure that
the entire register is saved. On 32 bit targets the code would instead use ebx in the
push / pop .
This can also be used with a general register class (e.g. reg ) to obtain a scratch register
for use inside the asm code:
use std::arch::asm;
use std::arch::asm;
By default the compiler will always choose the name that refers to the full register size
(e.g. rax on x86-64, eax on x86, etc).
This default can be overridden by using modifiers on the template string operands, just
like you would with format strings:
use std::arch::asm;
unsafe {
asm!("mov {0:h}, {0:l}", inout(reg_abcd) x);
}
assert_eq!(x, 0xabab);
In this example, we use the reg_abcd register class to restrict the register allocator to the
4 legacy x86 registers ( ax , bx , cx , dx ) of which the first two bytes can be addressed
independently.
Let us assume that the register allocator has chosen to allocate x in the ax register. The
h modifier will emit the register name for the high byte of that register and the l
modifier will emit the register name for the low byte. The asm code will therefore be
expanded as mov ah, al which copies the low byte of the value into the high byte.
If you use a smaller data type (e.g. u16 ) with an operand and forget the use template
modifiers, the compiler will emit a warning and suggest the correct modifier to use.
use std::arch::asm;
fn load_fpu_control_word(control: u16) {
unsafe {
asm!("fldcw [{}]", in(reg) &control, options(nostack));
}
}
Labels
Any reuse of a named label, local or otherwise, can result in an assembler or linker error
or may cause other strange behavior. Reuse of a named label can happen in a variety of
ways including:
• explicitly: using a label more than once in one asm! block, or multiple times across
blocks.
• implicitly via inlining: the compiler is allowed to instantiate multiple copies of an
asm! block, for example when the function containing it is inlined in multiple places.
• implicitly via LTO: LTO can cause code from other crates to be placed in the same
codegen unit, and so could bring in arbitrary labels.
As a consequence, you should only use GNU assembler numeric local labels inside inline
assembly code. Defining symbols in assembly code may lead to assembler and/or linker
Moreover, on x86 when using the default Intel syntax, due to an LLVM bug, you shouldn't
use labels exclusively made of 0 and 1 digits, e.g. 0 , 11 or 101010 , as they may end up
being interpreted as binary values. Using options(att_syntax) will avoid any ambiguity,
but that affects the syntax of the entire asm! block. (See Options, below, for more on
options .)
use std::arch::asm;
let mut a = 0;
unsafe {
asm!(
"mov {0}, 10",
"2:",
"sub {0}, 1",
"cmp {0}, 3",
"jle 2f",
"jmp 2b",
"2:",
"add {0}, 2",
out(reg) a
);
}
assert_eq!(a, 5);
This will decrement the {0} register value from 10 to 3, then add 2 and store it in a .
• First, that the same number can be used as a label multiple times in the same inline
block.
• Second, that when a numeric label is used as a reference (as an instruction operand,
for example), the suffixes “b” (“backward”) or ”f” (“forward”) should be added to the
numeric label. It will then refer to the nearest label defined by this number in this
direction.
Options
By default, an inline assembly block is treated the same way as an external FFI function
call with a custom calling convention: it may read/write memory, have observable side
effects, etc. However, in many cases it is desirable to give the compiler more information
about what the assembly code is actually doing so that it can optimize better.
use std::arch::asm;
Options can be provided as an optional final argument to the asm! macro. We specified
three options here:
• pure means that the asm code has no observable side effects and that its output
depends only on its inputs. This allows the compiler optimizer to call the inline asm
fewer times or even eliminate it entirely.
• nomem means that the asm code does not read or write to memory. By default the
compiler will assume that inline assembly can read or write any memory address
that is accessible to it (e.g. through a pointer passed as an operand, or a global).
• nostack means that the asm code does not push any data onto the stack. This
allows the compiler to use optimizations such as the stack red zone on x86-64 to
avoid stack pointer adjustments.
These allow the compiler to better optimize code using asm! , for example by eliminating
pure asm! blocks whose outputs are not needed.
See the reference for the full list of available options and their effects.
macro
Tests
Write Tests
Benchmark
https://doc.rust-lang.org/unstable-book/library-features/test.html
Assertions
Async/Await
Future
Stream
String
This chapter will provide some exercises to help us avoid such cases to lower the steep
learning curve.
Borrowing
1. ��
impl test {
pub fn new() -> Self {
test { list:vec![1,2,3,4,5,6,7], a:0 }
}
fn main() {}
You can find the solutions here(under the solutions path), but only use it when you
need it :)