Skip to content

Commit

Permalink
Merge pull request #12 from paulstey/some-more-random-progress
Browse files Browse the repository at this point in the history
feat: some random progress on a few problems
  • Loading branch information
paulstey authored May 29, 2024
2 parents 475e4f8 + 5a6dc05 commit 5ac873e
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 61 deletions.
4 changes: 1 addition & 3 deletions exercises/clippy/clippy1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
// 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 pi = f32::consts::PI;
let radius = 5.00f32;

let area = pi * f32::powi(radius, 2);
Expand Down
6 changes: 2 additions & 4 deletions exercises/clippy/clippy2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// 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 {
while let Some(x) = option {
res += x;
}
println!("{}", res);
Expand Down
21 changes: 6 additions & 15 deletions exercises/clippy/clippy3.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
// clippy3.rs
//
//
// Here's a couple more easy Clippy fixes, so you can see its utility.
//
// Execute `rustlings hint clippy3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

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

let my_arr = &[
-1, -2, -3
-4, -5, -6
];
let my_arr = &[-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];
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);
}
22 changes: 20 additions & 2 deletions exercises/conversions/from_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,28 @@ 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

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

let person_string_split: Vec<_> = s.split(',').collect();
let name = person_string_split[0];

if name.is_empty() || person_string_split.len() != 2 {
return Person::default();
}

let age = match person_string_split[1].parse::<usize>() {
Ok(n) => n,
Err(e) => return Person::default(),
};

Person {
name: name.to_string(),
age,
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions exercises/conversions/using_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
// 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() {
Expand Down
4 changes: 1 addition & 3 deletions exercises/macros/macros1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}

fn main() {
my_macro();
my_macro!();
}
10 changes: 4 additions & 6 deletions exercises/macros/macros2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {
my_macro!();
}

macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}

fn main() {
my_macro!();
}
3 changes: 1 addition & 2 deletions exercises/macros/macros3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

mod macros {
#[macro_export]
macro_rules! my_macro {
() => {
println!("Check out my macro!");
Expand Down
4 changes: 1 addition & 3 deletions exercises/macros/macros4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[rustfmt::skip]
macro_rules! my_macro {
() => {
println!("Check out my macro!");
}
};
($val:expr) => {
println!("Look at this other macro: {}", $val);
}
Expand Down
6 changes: 2 additions & 4 deletions exercises/smart_pointers/arc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
//
// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

#![forbid(unused_imports)] // Do not change this, (or the next) line.
use std::sync::Arc;
use std::thread;

fn main() {
let numbers: Vec<_> = (0..100u32).collect();
let shared_numbers = // TODO
let shared_numbers = Arc::new(numbers);
let mut joinhandles = Vec::new();

for offset in 0..8 {
let child_numbers = // TODO
let child_numbers = Arc::clone(&shared_numbers); // TODO
joinhandles.push(thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
println!("Sum of offset {} is {}", offset, sum);
Expand Down
11 changes: 6 additions & 5 deletions exercises/smart_pointers/cow1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
//
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

use std::borrow::Cow;

fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
Expand Down Expand Up @@ -48,7 +46,8 @@ mod tests {
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => Ok(()),
_ => Err("Expected owned value"),
}
}

Expand All @@ -60,7 +59,8 @@ mod tests {
let slice = vec![0, 1, 2];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}

Expand All @@ -72,7 +72,8 @@ mod tests {
let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
}
3 changes: 1 addition & 2 deletions exercises/threads/threads1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::thread;
use std::time::{Duration, Instant};

Expand All @@ -27,6 +25,7 @@ fn main() {
let mut results: Vec<u128> = vec![];
for handle in handles {
// TODO: a struct is returned from thread::spawn, can you use it?
results.push(handle.join().unwrap());
}

if results.len() != 10 {
Expand Down
11 changes: 5 additions & 6 deletions exercises/threads/threads2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

Expand All @@ -18,14 +16,15 @@ struct JobStatus {
}

fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 });
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));

let mut handles = vec![];
for _ in 0..10 {
let status_shared = Arc::clone(&status);
let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value
status_shared.jobs_completed += 1;
status_shared.lock().unwrap().jobs_completed += 1;
});
handles.push(handle);
}
Expand All @@ -34,6 +33,6 @@ fn main() {
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
// anything interesting in the output? Do you have to 'join' on all the
// handles?
println!("jobs completed {}", ???);
println!("jobs completed {:?}", status.lock().unwrap().jobs_completed);
}
}
5 changes: 2 additions & 3 deletions exercises/threads/threads3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::sync::mpsc;
use std::sync::Arc;
use std::thread;
Expand All @@ -30,6 +28,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc = Arc::new(q);
let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc);
let tx1 = tx.clone();

thread::spawn(move || {
for val in &qc1.first_half {
Expand All @@ -42,7 +41,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
thread::spawn(move || {
for val in &qc2.second_half {
println!("sending {:?}", val);
tx.send(*val).unwrap();
tx1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
Expand Down

0 comments on commit 5ac873e

Please sign in to comment.