Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rust-lang/rustlings
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: apantina/rustlings-solutions
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 94 files changed
  • 1 contributor

Commits on Sep 15, 2022

  1. Finished rustlings exercises

    apantina committed Sep 15, 2022
    Copy the full SHA
    5d6c7b9 View commit details
Showing with 388 additions and 334 deletions.
  1. +1 −4 exercises/clippy/clippy1.rs
  2. +1 −3 exercises/clippy/clippy2.rs
  3. +4 −7 exercises/clippy/clippy3.rs
  4. +4 −6 exercises/conversions/as_ref_mut.rs
  5. +15 −1 exercises/conversions/from_into.rs
  6. +20 −2 exercises/conversions/from_str.rs
  7. +47 −5 exercises/conversions/try_from_into.rs
  8. +1 −3 exercises/conversions/using_as.rs
  9. +2 −2 exercises/enums/enums1.rs
  10. +4 −3 exercises/enums/enums2.rs
  11. +11 −4 exercises/enums/enums3.rs
  12. +3 −5 exercises/error_handling/errors1.rs
  13. +1 −3 exercises/error_handling/errors2.rs
  14. +2 −3 exercises/error_handling/errors3.rs
  15. +7 −2 exercises/error_handling/errors4.rs
  16. +2 −3 exercises/error_handling/errors5.rs
  17. +4 −6 exercises/error_handling/errors6.rs
  18. +3 −1 exercises/functions/functions1.rs
  19. +1 −2 exercises/functions/functions2.rs
  20. +1 −2 exercises/functions/functions3.rs
  21. +1 −2 exercises/functions/functions4.rs
  22. +1 −2 exercises/functions/functions5.rs
  23. +1 −3 exercises/generics/generics1.rs
  24. +4 −6 exercises/generics/generics2.rs
  25. +4 −2 exercises/hashmaps/hashmaps1.rs
  26. +5 −5 exercises/hashmaps/hashmaps2.rs
  27. +25 −2 exercises/hashmaps/hashmaps3.rs
  28. +3 −2 exercises/if/if1.rs
  29. +5 −3 exercises/if/if2.rs
  30. +0 −1 exercises/intro/intro1.rs
  31. +2 −2 exercises/intro/intro2.rs
  32. +1 −2 exercises/lifetimes/lifetimes1.rs
  33. +2 −4 exercises/lifetimes/lifetimes2.rs
  34. +3 −5 exercises/lifetimes/lifetimes3.rs
  35. +1 −3 exercises/macros/macros1.rs
  36. +3 −5 exercises/macros/macros2.rs
  37. +1 −2 exercises/macros/macros3.rs
  38. +1 −3 exercises/macros/macros4.rs
  39. +2 −3 exercises/modules/modules1.rs
  40. +2 −3 exercises/modules/modules2.rs
  41. +1 −2 exercises/modules/modules3.rs
  42. +2 −3 exercises/move_semantics/move_semantics1.rs
  43. +5 −8 exercises/move_semantics/move_semantics2.rs
  44. +4 −6 exercises/move_semantics/move_semantics3.rs
  45. +2 −5 exercises/move_semantics/move_semantics4.rs
  46. +1 −2 exercises/move_semantics/move_semantics5.rs
  47. +5 −6 exercises/move_semantics/move_semantics6.rs
  48. +8 −4 exercises/options/options1.rs
  49. +6 −6 exercises/options/options2.rs
  50. +1 −3 exercises/options/options3.rs
  51. +1 −2 exercises/primitive_types/primitive_types1.rs
  52. +1 −2 exercises/primitive_types/primitive_types2.rs
  53. +1 −2 exercises/primitive_types/primitive_types3.rs
  54. +1 −2 exercises/primitive_types/primitive_types4.rs
  55. +1 −2 exercises/primitive_types/primitive_types5.rs
  56. +1 −2 exercises/primitive_types/primitive_types6.rs
  57. +7 −2 exercises/quiz1.rs
  58. +15 −6 exercises/quiz2.rs
  59. +4 −6 exercises/quiz3.rs
  60. +2 −4 exercises/standard_library_types/arc1.rs
  61. +3 −4 exercises/standard_library_types/box1.rs
  62. +2 −4 exercises/standard_library_types/cow1.rs
  63. +5 −6 exercises/standard_library_types/iterators1.rs
  64. +3 −5 exercises/standard_library_types/iterators2.rs
  65. +15 −5 exercises/standard_library_types/iterators3.rs
  66. +5 −2 exercises/standard_library_types/iterators4.rs
  67. +2 −4 exercises/standard_library_types/iterators5.rs
  68. +6 −7 exercises/standard_library_types/rc1.rs
  69. +1 −3 exercises/strings/strings1.rs
  70. +1 −3 exercises/strings/strings2.rs
  71. +3 −7 exercises/strings/strings3.rs
  72. +10 −12 exercises/strings/strings4.rs
  73. +11 −7 exercises/structs/structs1.rs
  74. +5 −2 exercises/structs/structs2.rs
  75. +4 −6 exercises/structs/structs3.rs
  76. +1 −2 exercises/tests/tests1.rs
  77. +2 −3 exercises/tests/tests2.rs
  78. +2 −4 exercises/tests/tests3.rs
  79. +3 −4 exercises/threads/threads1.rs
  80. +5 −5 exercises/threads/threads2.rs
  81. +4 −4 exercises/threads/threads3.rs
  82. +3 −3 exercises/traits/traits1.rs
  83. +7 −4 exercises/traits/traits2.rs
  84. +3 −2 exercises/traits/traits3.rs
  85. +1 −3 exercises/traits/traits4.rs
  86. +1 −3 exercises/traits/traits5.rs
  87. +1 −2 exercises/variables/variables1.rs
  88. +1 −2 exercises/variables/variables2.rs
  89. +1 −1 exercises/variables/variables3.rs
  90. +1 −2 exercises/variables/variables4.rs
  91. +1 −2 exercises/variables/variables5.rs
  92. +1 −3 exercises/variables/variables6.rs
  93. +2 −2 exercises/vecs/vecs1.rs
  94. +2 −4 exercises/vecs/vecs2.rs
