mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-08 03:39:18 +00:00
update again
This commit is contained in:
parent
eea0a2957e
commit
0babb9faf7
@ -1,10 +1,8 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut shopping_list: Vec<?> = Vec::new();
|
let mut shopping_list: Vec<&str> = Vec::new();
|
||||||
shopping_list.push("milk");
|
shopping_list.push("milk");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
// This powerful wrapper provides the ability to store a positive integer value.
|
// This powerful wrapper provides the ability to store a positive integer value.
|
||||||
// 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
|
struct Wrapper<T> {
|
||||||
struct Wrapper<u32> {
|
value: T
|
||||||
value: u32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<u32> Wrapper<u32> {
|
impl<T> Wrapper<T> {
|
||||||
pub fn new(value: u32) -> Self {
|
pub fn new(value: T) -> Self {
|
||||||
Wrapper { value }
|
Wrapper { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -18,13 +17,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() {
|
||||||
// TODO: Delete this assert and uncomment the one below once you have finished the exercise.
|
assert_eq!(Wrapper::new("Foo").value, "Foo");
|
||||||
assert!(false);
|
|
||||||
// assert_eq!(Wrapper::new("Foo").value, "Foo");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5,15 +5,15 @@
|
|||||||
|
|
||||||
// Make the necessary code changes to support alphabetical report cards, thereby making the second
|
// Make the necessary code changes to support alphabetical report cards, thereby making the second
|
||||||
// test pass.
|
// test pass.
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
// I AM NOT DONE
|
pub struct ReportCard<T> {
|
||||||
pub struct ReportCard {
|
pub grade: T,
|
||||||
pub grade: f32,
|
|
||||||
pub student_name: String,
|
pub student_name: String,
|
||||||
pub student_age: u8,
|
pub student_age: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReportCard {
|
impl<T:Display> ReportCard<T> {
|
||||||
pub fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
|
format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade)
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ 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: "A+",
|
||||||
student_name: "Gary Plotter".to_string(),
|
student_name: "Gary Plotter".to_string(),
|
||||||
student_age: 11,
|
student_age: 11,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,8 +7,6 @@
|
|||||||
// Execute `rustlings hint iterators3` to get some hints!
|
// Execute `rustlings hint iterators3` to get some hints!
|
||||||
// Have fun :-)
|
// Have fun :-)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum DivisionError {
|
pub enum DivisionError {
|
||||||
NotDivisible(NotDivisibleError),
|
NotDivisible(NotDivisibleError),
|
||||||
@ -70,12 +68,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterator exercises using your `divide` function
|
// Iterator exercises using your `divide` function
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn result_with_list() {
|
fn result_with_list() {
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
let numbers = vec![27, 297, 38502, 81];
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
let x //... Fill in here!
|
let x = division_results.collect::<Result<Vec<_>, _>>();
|
||||||
assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])");
|
assert_eq!(format!("{:?}", x), "Ok([1, 11, 1426, 3])");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,8 +80,7 @@ mod tests {
|
|||||||
fn list_of_results() {
|
fn list_of_results() {
|
||||||
let numbers = vec![27, 297, 38502, 81];
|
let numbers = vec![27, 297, 38502, 81];
|
||||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||||
let x //... Fill in here!
|
let x = division_results.collect::<Vec<_>>();
|
||||||
assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]");
|
assert_eq!(format!("{:?}", x), "[Ok(1), Ok(11), Ok(1426), Ok(3)]");
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
// iterators4.rs
|
// iterators4.rs
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn factorial(num: u64) -> u64 {
|
pub fn factorial(num: u64) -> u64 {
|
||||||
// Complete this function to return the factorial of num
|
// Complete this function to return the factorial of num
|
||||||
// Do not use:
|
// Do not use:
|
||||||
@ -12,6 +10,7 @@ pub fn factorial(num: u64) -> u64 {
|
|||||||
// For an extra challenge, don't use:
|
// For an extra challenge, don't use:
|
||||||
// - recursion
|
// - recursion
|
||||||
// Execute `rustlings hint iterators4` for hints.
|
// Execute `rustlings hint iterators4` for hints.
|
||||||
|
(1..num+1).fold(1, |m, x| m * x)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -8,14 +8,15 @@
|
|||||||
// which appends "Bar" to any object
|
// which appends "Bar" to any object
|
||||||
// implementing this trait.
|
// implementing this trait.
|
||||||
|
|
||||||
// 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
|
fn append_bar(mut self) -> Self {
|
||||||
|
self.push_str("Bar");
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -10,16 +10,16 @@
|
|||||||
// No boiler plate code this time,
|
// No boiler plate code this time,
|
||||||
// you can do this!
|
// you can do this!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Add your code here
|
impl AppendBar for Vec<String> {
|
||||||
|
fn append_bar(mut self) -> Self {
|
||||||
|
self.push(String::from("Bar"));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user