mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-02-11 20:29:18 +00:00
Update exercises
This commit is contained in:
parent
6313e68f68
commit
1d99518131
@ -9,27 +9,10 @@
|
||||
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
fn main() {
|
||||
let x = 1.2331f64;
|
||||
let y = 1.2332f64;
|
||||
if (x - y).abs() < 0.00001 {
|
||||
println!("Success!");
|
||||
}
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::f32;
|
||||
|
||||
fn main() {
|
||||
let pi = 3.14f32;
|
||||
let radius = 5.00f32;
|
||||
|
||||
let area = pi * f32::powi(radius, 2);
|
||||
|
||||
println!(
|
||||
"The area of a circle with radius {:.2} is {:.5}!",
|
||||
radius, area
|
||||
)
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
@ -24,18 +24,11 @@ impl Default for Person {
|
||||
}
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
// Your task is to complete this implementation
|
||||
// in order for the line `let p = Person::from("Mark,20")` to compile
|
||||
// Please note that you'll need to parse the age component into a `usize`
|
||||
// with something like `"4".parse::<usize>()`. The outcome of this needs to
|
||||
// be handled appropriately.
|
||||
=======
|
||||
// Your task is to complete this implementation in order for the line `let p =
|
||||
// Person::from("Mark,20")` to compile Please note that you'll need to parse the
|
||||
// age component into a `usize` with something like `"4".parse::<usize>()`. The
|
||||
// outcome of this needs to be handled appropriately.
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
//
|
||||
// Steps:
|
||||
// 1. If the length of the provided string is 0, then return the default of
|
||||
|
||||
@ -10,20 +10,12 @@
|
||||
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
// The goal is to make sure that the division does not fail to compile
|
||||
fn average(values: &[f64]) -> f64 {
|
||||
let total = values
|
||||
.iter()
|
||||
.fold(0.0, |a, b| a + b);
|
||||
total / (values.len() as f64)
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
fn average(values: &[f64]) -> f64 {
|
||||
let total = values.iter().sum::<f64>();
|
||||
total / values.len()
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -41,7 +41,6 @@ impl State {
|
||||
}
|
||||
|
||||
fn process(&mut self, message: Message) {
|
||||
<<<<<<< HEAD
|
||||
// TODO: create a match expression to process the different message variants
|
||||
match message {
|
||||
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
|
||||
@ -52,12 +51,6 @@ impl State {
|
||||
},
|
||||
_ => self.quit(),
|
||||
}
|
||||
=======
|
||||
// TODO: create a match expression to process the different message
|
||||
// variants
|
||||
// Remember: When passing a tuple as a function argument, you'll need
|
||||
// extra parentheses: fn function((t, u, p, l, e))
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,11 +68,7 @@ mod tests {
|
||||
};
|
||||
state.process(Message::ChangeColor(255, 0, 255));
|
||||
state.process(Message::Echo(String::from("hello world")));
|
||||
<<<<<<< HEAD
|
||||
state.process(Message::Move{ x: 10, y: 15 });
|
||||
=======
|
||||
state.process(Message::Move(Point { x: 10, y: 15 }));
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
state.process(Message::Quit);
|
||||
|
||||
assert_eq!(state.color, (255, 0, 255));
|
||||
|
||||
@ -9,23 +9,12 @@
|
||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
pub fn generate_nametag_text(name: String) -> Result<String, String> {
|
||||
if name.len() > 0 {
|
||||
Ok(format!("Hi! My name is {}", name))
|
||||
} else {
|
||||
// Empty names aren't allowed.
|
||||
Err("`name` was empty; it must be nonempty.".into())
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
||||
if name.is_empty() {
|
||||
// Empty names aren't allowed.
|
||||
None
|
||||
} else {
|
||||
Some(format!("Hi! My name is {}", name))
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@ enum CreationError {
|
||||
|
||||
impl PositiveNonzeroInteger {
|
||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||
<<<<<<< HEAD:exercises/error_handling/result1.rs
|
||||
if value > 1 {
|
||||
Ok(PositiveNonzeroInteger(value as u64))
|
||||
} else if value == 0 {
|
||||
@ -23,10 +22,6 @@ impl PositiveNonzeroInteger {
|
||||
Err(CreationError::Negative)
|
||||
}
|
||||
|
||||
=======
|
||||
// Hmm... Why is this always returning an Ok value?
|
||||
Ok(PositiveNonzeroInteger(value as u64))
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e:exercises/error_handling/errors4.rs
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,23 +1,11 @@
|
||||
// functions2.rs
|
||||
<<<<<<< HEAD
|
||||
// Make me compile! Execute `rustlings hint functions2` for hints :)s
|
||||
=======
|
||||
//
|
||||
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
|
||||
fn main() {
|
||||
call_me(3);
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
fn call_me(num: i32) {
|
||||
=======
|
||||
fn call_me(num:) {
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
for i in 0..num {
|
||||
println!("Ring! Call number {}", i + 1);
|
||||
}
|
||||
|
||||
@ -6,17 +6,9 @@
|
||||
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
struct Wrapper<T> {
|
||||
value: T
|
||||
}
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Wrapper {
|
||||
value: u32,
|
||||
}
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
|
||||
impl<T> Wrapper<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
|
||||
@ -7,15 +7,12 @@ pub fn bigger(a: i32, b: i32) -> i32 {
|
||||
// Do not use:
|
||||
// - another function call
|
||||
// - additional variables
|
||||
<<<<<<< HEAD
|
||||
// Execute `rustlings hint if1` for hints
|
||||
if (a > b) {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
=======
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
// Don't mind this for now :)
|
||||
|
||||
@ -5,13 +5,7 @@
|
||||
//
|
||||
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
pub fn fizz_if_foo(fizzish: &str) -> &str {
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn foo_if_fizz(fizzish: &str) -> &str {
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
if fizzish == "fizz" {
|
||||
"foo"
|
||||
} else if fizzish == "fuzz" {
|
||||
|
||||
@ -3,12 +3,6 @@
|
||||
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
#[rustfmt::skip]
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
macro_rules! my_macro {
|
||||
() => {
|
||||
println!("Check out my macro!");
|
||||
|
||||
@ -4,17 +4,7 @@
|
||||
// hint.
|
||||
|
||||
mod sausage_factory {
|
||||
<<<<<<< HEAD
|
||||
pub fn make_sausage() {
|
||||
=======
|
||||
// Don't let anybody outside of this module see this!
|
||||
fn get_secret_recipe() -> String {
|
||||
String::from("Ginger")
|
||||
}
|
||||
|
||||
fn make_sausage() {
|
||||
get_secret_recipe();
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
println!("sausage!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,14 +8,8 @@
|
||||
// hint.
|
||||
|
||||
mod delicious_snacks {
|
||||
<<<<<<< HEAD
|
||||
pub use self::fruits::PEAR as fruit;
|
||||
pub use self::veggies::CUCUMBER as veggie;
|
||||
=======
|
||||
// TODO: Fix these use statements
|
||||
use self::fruits::PEAR as ???
|
||||
use self::veggies::CUCUMBER as ???
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
|
||||
mod fruits {
|
||||
pub const PEAR: &'static str = "Pear";
|
||||
|
||||
@ -9,13 +9,7 @@
|
||||
fn main() {
|
||||
// Characters (`char`)
|
||||
|
||||
<<<<<<< HEAD
|
||||
let my_first_initial: char = 'C';
|
||||
=======
|
||||
// Note the _single_ quotes, these are different from the double quotes
|
||||
// you've been seeing around.
|
||||
let my_first_initial = 'C';
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
if my_first_initial.is_alphabetic() {
|
||||
println!("Alphabetical!");
|
||||
} else if my_first_initial.is_numeric() {
|
||||
|
||||
@ -6,20 +6,7 @@
|
||||
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand
|
||||
// for a hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
fn main() {
|
||||
let numbers = (1, 2, 3);
|
||||
println!("The second number is {}", numbers.1);
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
#[test]
|
||||
fn indexing_tuple() {
|
||||
let numbers = (1, 2, 3);
|
||||
// Replace below ??? with the tuple indexing syntax.
|
||||
let second = ???;
|
||||
|
||||
assert_eq!(2, second,
|
||||
"This is not the 2nd number in the tuple!")
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
@ -16,24 +16,8 @@
|
||||
//
|
||||
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
pub fn times_two(num: i32) -> i32 {
|
||||
num * 2
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
pub struct ReportCard {
|
||||
pub grade: f32,
|
||||
pub student_name: String,
|
||||
pub student_age: u8,
|
||||
}
|
||||
|
||||
impl ReportCard {
|
||||
pub fn print(&self) -> String {
|
||||
format!("{} ({}) - achieved a grade of {}",
|
||||
&self.student_name, &self.student_age, &self.grade)
|
||||
}
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -41,7 +25,6 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
<<<<<<< HEAD
|
||||
fn returns_twice_of_positive_numbers() {
|
||||
assert_eq!(times_two(4), 8);
|
||||
}
|
||||
@ -50,31 +33,5 @@ mod tests {
|
||||
fn returns_twice_of_negative_numbers() {
|
||||
// TODO write an assert for `times_two(-4)`
|
||||
assert_eq!(times_two(-4), -8);
|
||||
=======
|
||||
fn generate_numeric_report_card() {
|
||||
let report_card = ReportCard {
|
||||
grade: 2.1,
|
||||
student_name: "Tom Wriggle".to_string(),
|
||||
student_age: 12,
|
||||
};
|
||||
assert_eq!(
|
||||
report_card.print(),
|
||||
"Tom Wriggle (12) - achieved a grade of 2.1"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_alphabetic_report_card() {
|
||||
// TODO: Make sure to change the grade here after you finish the exercise.
|
||||
let report_card = ReportCard {
|
||||
grade: 2.1,
|
||||
student_name: "Gary Plotter".to_string(),
|
||||
student_age: 11,
|
||||
};
|
||||
assert_eq!(
|
||||
report_card.print(),
|
||||
"Gary Plotter (11) - achieved a grade of A+"
|
||||
);
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,19 +33,11 @@ fn main() {
|
||||
}
|
||||
|
||||
pub fn create_empty_list() -> List {
|
||||
<<<<<<< HEAD:exercises/standard_library_types/box1.rs
|
||||
List::Nil
|
||||
}
|
||||
|
||||
pub fn create_non_empty_list() -> List {
|
||||
List::Cons(1, Box::new(List::Nil))
|
||||
=======
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn create_non_empty_list() -> List {
|
||||
todo!()
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e:exercises/smart_pointers/box1.rs
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -45,15 +45,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn unit_structs() {
|
||||
<<<<<<< HEAD
|
||||
// TODO: Instantiate a unit struct!
|
||||
let unit_struct = UnitStruct;
|
||||
let message = format!("{:?}s are fun!", unit_struct);
|
||||
=======
|
||||
// TODO: Instantiate a unit-like struct!
|
||||
// let unit_like_struct =
|
||||
let message = format!("{:?}s are fun!", unit_like_struct);
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
|
||||
assert_eq!(message, "UnitLikeStructs are fun!");
|
||||
}
|
||||
|
||||
@ -17,12 +17,8 @@ struct Package {
|
||||
impl Package {
|
||||
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
|
||||
if weight_in_grams <= 0 {
|
||||
<<<<<<< HEAD
|
||||
// Something goes here...
|
||||
panic!("Package is lighter then air!")
|
||||
=======
|
||||
panic!("Can not ship a weightless package.")
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
} else {
|
||||
Package {
|
||||
sender_country,
|
||||
@ -37,14 +33,9 @@ impl Package {
|
||||
self.recipient_country != self.sender_country
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
fn get_fees(&self, cents_per_kg: i32) -> i32 {
|
||||
// Something goes here... (beware of grams to kg conversion)
|
||||
(self.weight_in_grams * cents_per_kg) / 100
|
||||
=======
|
||||
fn get_fees(&self, cents_per_gram: i32) -> ??? {
|
||||
// Something goes here...
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,13 +77,8 @@ mod tests {
|
||||
let sender_country = String::from("Spain");
|
||||
let recipient_country = String::from("Spain");
|
||||
|
||||
<<<<<<< HEAD
|
||||
let cents_per_kg = 300;
|
||||
|
||||
=======
|
||||
let cents_per_gram = 3;
|
||||
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
let package = Package::new(sender_country, recipient_country, 1500);
|
||||
|
||||
assert_eq!(package.get_fees(cents_per_gram), 4500);
|
||||
|
||||
@ -8,17 +8,11 @@
|
||||
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
use std::sync::{Arc, Mutex};
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn main() {
|
||||
<<<<<<< HEAD
|
||||
// Introduce Mutex
|
||||
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
||||
let status_shared = status.clone();
|
||||
@ -34,29 +28,5 @@ fn main() {
|
||||
while status.lock().unwrap().jobs_completed < 10 {
|
||||
println!("waiting... ");
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
=======
|
||||
let mut handles = vec![];
|
||||
for i in 0..10 {
|
||||
handles.push(thread::spawn(move || {
|
||||
let start = Instant::now();
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
println!("thread {} is complete", i);
|
||||
start.elapsed().as_millis()
|
||||
}));
|
||||
}
|
||||
|
||||
let mut results: Vec<u128> = vec![];
|
||||
for handle in handles {
|
||||
// TODO: a struct is returned from thread::spawn, can you use it?
|
||||
}
|
||||
|
||||
if results.len() != 10 {
|
||||
panic!("Oh no! All the spawned threads did not finish!");
|
||||
}
|
||||
|
||||
println!();
|
||||
for (i, result) in results.into_iter().enumerate() {
|
||||
println!("thread {} took {}ms", i, result);
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,23 +7,14 @@
|
||||
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
// I AM NOT DONE
|
||||
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
trait AppendBar {
|
||||
fn append_bar(self) -> Self;
|
||||
}
|
||||
|
||||
impl AppendBar for String {
|
||||
<<<<<<< HEAD
|
||||
fn append_bar(self) -> Self {
|
||||
self + "Bar"
|
||||
}
|
||||
=======
|
||||
// TODO: Implement `AppendBar` for type `String`.
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -12,16 +12,12 @@ trait AppendBar {
|
||||
fn append_bar(self) -> Self;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
impl AppendBar for Vec<String> {
|
||||
fn append_bar(mut self) -> Self {
|
||||
self.push(String::from("Bar"));
|
||||
self
|
||||
}
|
||||
}
|
||||
=======
|
||||
// TODO: Implement trait `AppendBar` for a vector of strings.
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -4,12 +4,8 @@
|
||||
// hint.
|
||||
|
||||
fn main() {
|
||||
<<<<<<< HEAD
|
||||
let mut x = 3;
|
||||
println!("Number {}", x);
|
||||
x = 5; // don't change this line
|
||||
=======
|
||||
let x: i32;
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
println!("Number {}", x);
|
||||
}
|
||||
|
||||
@ -4,12 +4,6 @@
|
||||
// hint.
|
||||
|
||||
fn main() {
|
||||
<<<<<<< HEAD
|
||||
let x: i32 = 256 * 256 * 256;
|
||||
=======
|
||||
let x = 3;
|
||||
println!("Number {}", x);
|
||||
x = 5; // don't change this line
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
println!("Number {}", x);
|
||||
}
|
||||
|
||||
@ -4,15 +4,8 @@
|
||||
// hint.
|
||||
|
||||
fn main() {
|
||||
<<<<<<< HEAD
|
||||
let number = "3"; // don't change this line
|
||||
println!("Number {}", number);
|
||||
let number = 3;
|
||||
println!("Number {}", number);
|
||||
=======
|
||||
let number = "T-H-R-E-E"; // don't change this line
|
||||
println!("Spell a Number : {}", number);
|
||||
number = 3; // don't rename this variable
|
||||
println!("Number plus two is : {}", number + 2);
|
||||
>>>>>>> 11d8aea96f2c744d970ed1ffb38785cf5b511e5e
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user