mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 23:04:17 -05:00
Auto merge of #171 - miller-time:rustfmt-exercises, r=komaeda
chore: Clean up some formatting in exercises I noticed some formatting that isn't consistent with the `rustfmt` style and tried my best to run it on the files in the exercises directory (which does fail for files that can't compile!), which just caught some minor whitespace things here and there. Note: also can't just apply `rustfmt` blindly because of the blank lines that lead to the hint comments
This commit is contained in:
commit
5f16469807
11 changed files with 19 additions and 19 deletions
|
@ -40,7 +40,7 @@ fn square(num: i32) -> i32 {
|
||||||
|
|
||||||
// This is a really common error that can be fixed by removing one character.
|
// This is a really common error that can be fixed by removing one character.
|
||||||
// It happens because Rust distinguishes between expressions and statements: expressions return
|
// It happens because Rust distinguishes between expressions and statements: expressions return
|
||||||
// a value based on its operand, and statements simply return a () type which behaves just like `void` in C/C++ language.
|
// a value based on its operand, and statements simply return a () type which behaves just like `void` in C/C++ language.
|
||||||
// We want to return a value of `i32` type from the `square` function, but it is returning a `()` type...
|
// We want to return a value of `i32` type from the `square` function, but it is returning a `()` type...
|
||||||
// They are not the same. There are two solutions:
|
// They are not the same. There are two solutions:
|
||||||
// 1. Add a `return` ahead of `num * num;`
|
// 1. Add a `return` ahead of `num * num;`
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// if1.rs
|
// if1.rs
|
||||||
|
|
||||||
pub fn bigger(a: i32, b:i32) -> i32 {
|
pub fn bigger(a: i32, b: i32) -> i32 {
|
||||||
// Complete this function to return the bigger number!
|
// Complete this function to return the bigger number!
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - return
|
// - return
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// modules2.rs
|
// modules2.rs
|
||||||
// Make me compile! Scroll down for hints :)
|
// Make me compile! Scroll down for hints :)
|
||||||
|
|
||||||
mod delicious_snacks {
|
mod delicious_snacks {
|
||||||
use self::fruits::PEAR as fruit;
|
use self::fruits::PEAR as fruit;
|
||||||
use self::veggies::CUCUMBER as veggie;
|
use self::veggies::CUCUMBER as veggie;
|
||||||
|
|
||||||
|
@ -17,9 +17,11 @@ mod delicious_snacks {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("favorite snacks: {} and {}",
|
println!(
|
||||||
delicious_snacks::fruit,
|
"favorite snacks: {} and {}",
|
||||||
delicious_snacks::veggie);
|
delicious_snacks::fruit,
|
||||||
|
delicious_snacks::veggie
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,6 @@ fn main() {
|
||||||
vec1.push(88);
|
vec1.push(88);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
|
|
|
@ -12,7 +12,6 @@ fn main() {
|
||||||
vec1.push(88);
|
vec1.push(88);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
|
|
|
@ -13,7 +13,6 @@ fn main() {
|
||||||
vec1.push(88);
|
vec1.push(88);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
|
|
|
@ -13,7 +13,6 @@ fn main() {
|
||||||
vec1.push(88);
|
vec1.push(88);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// `fill_vec()` no longer take `vec: Vec<i32>` as argument
|
// `fill_vec()` no longer take `vec: Vec<i32>` as argument
|
||||||
|
|
|
@ -38,8 +38,8 @@ fn main() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// While you could use a destructuring `let` for the tuple here, try
|
// While you could use a destructuring `let` for the tuple here, try
|
||||||
// indexing into it instead, as explained in the last example of the
|
// indexing into it instead, as explained in the last example of the
|
||||||
// Data Types -> The Tuple Type section of the book:
|
// Data Types -> The Tuple Type section of the book:
|
||||||
// https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-tuple-type
|
// https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-tuple-type
|
||||||
// Now you have another tool in your toolbox!
|
// Now you have another tool in your toolbox!
|
||||||
|
|
|
@ -13,8 +13,7 @@ fn main() {
|
||||||
let mut joinhandles = Vec::new();
|
let mut joinhandles = Vec::new();
|
||||||
|
|
||||||
for offset in 0..8 {
|
for offset in 0..8 {
|
||||||
joinhandles.push(
|
joinhandles.push(thread::spawn(move || {
|
||||||
thread::spawn(move || {
|
|
||||||
let mut i = offset;
|
let mut i = offset;
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
while i < child_numbers.len() {
|
while i < child_numbers.len() {
|
||||||
|
|
|
@ -23,8 +23,7 @@ pub struct NotDivisibleError {
|
||||||
// This function should calculate `a` divided by `b` if `a` is
|
// This function should calculate `a` divided by `b` if `a` is
|
||||||
// evenly divisible by b.
|
// evenly divisible by b.
|
||||||
// Otherwise, it should return a suitable error.
|
// Otherwise, it should return a suitable error.
|
||||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -40,7 +39,7 @@ mod tests {
|
||||||
fn test_not_divisible() {
|
fn test_not_divisible() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
divide(81, 6),
|
divide(81, 6),
|
||||||
Err(DivisionError::NotDivisible(NotDivisibleError{
|
Err(DivisionError::NotDivisible(NotDivisibleError {
|
||||||
dividend: 81,
|
dividend: 81,
|
||||||
divisor: 6
|
divisor: 6
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -7,8 +7,12 @@
|
||||||
// you think each value is. That is, add either `string_slice` or `string`
|
// you think each value is. That is, add either `string_slice` or `string`
|
||||||
// before the parentheses on each line. If you're right, it will compile!
|
// before the parentheses on each line. If you're right, it will compile!
|
||||||
|
|
||||||
fn string_slice(arg: &str) { println!("{}", arg); }
|
fn string_slice(arg: &str) {
|
||||||
fn string(arg: String) { println!("{}", arg); }
|
println!("{}", arg);
|
||||||
|
}
|
||||||
|
fn string(arg: String) {
|
||||||
|
println!("{}", arg);
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
("blue");
|
("blue");
|
||||||
|
|
Loading…
Reference in a new issue