mirror of
https://github.com/rust-lang/rustlings
synced 2024-11-05 00:00:12 +00:00
Merge pull request #468 from benjaminfjones/fix/exercise_whitespace
chore: change exercise whitespace for consistency
This commit is contained in:
commit
c2bfcf1c54
@ -3,6 +3,7 @@
|
|||||||
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
// Obtain the number of bytes (not characters) in the given argument
|
// Obtain the number of bytes (not characters) in the given argument
|
||||||
// Add the AsRef trait appropriately as a trait bound
|
// Add the AsRef trait appropriately as a trait bound
|
||||||
fn byte_counter<T>(arg: T) -> usize {
|
fn byte_counter<T>(arg: T) -> usize {
|
||||||
|
@ -18,7 +18,6 @@ impl Default for Person {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
// Your task is to complete this implementation
|
// Your task is to complete this implementation
|
||||||
// in order for the line `let p = Person::from("Mark,20")` to compile
|
// 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`
|
// Please note that you'll need to parse the age component into a `usize`
|
||||||
@ -33,6 +32,9 @@ impl Default for Person {
|
|||||||
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
|
||||||
// If while parsing the age, something goes wrong, then return the default of 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
|
// Otherwise, then return an instantiated Person object with the results
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
impl From<&str> for Person {
|
impl From<&str> for Person {
|
||||||
fn from(s: &str) -> Person {
|
fn from(s: &str) -> Person {
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
// Type casting in Rust is done via the usage of the `as` operator.
|
// Type casting in Rust is done via the usage of the `as` operator.
|
||||||
// Please note that the `as` operator is not only used when type casting.
|
// Please note that the `as` operator is not only used when type casting.
|
||||||
// It also helps with renaming imports.
|
// It also helps with renaming imports.
|
||||||
|
//
|
||||||
|
// The goal is to make sure that the division does not fail to compile
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
// The goal is to make sure that the division does not fail to compile
|
|
||||||
fn average(values: &[f64]) -> f64 {
|
fn average(values: &[f64]) -> f64 {
|
||||||
let total = values
|
let total = values
|
||||||
.iter()
|
.iter()
|
||||||
@ -14,4 +16,4 @@ fn average(values: &[f64]) -> f64 {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let values = [3.5, 0.3, 13.0, 11.7];
|
let values = [3.5, 0.3, 13.0, 11.7];
|
||||||
println!("{}", average(&values));
|
println!("{}", average(&values));
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// This shopping list program isn't compiling!
|
// This shopping list program isn't compiling!
|
||||||
// Use your knowledge of generics to fix it.
|
// Use your knowledge of generics to fix it.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
// Rewrite it using generics so that it supports wrapping ANY type.
|
// Rewrite it using generics so that it supports wrapping ANY type.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
struct Wrapper {
|
struct Wrapper {
|
||||||
value: u32
|
value: u32
|
||||||
}
|
}
|
||||||
@ -18,11 +19,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_u32_in_wrapper() {
|
fn store_u32_in_wrapper() {
|
||||||
assert_eq!(Wrapper::new(42).value, 42);
|
assert_eq!(Wrapper::new(42).value, 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn store_str_in_wrapper() {
|
fn store_str_in_wrapper() {
|
||||||
assert_eq!(Wrapper::new("Foo").value, "Foo");
|
assert_eq!(Wrapper::new("Foo").value, "Foo");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
// An imaginary magical school has a new report card generation system written in Rust!
|
// An imaginary magical school has a new report card generation system written in Rust!
|
||||||
// Currently the system only supports creating report cards where the student's grade
|
// Currently the system only supports creating report cards where the student's grade
|
||||||
// is represented numerically (e.g. 1.0 -> 5.5).
|
// is represented numerically (e.g. 1.0 -> 5.5).
|
||||||
// However, the school also issues alphabetical grades (A+ -> F-) and needs
|
// However, the school also issues alphabetical grades (A+ -> F-) and needs
|
||||||
// to be able to print both types of report card!
|
// to be able to print both types of report card!
|
||||||
|
|
||||||
// Make the necessary code changes to support alphabetical report cards, thereby making
|
// Make the necessary code changes to support alphabetical report cards, thereby making
|
||||||
// the second test pass.
|
// the second test pass.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
pub struct ReportCard {
|
pub struct ReportCard {
|
||||||
pub grade: f32,
|
pub grade: f32,
|
||||||
pub student_name: String,
|
pub student_name: String,
|
||||||
@ -16,7 +17,7 @@ pub struct ReportCard {
|
|||||||
|
|
||||||
impl ReportCard {
|
impl ReportCard {
|
||||||
pub fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
format!("{} ({}) - achieved a grade of {}",
|
format!("{} ({}) - achieved a grade of {}",
|
||||||
&self.student_name, &self.student_age, &self.grade)
|
&self.student_name, &self.student_age, &self.grade)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -28,8 +29,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn generate_numeric_report_card() {
|
fn generate_numeric_report_card() {
|
||||||
let report_card = ReportCard {
|
let report_card = ReportCard {
|
||||||
grade: 2.1,
|
grade: 2.1,
|
||||||
student_name: "Tom Wriggle".to_string(),
|
student_name: "Tom Wriggle".to_string(),
|
||||||
student_age: 12,
|
student_age: 12,
|
||||||
};
|
};
|
||||||
assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1");
|
assert_eq!(report_card.print(), "Tom Wriggle (12) - achieved a grade of 2.1");
|
||||||
@ -39,10 +40,10 @@ mod tests {
|
|||||||
fn generate_alphabetic_report_card() {
|
fn generate_alphabetic_report_card() {
|
||||||
// TODO: Make sure to change the grade here after you finish the exercise.
|
// TODO: Make sure to change the grade here after you finish the exercise.
|
||||||
let report_card = ReportCard {
|
let report_card = ReportCard {
|
||||||
grade: 2.1,
|
grade: 2.1,
|
||||||
student_name: "Gary Plotter".to_string(),
|
student_name: "Gary Plotter".to_string(),
|
||||||
student_age: 11,
|
student_age: 11,
|
||||||
};
|
};
|
||||||
assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+");
|
assert_eq!(report_card.print(), "Gary Plotter (11) - achieved a grade of A+");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// primitive_types3.rs
|
// primitive_types3.rs
|
||||||
// Create an array with at least 100 elements in it where the ??? is.
|
// Create an array with at least 100 elements in it where the ??? is.
|
||||||
// Execute `rustlings hint primitive_types3` for hints!
|
// Execute `rustlings hint primitive_types3` for hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
@ -47,7 +47,7 @@ mod tests {
|
|||||||
fn create_international_package() {
|
fn create_international_package() {
|
||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("Spain");
|
||||||
let recipient_country = String::from("Russia");
|
let recipient_country = String::from("Russia");
|
||||||
|
|
||||||
let package = Package::new(sender_country, recipient_country, 1200);
|
let package = Package::new(sender_country, recipient_country, 1200);
|
||||||
|
|
||||||
assert!(package.is_international());
|
assert!(package.is_international());
|
||||||
@ -59,9 +59,9 @@ mod tests {
|
|||||||
let recipient_country = String::from("Spain");
|
let recipient_country = String::from("Spain");
|
||||||
|
|
||||||
let cents_per_kg = ???;
|
let cents_per_kg = ???;
|
||||||
|
|
||||||
let package = Package::new(sender_country, recipient_country, 1500);
|
let package = Package::new(sender_country, recipient_country, 1500);
|
||||||
|
|
||||||
assert_eq!(package.get_fees(cents_per_kg), 4500);
|
assert_eq!(package.get_fees(cents_per_kg), 4500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
// traits1.rs
|
// traits1.rs
|
||||||
// Time to implement some traits!
|
// Time to implement some traits!
|
||||||
//
|
//
|
||||||
// Your task is to implement the trait
|
// Your task is to implement the trait
|
||||||
// `AppendBar' for the type `String'.
|
// `AppendBar' for the type `String'.
|
||||||
//
|
//
|
||||||
// The trait AppendBar has only one function,
|
// The trait AppendBar has only one function,
|
||||||
// which appends "Bar" to any object
|
// which appends "Bar" to any object
|
||||||
// implementing this trait.
|
// implementing this trait.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppendBar for String {
|
impl AppendBar for String {
|
||||||
//Add your code here
|
//Add your code here
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -40,5 +40,4 @@ mod tests {
|
|||||||
String::from("BarBar")
|
String::from("BarBar")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
// traits2.rs
|
// traits2.rs
|
||||||
//
|
//
|
||||||
// Your task is to implement the trait
|
// Your task is to implement the trait
|
||||||
// `AppendBar' for a vector of strings.
|
// `AppendBar' for a vector of strings.
|
||||||
//
|
//
|
||||||
// To implement this trait, consider for
|
// To implement this trait, consider for
|
||||||
// a moment what it means to 'append "Bar"'
|
// a moment what it means to 'append "Bar"'
|
||||||
// to a vector of strings.
|
// to a vector of strings.
|
||||||
//
|
//
|
||||||
// No boiler plate code this time,
|
// No boiler plate code this time,
|
||||||
// you can do this!
|
// you can do this!
|
||||||
|
|
||||||
@ -31,5 +31,4 @@ mod tests {
|
|||||||
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
|
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
|
||||||
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
|
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user