Tune spaces and indentation

pull/76/head
Sudo Nice 4 years ago
parent cea0927fcb
commit 644fc963af

@ -986,7 +986,6 @@ The hashtag #IceToSeeYou had become very popular.
fn main() {
let r#let = 6; // The variable's name is let
let mut r#mut = 10; // This variable's name is mut
}
```
@ -1721,11 +1720,11 @@ fn loop_then_return(mut counter: i32) -> i32 {
fn main() {
let my_number;
{ // Pretend we need to have this code block
{
// Pretend we need to have this code block
let number = {
// Pretend there is code here to make a number
// Lots of code, and finally:
// Lots of code, and finally:
57
};
@ -4028,7 +4027,6 @@ fn main() {
This prints `Some("L\'Allemagne Moderne")` because there was already a key for `1`, so we didn't insert `Le Petit Prince`.
`HashMap` has a very interesting method called `.entry()` that you definitely want to try out. With it you can try to make an entry and use another method like `.or_insert()` to insert the value if there is no key. The interesting part is that it also gives a mutable reference so you can change it if you want. First is an example where we just insert `true` every time we insert a book title into the `HashMap`.
Let's pretend that we have a library and want to keep track of our books.
@ -4068,7 +4066,7 @@ pub fn entry(&mut self, key: K) -> Entry<K, V> // 🚧
[Here is the page for Entry](https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html). Here is a simple version of its code. `K` means key and `V` means variable.
```rust
// 🚧
// 🚧
use std::collections::hash_map::*;
enum Entry<K, V> {
@ -4299,7 +4297,7 @@ use std::collections::BinaryHeap;
fn main() {
let mut jobs = BinaryHeap::new();
// Add jobs to do throughout the day
// Add jobs to do throughout the day
jobs.push((100, "Write back to email from the CEO"));
jobs.push((80, "Finish the report today"));
jobs.push((5, "Watch some YouTube"));
@ -4387,7 +4385,7 @@ fn check_remaining(input: &VecDeque<(&str, bool)>) { // Each item is a (&str, bo
fn done(input: &mut VecDeque<(&str, bool)>) {
let mut task_done = input.pop_back().unwrap(); // pop off the back
task_done.1 = true; // now it's done - mark as true
task_done.1 = true; // now it's done - mark as true
input.push_front(task_done); // put it at the front now
}
@ -5655,12 +5653,12 @@ fn main() {
let mut my_vec_iter = my_vec.iter(); // This is an Iterator type now, but we haven't called it yet
assert_eq!(my_vec_iter.next(), Some(&'a')); // Call the first item with .next()
assert_eq!(my_vec_iter.next(), Some(&'b')); // Call the next
assert_eq!(my_vec_iter.next(), Some(&'a')); // Call the first item with .next()
assert_eq!(my_vec_iter.next(), Some(&'b')); // Call the next
assert_eq!(my_vec_iter.next(), Some(&'거')); // Again
assert_eq!(my_vec_iter.next(), Some(&'柳')); // Again
assert_eq!(my_vec_iter.next(), None); // Nothing is left: just None
assert_eq!(my_vec_iter.next(), None); // You can keep calling .next() but it will always be None
assert_eq!(my_vec_iter.next(), None); // You can keep calling .next() but it will always be None
}
```
@ -5791,7 +5789,6 @@ impl Iterator for Library {
type Item = String;
fn next(&mut self) -> Option<String> {
match self.books.pop() {
Some(book) => Some(book + " is found!"), // Rust allows String + &str
None => None,
@ -5936,7 +5933,7 @@ fn main() {
let num_vec = vec![2, 4, 6];
let double_vec = num_vec // take num_vec
.iter() // iterate over it
.iter() // iterate over it
.map(|number| number * 2) // for each item, multiply by two
.collect::<Vec<i32>>(); // then make a new Vec from this
println!("{:?}", double_vec);
@ -5950,7 +5947,7 @@ fn main() {
let num_vec = vec![10, 9, 8];
num_vec
.iter() // iterate over num_vec
.iter() // iterate over num_vec
.enumerate() // get (index, number)
.for_each(|(index, number)| println!("Index number {} has number {}", index, number)); // do something for each one
}
@ -6024,7 +6021,7 @@ fn main() {
let some_words = vec!["zero", "one", "two", "three", "four", "five"]; // a Vec<&str>
let number_word_hashmap = some_numbers
.into_iter() // now it is an iter
.into_iter() // now it is an iter
.zip(some_words.into_iter()) // inside .zip() we put in the other iter. Now they are together.
.collect::<HashMap<_, _>>();
@ -6049,7 +6046,7 @@ fn main() {
let number_word_hashmap: HashMap<_, _> = some_numbers // Because we tell it the type here...
.into_iter()
.zip(some_words.into_iter())
.collect(); // we don't have to tell it here
.collect(); // we don't have to tell it here
}
```
@ -6118,9 +6115,9 @@ fn main() {
let months = vec!["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let filtered_months = months
.into_iter() // make an iter
.filter(|month| month.len() < 5) // We don't want months more than 5 bytes in length.
// We know that each letter is one byte so .len() is fine
.into_iter() // make an iter
.filter(|month| month.len() < 5) // We don't want months more than 5 bytes in length.
// We know that each letter is one byte so .len() is fine
.filter(|month| month.contains("u")) // Also we only like months with the letter u
.collect::<Vec<&str>>();
@ -6330,8 +6327,8 @@ An easy example is a number that we get from a vec using `.get()`, because that
fn main() {
let new_vec = vec![8, 9, 0]; // just a vec with numbers
let number_to_add = 5; // use this in the math later
let mut empty_vec = vec![]; // results go in here
let number_to_add = 5; // use this in the math later
let mut empty_vec = vec![]; // results go in here
for index in 0..5 {
@ -6570,7 +6567,7 @@ fn main() {
let some_numbers = vec![9, 6, 9, 10, 11];
println!("{}", some_numbers
.iter()
.iter()
.fold(0, |total_so_far, next_number| total_so_far + next_number)
);
}
@ -8738,7 +8735,7 @@ fn returns_a_closure(input: u8) -> impl FnMut(i32) -> i32 {
fn main() {
let my_number = 10;
// Make three closures
// Make three closures
let mut give_two = returns_a_closure(2);
let mut give_forty = returns_a_closure(40);
let mut give_fifty = returns_a_closure(50);
@ -9007,7 +9004,7 @@ fn main() {
}
});
handle_vec.push(handle); // save the handle so we can call join on it outside of the loop
// If we don't push it in the vec, it will just die here
// If we don't push it in the vec, it will just die here
}
handle_vec.into_iter().for_each(|handle| handle.join().unwrap()); // call join on all handles
@ -9470,7 +9467,6 @@ fn main() {
does_nothing(number_and_bool);
does_nothing(number_and_bool); // If it didn't have copy, this would make an error
}
```
@ -10174,7 +10170,7 @@ fn main() {
.weight(100)
.build(); // This character is okay. Name is fine, height and weight are fine
// Now they are not Character, they are Result<Character, String>. So let's put them in a Vec so we can see them:
// Now they are not Character, they are Result<Character, String>. So let's put them in a Vec so we can see them:
let character_vec = vec![character_with_smurf, character_too_tall, character_too_heavy, okay_character];
for character in character_vec { // Now we will print the character if it's Ok, and print the error if it's Err
@ -10518,11 +10514,10 @@ fn main() {
let brandy = Character::new("Brandy".to_string(), 9, 8, 7, 10, 19, 19, 5, Alignment::Good);
let mut hit_points_vec = vec![]; // Put our hit points data in here
hit_points_vec.push(*billy); // Push *billy?
hit_points_vec.push(*brandy); // Push *brandy?
hit_points_vec.push(*billy); // Push *billy?
hit_points_vec.push(*brandy); // Push *brandy?
println!("{:?}", hit_points_vec);
}
```
@ -10662,8 +10657,6 @@ mod print_things {
pub fn prints_one_thing<T: Display>(input: T) {
println!("{}", input)
}
}
fn main() {
@ -10671,7 +10664,6 @@ fn main() {
let my_billy = Billy::new(3);
my_billy.print_billy();
}
```
@ -11805,15 +11797,15 @@ You can get a char from `u8` using the `From` trait, but for a `u32` you use `Tr
```rust
use std::convert::TryFrom; // You need to brig TryFrom in to use it
use rand::prelude::*; // We will use random numbers too
use rand::prelude::*; // We will use random numbers too
fn main() {
let some_character = char::from(99); // This one is easy - no need for TryFrom
println!("{}", some_character);
let mut random_generator = rand::thread_rng();
// This will try 40,000 times to make a char from a u32.
// The range is 0 (std::u32::MIN) to u32's highest number (std::u32::MAX). If it doesn't work, we will give it '-'.
// This will try 40,000 times to make a char from a u32.
// The range is 0 (std::u32::MIN) to u32's highest number (std::u32::MAX). If it doesn't work, we will give it '-'.
for _ in 0..40_000 {
let bigger_character = char::try_from(random_generator.gen_range(std::u32::MIN, std::u32::MAX)).unwrap_or('-');
print!("{}", bigger_character)
@ -11946,8 +11938,8 @@ fn main() {
let vanuatu = Country::new("Vanuatu", 307_815, 820_000_000);
let micronesia = Country::new("Micronesia", 104_468, 367_000_000);
// We could have given Country a &str instead of a String for the name. But we would have to write lifetimes everywhere
// and that would be too much for a small example. Better to just clone them when we call println!.
// We could have given Country a &str instead of a String for the name. But we would have to write lifetimes everywhere
// and that would be too much for a small example. Better to just clone them when we call println!.
println!("{}", nauru.clone());
println!("{}", nauru.clone() + vanuatu.clone());
println!("{}", nauru + vanuatu + micronesia);
@ -12340,7 +12332,7 @@ impl fmt::Display for Ring { // Display to show who has it and who wants it
}
fn main() {
let mut one_ring = Ring::new("Frodo", "Gollum", "Sauron"); //
let mut one_ring = Ring::new("Frodo", "Gollum", "Sauron");
println!("{}", one_ring);
mem::swap(&mut one_ring.owner, &mut one_ring.former_owner); // Gollum got the ring back for a second
println!("{}", one_ring);
@ -12554,7 +12546,7 @@ The standard library has a prelude too, which is why you don't have to write thi
What if you don't want the prelude for some reason? Just add the attribute `#![no_implicit_prelude]`. Let's give it a try and watch the compiler complain:
```rust
// ⚠️
// ⚠️
#![no_implicit_prelude]
fn main() {
let my_vec = vec![8, 9, 10];
@ -14055,9 +14047,7 @@ impl PrintThing {
}
}
fn main() {
}
fn main() {}
```
@ -14135,9 +14125,7 @@ impl PrintThing {
}
}
fn main() {
}
fn main() {}
```
@ -14146,8 +14134,8 @@ Now it will print:
```text
Crate rust_book
Structs
DoesNothing This is a struct that does nothing
PrintThing This struct only has one method.
DoesNothing This is a struct that does nothing
PrintThing This struct only has one method.
Functions
main
```

Loading…
Cancel
Save