5 changes: 1 addition & 4 deletions exercises/clippy/clippy1.rs
Original file line number Diff line number Diff line change
@@ -6,15 +6,12 @@
// check clippy's suggestions from the output to solve the exercise.
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

use std::f32;

fn main() {
let pi = 3.14f32;
let radius = 5.00f32;

let area = pi * f32::powi(radius, 2);
let area = f32::consts::PI * f32::powi(radius, 2);

println!(
"The area of a circle with radius {:.2} is {:.5}!",
4 changes: 1 addition & 3 deletions exercises/clippy/clippy2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// clippy2.rs
// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

fn main() {
let mut res = 42;
let option = Some(12);
for x in option {
if let Some(x) = option {
res += x;
}
println!("{}", res);
11 changes: 4 additions & 7 deletions exercises/clippy/clippy3.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
// clippy3.rs
// Here's a couple more easy Clippy fixes, so you can see its utility.

// I AM NOT DONE

#[allow(unused_variables, unused_assignments)]
fn main() {
let my_option: Option<()> = None;
if my_option.is_none() {
if let Some(x) = my_option {
my_option.unwrap();
}

let my_arr = &[
-1, -2, -3
-1, -2, -3,
-4, -5, -6
];
println!("My array! Here it is: {:?}", my_arr);

let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
let my_empty_vec = vec![1, 2, 3, 4, 5].clear();
println!("This Vec is empty, see? {:?}", my_empty_vec);

let mut value_a = 45;
let mut value_b = 66;
// Let's swap these two!
value_a = value_b;
value_b = value_a;
std::mem::swap(&mut value_a, &mut value_b);
println!("value a: {}; value b: {}", value_a, value_b);
}
10 changes: 4 additions & 6 deletions exercises/conversions/as_ref_mut.rs
Original file line number Diff line number Diff line change
@@ -3,24 +3,22 @@
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

// Obtain the number of bytes (not characters) in the given argument
// Add the AsRef trait appropriately as a trait bound
fn byte_counter<T>(arg: T) -> usize {
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().as_bytes().len()
}

// Obtain the number of characters (not bytes) in the given argument
// Add the AsRef trait appropriately as a trait bound
fn char_counter<T>(arg: T) -> usize {
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().chars().count()
}

// Squares a number using AsMut. Add the trait bound as is appropriate and
// implement the function body.
fn num_sq<T>(arg: &mut T) {
???
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
*arg.as_mut() *= *arg.as_mut()
}

#[cfg(test)]
16 changes: 15 additions & 1 deletion exercises/conversions/from_into.rs
Original file line number Diff line number Diff line change
@@ -35,10 +35,24 @@ impl Default for Person {
// If while parsing the age, something goes wrong, then return the default of Person
// Otherwise, then return an instantiated Person object with the results

// I AM NOT DONE
use std::str::FromStr;

impl From<&str> for Person {
fn from(s: &str) -> Person {
if s.is_empty() {
return Person::default();
}

let elements: Vec<&str> = s.split(',').collect();
if elements[0].is_empty() || elements.len() != 2 {
return Person::default();
}

let res =usize::from_str(elements[1]);
match res {
Ok(age) => return Person{ name: String::from(elements[0]), age },
Err(_) => return Person::default()
}
}
}

22 changes: 20 additions & 2 deletions exercises/conversions/from_str.rs
Original file line number Diff line number Diff line change
@@ -28,8 +28,6 @@ enum ParsePersonError {
ParseInt(ParseIntError),
}

// I AM NOT DONE

// Steps:
// 1. If the length of the provided string is 0, an error should be returned
// 2. Split the given string on the commas present in it
@@ -46,6 +44,24 @@ enum ParsePersonError {
impl FromStr for Person {
type Err = ParsePersonError;
fn from_str(s: &str) -> Result<Person, Self::Err> {
if s.is_empty() {
return Err(ParsePersonError::Empty);
}

let elements: Vec<&str> = s.split(',').collect();
if elements[0].is_empty() {
return Err(ParsePersonError::NoName);
}

if elements.len() != 2 {
return Err(ParsePersonError::BadLen);
}

let res = usize::from_str(elements[1]);
match res {
Ok(age) => return Ok(Person { name: String::from(elements[0]), age }),
Err(e) => return Err(ParsePersonError::ParseInt(e)),
}
}
}

@@ -62,6 +78,7 @@ mod tests {
fn empty_input() {
assert_eq!("".parse::<Person>(), Err(ParsePersonError::Empty));
}

#[test]
fn good_input() {
let p = "John,32".parse::<Person>();
@@ -70,6 +87,7 @@ mod tests {
assert_eq!(p.name, "John");
assert_eq!(p.age, 32);
}

#[test]
fn missing_age() {
assert!(matches!(
52 changes: 47 additions & 5 deletions exercises/conversions/try_from_into.rs
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@ enum IntoColorError {
IntConversion,
}

// I AM NOT DONE

// Your task is to complete this implementation
// and return an Ok result of inner type Color.
// You need to create an implementation for a tuple of three integers,
@@ -38,20 +36,51 @@ enum IntoColorError {
impl TryFrom<(i16, i16, i16)> for Color {
type Error = IntoColorError;
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
if tuple.0 > 255 || tuple.0 < 0 || tuple.1 > 255 || tuple.1 < 0 || tuple.2 > 255 || tuple.2 < 0 {
return Err(IntoColorError::IntConversion);
}

Ok(Color {
red: tuple.0 as u8,
green: tuple.1 as u8,
blue: tuple.2 as u8,
})
}
}

// Array implementation
impl TryFrom<[i16; 3]> for Color {
type Error = IntoColorError;
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
if arr[0] > 255 || arr[0] < 0 || arr[1] > 255 || arr[1] < 0 || arr[2] > 255 || arr[2] < 0 {
return Err(IntoColorError::IntConversion);
}

Ok(Color {
red: arr[0] as u8,
green: arr[1] as u8,
blue: arr[2] as u8,
})
}
}

// Slice implementation
impl TryFrom<&[i16]> for Color {
type Error = IntoColorError;
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
if slice.len() != 3 {
return Err(IntoColorError::BadLen);
}

if slice[0] > 255 || slice[0] < 0 || slice[1] > 255 || slice[1] < 0 || slice[2] > 255 || slice[2] < 0 {
return Err(IntoColorError::IntConversion);
}

Ok(Color {
red: slice[0] as u8,
green: slice[1] as u8,
blue: slice[2] as u8,
})
}
}

@@ -84,20 +113,23 @@ mod tests {
Err(IntoColorError::IntConversion)
);
}

#[test]
fn test_tuple_out_of_range_negative() {
assert_eq!(
Color::try_from((-1, -10, -256)),
Err(IntoColorError::IntConversion)
);
}

#[test]
fn test_tuple_sum() {
assert_eq!(
Color::try_from((-1, 255, 255)),
Err(IntoColorError::IntConversion)
);
}

#[test]
fn test_tuple_correct() {
let c: Result<Color, _> = (183, 65, 14).try_into();
@@ -107,25 +139,29 @@ mod tests {
Color {
red: 183,
green: 65,
blue: 14
blue: 14,
}
);
}

#[test]
fn test_array_out_of_range_positive() {
let c: Result<Color, _> = [1000, 10000, 256].try_into();
assert_eq!(c, Err(IntoColorError::IntConversion));
}

#[test]
fn test_array_out_of_range_negative() {
let c: Result<Color, _> = [-10, -256, -1].try_into();
assert_eq!(c, Err(IntoColorError::IntConversion));
}

#[test]
fn test_array_sum() {
let c: Result<Color, _> = [-1, 255, 255].try_into();
assert_eq!(c, Err(IntoColorError::IntConversion));
}

#[test]
fn test_array_correct() {
let c: Result<Color, _> = [183, 65, 14].try_into();
@@ -135,10 +171,11 @@ mod tests {
Color {
red: 183,
green: 65,
blue: 14
blue: 14,
}
);
}

#[test]
fn test_slice_out_of_range_positive() {
let arr = [10000, 256, 1000];
@@ -147,6 +184,7 @@ mod tests {
Err(IntoColorError::IntConversion)
);
}

#[test]
fn test_slice_out_of_range_negative() {
let arr = [-256, -1, -10];
@@ -155,6 +193,7 @@ mod tests {
Err(IntoColorError::IntConversion)
);
}

#[test]
fn test_slice_sum() {
let arr = [-1, 255, 255];
@@ -163,6 +202,7 @@ mod tests {
Err(IntoColorError::IntConversion)
);
}

#[test]
fn test_slice_correct() {
let v = vec![183, 65, 14];
@@ -173,15 +213,17 @@ mod tests {
Color {
red: 183,
green: 65,
blue: 14
blue: 14,
}
);
}

#[test]
fn test_slice_excess_length() {
let v = vec![0, 0, 0, 0];
assert_eq!(Color::try_from(&v[..]), Err(IntoColorError::BadLen));
}

#[test]
fn test_slice_insufficient_length() {
let v = vec![0, 0];
4 changes: 1 addition & 3 deletions exercises/conversions/using_as.rs
Original file line number Diff line number Diff line change
@@ -6,11 +6,9 @@
// and returns the proper type.
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

fn average(values: &[f64]) -> f64 {
let total = values.iter().sum::<f64>();
total / values.len()
total / values.len() as f64
}

fn main() {
4 changes: 2 additions & 2 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// enums1.rs
// No hints this time! ;)

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo, Move, ChangeColor
}

fn main() {
7 changes: 4 additions & 3 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// enums2.rs
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Quit,
Echo(String),
Move{x:i32, y:usize},
ChangeColor(i32,i32,i32)
}

impl Message {
Loading