mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 03:09:19 +00:00
chore: Alter whitespace for consistency
* Add newline after "I AM DONE" in exercises for consistency * Remove trailing whitespace from exercises
This commit is contained in:
parent
9f61db5dbe
commit
ffa953a39b
@ -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,9 +32,11 @@ 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 {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
// 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().fold(0.0, |a, b| a + b);
|
||||||
.iter()
|
|
||||||
.fold(0.0, |a, b| a + b);
|
|
||||||
total / values.len()
|
total / values.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,9 @@
|
|||||||
// 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wrapper {
|
impl Wrapper {
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
// 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,8 +17,10 @@ pub struct ReportCard {
|
|||||||
|
|
||||||
impl ReportCard {
|
impl ReportCard {
|
||||||
pub fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
format!("{} ({}) - achieved a grade of {}",
|
format!(
|
||||||
&self.student_name, &self.student_age, &self.grade)
|
"{} ({}) - achieved a grade of {}",
|
||||||
|
&self.student_name, &self.student_age, &self.grade
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +35,10 @@ mod tests {
|
|||||||
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"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -43,6 +49,9 @@ mod tests {
|
|||||||
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+"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9,13 +9,13 @@
|
|||||||
// 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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -18,9 +18,6 @@ trait AppendBar {
|
|||||||
|
|
||||||
//TODO: Add your code here
|
//TODO: Add your code here
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -31,5 +28,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…
x
Reference in New Issue
Block a user