Merge pull request #6 from sudo-nice/master

Another bunch of fixes
pull/10/head
Dhghomon 4 years ago committed by GitHub
commit 57cbd36daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,7 +65,7 @@ Rust is a new language that already has good textbooks. But sometimes its textbo
- [Multiple threads](#multiple-threads)
- [Arc](#arc)
- [Channels](#channels)
- [Reading Rust documents](#reading-rust-documents)
- [Reading Rust documentation](#reading-rust-documentation)
- [assert_eq!](#assert_eq)
- [Searching](#searching)
- [[src] button](#src-button)
@ -113,8 +113,8 @@ Casting with ```as``` is useful because Rust always needs to know the type of th
fn main() {
let my_number = 100; // We didn't write a type of integer,
// so Rust chooses i32. Rust always
// chooses i32 for integers if you don't
// tell it to use a different type
// chooses i32 for integers if you don't
// tell it to use a different type
println!("{}", my_number as char);
}
@ -167,6 +167,7 @@ So usually the compiler can guess. But sometimes you need to tell it, for two re
2) You want a different type (for example, you want an ```i128```, not an ```i32```).
To specify a type, add a colon after the variable name.
```rust
fn main() {
let small_number: u8 = 10;
@ -182,6 +183,7 @@ fn main() {
```
You can also add ```_``` if you want to make the number easy to read.
```rust
fn main() {
let small_number = 10_u8; // This is easier to read
@ -420,9 +422,9 @@ You can use a code block to return a value:
```rust
fn main() {
let my_number = {
let second_number = 8;
let second_number = 8;
second_number + 9 // No semicolon, so the code block returns 8 + 9.
// It works just like a function
// It works just like a function
};
println!("My number is: {}", my_number);
@ -434,9 +436,9 @@ If you add a semicolon inside the block, it will return ```()``` (nothing):
```rust
fn main() {
let my_number = {
let second_number = 8; // declare second_number,
let second_number = 8; // declare second_number,
second_number + 9; // add 9 to second_number
// but we didn't return it!
// but we didn't return it!
// second_number dies now
};
@ -620,7 +622,7 @@ Without shadowing you would have to think of different names, even though you do
```rust
fn main() {
// Pretending we are using Rust without shadowing
// Pretending we are using Rust without shadowing
let final_number = {
let y = 10;
let x = 9; // x starts at 9
@ -801,7 +803,6 @@ fn main() {
println!("And an f64 is always {:?} bytes. It is Sized.", std::mem::size_of::<f64>());
println!("But a &str? It can be anything. '서태지' is {:?} bytes. It is not Sized.", std::mem::size_of_val("서태지")); // std::mem::size_of_val() gives you the size in bytes of a variable
println!("And 'Adrian Fahrenheit Țepeș' is {:?} bytes. It is not Sized.", std::mem::size_of_val("Adrian Fahrenheit Țepeș"));
}
```
@ -1333,7 +1334,7 @@ This prints ```["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
This method is used a lot to create buffers. For example, ```let mut buffer = [0; 640]``` creates an array of 640 zeroes. Then we can change zero to other numbers in order to add data.
You can index (find) entries in an array with []. The first entry is [0], the second is [1], and so on.
You can index (get) entries in an array with []. The first entry is [0], the second is [1], and so on.
```rust
fn main() {
@ -1387,7 +1388,6 @@ fn main() {
my_vec.push(name1); // Now it knows: it's Vec<String>
my_vec.push(name2);
}
```
@ -1417,7 +1417,7 @@ You can slice a vector too, just like in an array.
```
fn main() {
let vec_of_ten = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Everything is the same except we added vec!
// Everything is the same except we added vec!
let three_to_five = &vec_of_ten[2..5];
let start_at_two = &vec_of_ten[1..];
let end_at_five = &vec_of_ten[..5];
@ -1657,7 +1657,6 @@ You can use _ as many times as you want in a match. In this match on colours, we
```rust
fn match_colours(rbg: (i32, i32, i32)) {
match rbg {
(r, _, _) if r < 10 => println!("Not much red"),
(_, b, _) if b < 10 => println!("Not much blue"),
@ -1894,11 +1893,9 @@ fn check_skystate(state: &ThingsInTheSky) {
}
fn main() {
let time = 8; // it's 8 o'clock
let skystate = create_skystate(time); // create_skystate returns a ThingsInTheSky
check_skystate(&skystate); // Give it a reference so it can read the varible skystate
}
```
@ -1925,11 +1922,9 @@ fn check_skystate(state: &ThingsInTheSky) {
}
fn main() {
let time = 8; // it's 8 o'clock
let skystate = create_skystate(time); // create_skystate returns a ThingsInTheSky
check_skystate(&skystate); // Give it a reference so it can read the varible skystate
}
```
@ -1964,7 +1959,6 @@ impl Number {
false => Number::I32(number), // otherwise just give the number because it's already i32
}
}
}
fn main() {
@ -2304,7 +2298,6 @@ Now that we know how to use loops, here is a better solution to the ```match```
```rust
fn match_colours(rbg: (i32, i32, i32)) {
let new_vec = vec![(rbg.0, "red"), (rbg.1, "blue"), (rbg.2, "green")]; // Put the colours in a vec. Inside are tuples with the colour names
let mut all_have_at_least_10 = true; // Start with true. We will set it to false if one colour is less than 10
for item in new_vec {
@ -2316,7 +2309,6 @@ fn match_colours(rbg: (i32, i32, i32)) {
if all_have_at_least_10 { // Check if it's still true, and print if true
println!("Each colour has at least 10.")
}
}
fn main() {
@ -2327,7 +2319,6 @@ fn main() {
match_colours(first);
match_colours(second);
match_colours(third);
}
```
@ -2887,7 +2878,6 @@ fn main() {
None => {}
}
}
}
```
@ -3570,8 +3560,8 @@ And here is an example of functional style:
```rust
fn main() {
let new_vec = (1..=10).collect::<Vec<i32>>();
// Or you can write it like this:
// let new_vec: Vec<i32> = (1..=10).collect();
// Or you can write it like this:
// let new_vec: Vec<i32> = (1..=10).collect();
println!("{:?}", new_vec);
}
```
@ -3698,7 +3688,6 @@ fn main() {
my_library.add_book("구운몽");
my_library.add_book("吾輩は猫である");
println!("{:?}", my_library.books); // we can print our list of books
}
```
@ -4321,14 +4310,12 @@ struct City {
}
fn main() {
let my_city = City {
name: "Ichinomiya",
date_founded: 1921,
};
println!("{} was founded in {}", my_city.name, my_city.date_founded);
}
```
@ -4342,7 +4329,6 @@ struct City {
}
fn main() {
let city_names = vec!["Ichinomiya".to_string(), "Kurume".to_string()]; // city_names does not live for the whole program
let my_city = City {
@ -4351,7 +4337,6 @@ fn main() {
};
println!("{} was founded in {}", my_city.name, my_city.date_founded);
}
```
@ -4381,7 +4366,6 @@ struct City<'a> { // City has lifetime 'a
}
fn main() {
let city_names = vec!["Ichinomiya".to_string(), "Kurume".to_string()];
let my_city = City {
@ -4390,7 +4374,6 @@ fn main() {
};
println!("{} was founded in {}", my_city.name, my_city.date_founded);
}
```
@ -4494,7 +4477,6 @@ fn main() {
// 10 years later, super_phone_3000 is not on sale anymore
super_phone_3000.on_sale.set(false);
}
```
@ -4529,7 +4511,6 @@ fn main() {
};
println!("{:?}", user_1.active);
}
```
@ -4622,7 +4603,6 @@ fn main() {
*mutex_changer = 6; // mutex_changer is a MutexGuard<i32> so we use * to change the i32
println!("{:?}", mutex_changer); // Now it says 6
}
```
@ -4644,9 +4624,9 @@ fn main() {
If you don't want to use a different ```{}``` code block, you can use ```std::mem::drop(mutex_changer)```. ```std::mem::drop``` means "make this go out of scope".
```rust
use std::sync::Mutex;
```rust
fn main() {
let my_mutex = Mutex::new(5);
let mut mutex_changer = my_mutex.lock().unwrap();
@ -4672,7 +4652,6 @@ fn main() {
// and will wait forever.
println!("This will never print...");
}
```
@ -4686,13 +4665,12 @@ fn main() {
let mut mutex_changer = my_mutex.lock().unwrap();
let mut other_mutex_changer = my_mutex.try_lock(); // try to get the lock
if let Ok(value) = other_mutex_changer { //
if let Ok(value) = other_mutex_changer {
println!("The MutexGuard has: {}", value)
} else {
println!("Didn't get the lock")
}
}
}
```
Also, you don't need to make a variable to change the ```Mutex```. You can just do this:
@ -4791,7 +4769,6 @@ fn main() {
} else {
println!("Couldn't get write access, sorry!")
};
}
```
@ -4827,7 +4804,6 @@ Here is an example to test ```Cow```. We will put a number into a function that
use std::borrow::Cow;
fn modulo_3(input: u8) -> Cow<'static, str> {
match input % 3 {
0 => "Remainder is 0".into(),
1 => "Remainder is 1".into(),
@ -4871,7 +4847,7 @@ type CharacterVec = Vec<char>;
The type is very difficult to read:
```rust
// this return type is extremely long
// this return type is extremely long
fn returns<'a>(input: &'a Vec<char>) -> std::iter::Take<std::iter::Skip<std::slice::Iter<'a, char>>> {
input.iter().skip(4).take(5)
}
@ -5148,7 +5124,6 @@ fn main() {
takes_a_string(user_name);
also_takes_a_string(user_name);
}
fn takes_a_string(input: String) {
@ -5192,7 +5167,6 @@ fn main() {
};
println!("Calgary's history is: {}", calgary.city_history);
}
```
@ -5255,7 +5229,6 @@ fn main() {
};
println!("Calgary's history is: {}", calgary.city_history);
}
```
@ -5299,7 +5272,7 @@ fn main() {
std::thread::spawn(|| {
println!("I am printing something");
});
// Now the threads start.
// Now the threads start.
}
} // How many can finish before main() ends here?
```
@ -5774,7 +5747,6 @@ fn main() {
sender.send(5);
receiver.recv();
}
```
@ -5788,7 +5760,6 @@ fn main() {
sender.send(5).unwrap();
println!("{}", receiver.recv().unwrap());
}
```
@ -5812,7 +5783,6 @@ fn main() {
});
println!("{}", receiver.recv().unwrap());
}
```
@ -5902,14 +5872,14 @@ fn main() {
newvec.push(receiver.recv().unwrap()); // push the results from receiver.recv() into the vec
}
// Now we have a Vec<Vec<u8>>. To put it together we can use .flatten()
// Now we have a Vec<Vec<u8>>. To put it together we can use .flatten()
let newvec = newvec.into_iter().flatten().collect::<Vec<u8>>(); // Now it's one vec of 1000 u8 numbers
}
```
If you print this you can see 1000 number 1s.
# Reading Rust documents
# Reading Rust documentation
It is important to know how to read documentation in Rust so you can understand what other people wrote. Here are some things to know in Rust documentation:

Loading…
Cancel
Save