Fix spaces

pull/6/head
Sudo Nice 4 years ago
parent 7bd21adee9
commit e571353276

@ -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
@ -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ș"));
}
```
@ -1387,7 +1388,6 @@ fn main() {
my_vec.push(name1); // Now it knows: it's Vec<String>
my_vec.push(name2);
}
```
@ -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 => {}
}
}
}
```
@ -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(),
@ -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);
}
```
@ -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());
}
```

Loading…
Cancel
Save