mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-03 01:09:18 +00:00
jnjjj
This commit is contained in:
parent
c29bf477b7
commit
6807a71031
13
cache/solidity-files-cache.json
vendored
Normal file
13
cache/solidity-files-cache.json
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"_format": "ethers-rs-sol-cache-3",
|
||||||
|
"paths": {
|
||||||
|
"artifacts": "out",
|
||||||
|
"sources": "src",
|
||||||
|
"tests": "test",
|
||||||
|
"scripts": "script",
|
||||||
|
"libraries": [
|
||||||
|
"lib"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"files": {}
|
||||||
|
}
|
||||||
@ -9,14 +9,14 @@
|
|||||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
|
||||||
|
pub fn generate_nametag_text(name: String) -> Result<String,String> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
// Empty names aren't allowed.
|
||||||
None
|
Err("`name` was empty; it must be nonempty.".to_string())
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {}", name))
|
Ok(format!("Hi! My name is {}", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,14 +19,13 @@
|
|||||||
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
let qty = item_quantity.parse::<i32>();
|
let qty = item_quantity.parse::<i32>()?;
|
||||||
|
|
||||||
Ok(qty * cost_per_item + processing_fee)
|
Ok(qty * cost_per_item + processing_fee)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
@ -15,7 +14,7 @@ fn main() {
|
|||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
let cost = total_cost(pretend_user_input)?;
|
let cost = total_cost(pretend_user_input).unwrap();
|
||||||
|
|
||||||
if cost > tokens {
|
if cost > tokens {
|
||||||
println!("You can't afford that many!");
|
println!("You can't afford that many!");
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
@ -17,7 +16,18 @@ enum CreationError {
|
|||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
// Hmm... Why is this always returning an Ok value?
|
// Hmm... Why is this always returning an Ok value?
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
if value > 0
|
||||||
|
{
|
||||||
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
|
}
|
||||||
|
else if value == 0
|
||||||
|
{
|
||||||
|
Err(CreationError::Zero)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Err(CreationError::Negative)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,14 +22,13 @@
|
|||||||
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// TODO: update the return type of `main()` to make this compile.
|
// TODO: update the return type of `main()` to make this compile.
|
||||||
fn main() -> Result<(), Box<dyn ???>> {
|
fn main() -> Result<(), Box<dyn error::Error >> {
|
||||||
let pretend_user_input = "42";
|
let pretend_user_input = "42";
|
||||||
let x: i64 = pretend_user_input.parse()?;
|
let x: i64 = pretend_user_input.parse()?;
|
||||||
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
||||||
|
|||||||
@ -9,7 +9,6 @@
|
|||||||
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
@ -25,13 +24,16 @@ impl ParsePosNonzeroError {
|
|||||||
ParsePosNonzeroError::Creation(err)
|
ParsePosNonzeroError::Creation(err)
|
||||||
}
|
}
|
||||||
// TODO: add another error conversion function here.
|
// TODO: add another error conversion function here.
|
||||||
// fn from_parseint...
|
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
|
||||||
|
ParsePosNonzeroError::ParseInt(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
||||||
// TODO: change this to return an appropriate error instead of panicking
|
// TODO: change this to return an appropriate error instead of panicking
|
||||||
// when `parse()` returns an error.
|
// when `parse()` returns an error.
|
||||||
let x: i64 = s.parse().unwrap();
|
let x : i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
|
||||||
|
|
||||||
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,9 +6,8 @@
|
|||||||
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut shopping_list: Vec<?> = Vec::new();
|
let mut shopping_list: Vec<&'static str> = Vec::new();
|
||||||
shopping_list.push("milk");
|
shopping_list.push("milk");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,14 +6,13 @@
|
|||||||
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Wrapper {
|
struct Wrapper<T> {
|
||||||
value: u32,
|
value: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wrapper {
|
impl<T> Wrapper<T> {
|
||||||
pub fn new(value: u32) -> Self {
|
pub fn new(value: T) -> Self {
|
||||||
Wrapper { value }
|
Wrapper { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
@ -15,6 +15,9 @@ trait AppendBar {
|
|||||||
|
|
||||||
impl AppendBar for String {
|
impl AppendBar for String {
|
||||||
// TODO: Implement `AppendBar` for type `String`.
|
// TODO: Implement `AppendBar` for type `String`.
|
||||||
|
fn append_bar(self) -> Self {
|
||||||
|
self + "Bar"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -8,13 +8,19 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement trait `AppendBar` for a vector of strings.
|
// TODO: Implement trait `AppendBar` for a vector of strings.
|
||||||
|
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 {
|
||||||
|
|||||||
@ -8,10 +8,12 @@
|
|||||||
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub trait Licensed {
|
pub trait Licensed {
|
||||||
fn licensing_info(&self) -> String;
|
fn licensing_info(&self) -> String
|
||||||
|
{
|
||||||
|
String::from("Some information")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SomeSoftware {
|
struct SomeSoftware {
|
||||||
@ -22,7 +24,7 @@ struct OtherSoftware {
|
|||||||
version_number: String,
|
version_number: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Licensed for SomeSoftware {} // Don't edit this line
|
impl Licensed for SomeSoftware {}// Don't edit this line
|
||||||
impl Licensed for OtherSoftware {} // Don't edit this line
|
impl Licensed for OtherSoftware {} // Don't edit this line
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub trait Licensed {
|
pub trait Licensed {
|
||||||
fn licensing_info(&self) -> String {
|
fn licensing_info(&self) -> String {
|
||||||
@ -23,7 +22,7 @@ impl Licensed for SomeSoftware {}
|
|||||||
impl Licensed for OtherSoftware {}
|
impl Licensed for OtherSoftware {}
|
||||||
|
|
||||||
// YOU MAY ONLY CHANGE THE NEXT LINE
|
// YOU MAY ONLY CHANGE THE NEXT LINE
|
||||||
fn compare_license_types(software: ??, software_two: ??) -> bool {
|
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool {
|
||||||
software.licensing_info() == software_two.licensing_info()
|
software.licensing_info() == software_two.licensing_info()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub trait SomeTrait {
|
pub trait SomeTrait {
|
||||||
fn some_function(&self) -> bool {
|
fn some_function(&self) -> bool {
|
||||||
@ -30,7 +29,7 @@ impl SomeTrait for OtherStruct {}
|
|||||||
impl OtherTrait for OtherStruct {}
|
impl OtherTrait for OtherStruct {}
|
||||||
|
|
||||||
// YOU MAY ONLY CHANGE THE NEXT LINE
|
// YOU MAY ONLY CHANGE THE NEXT LINE
|
||||||
fn some_func(item: ??) -> bool {
|
fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
|
||||||
item.some_function() && item.other_function()
|
item.some_function() && item.other_function()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,9 +8,8 @@
|
|||||||
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn longest(x: &str, y: &str) -> &str {
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
if x.len() > y.len() {
|
if x.len() > y.len() {
|
||||||
x
|
x
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
if x.len() > y.len() {
|
if x.len() > y.len() {
|
||||||
@ -22,6 +21,7 @@ fn main() {
|
|||||||
{
|
{
|
||||||
let string2 = String::from("xyz");
|
let string2 = String::from("xyz");
|
||||||
result = longest(string1.as_str(), string2.as_str());
|
result = longest(string1.as_str(), string2.as_str());
|
||||||
|
println!("The longest string is '{}'", result);
|
||||||
}
|
}
|
||||||
println!("The longest string is '{}'", result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,10 @@
|
|||||||
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Book {
|
struct Book<'a> {
|
||||||
author: &str,
|
author: &'a str,
|
||||||
title: &str,
|
title: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -10,12 +10,11 @@
|
|||||||
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn you_can_assert() {
|
fn you_can_assert() {
|
||||||
assert!();
|
assert!(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,12 +6,11 @@
|
|||||||
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn you_can_assert_eq() {
|
fn you_can_assert_eq() {
|
||||||
assert_eq!();
|
assert_eq!(1,1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,8 +7,6 @@
|
|||||||
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn is_even(num: i32) -> bool {
|
pub fn is_even(num: i32) -> bool {
|
||||||
num % 2 == 0
|
num % 2 == 0
|
||||||
}
|
}
|
||||||
@ -19,11 +17,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn is_true_when_even() {
|
fn is_true_when_even() {
|
||||||
assert!();
|
assert!(is_even(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn is_false_when_odd() {
|
fn is_false_when_odd() {
|
||||||
assert!();
|
assert!(!is_even(5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
width: i32,
|
width: i32,
|
||||||
@ -30,17 +29,20 @@ mod tests {
|
|||||||
fn correct_width_and_height() {
|
fn correct_width_and_height() {
|
||||||
// This test should check if the rectangle is the size that we pass into its constructor
|
// This test should check if the rectangle is the size that we pass into its constructor
|
||||||
let rect = Rectangle::new(10, 20);
|
let rect = Rectangle::new(10, 20);
|
||||||
assert_eq!(???, 10); // check width
|
assert_eq!(rect.width, 10); // check width
|
||||||
assert_eq!(???, 20); // check height
|
assert_eq!(rect.height, 20); // check height
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn negative_width() {
|
fn negative_width() {
|
||||||
// This test should check if program panics when we try to create rectangle with negative width
|
// This test should check if program panics when we try to create rectangle with negative width
|
||||||
let _rect = Rectangle::new(-10, 10);
|
let _rect = Rectangle::new(-10, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
|
||||||
fn negative_height() {
|
fn negative_height() {
|
||||||
// This test should check if program panics when we try to create rectangle with negative height
|
// This test should check if program panics when we try to create rectangle with negative height
|
||||||
let _rect = Rectangle::new(10, -10);
|
let _rect = Rectangle::new(10, -10);
|
||||||
|
|||||||
@ -9,18 +9,18 @@
|
|||||||
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
|
||||||
|
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// Step 1.
|
// Step 1.
|
||||||
// Complete the `capitalize_first` function.
|
// Complete the `capitalize_first` function.
|
||||||
@ -15,7 +14,7 @@ pub fn capitalize_first(input: &str) -> String {
|
|||||||
let mut c = input.chars();
|
let mut c = input.chars();
|
||||||
match c.next() {
|
match c.next() {
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
Some(first) => ???,
|
Some(first) => first.to_uppercase().to_string() + c.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +23,13 @@ pub fn capitalize_first(input: &str) -> String {
|
|||||||
// Return a vector of strings.
|
// Return a vector of strings.
|
||||||
// ["hello", "world"] -> ["Hello", "World"]
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
vec![]
|
words.iter().map(|input| capitalize_first(*input)).collect()
|
||||||
|
// let mut x : Vec<String> = vec![];
|
||||||
|
// for word in words.iter()
|
||||||
|
// {
|
||||||
|
// x.push(capitalize_first(word));
|
||||||
|
// }
|
||||||
|
// x
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3.
|
// Step 3.
|
||||||
@ -32,7 +37,18 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
|||||||
// Return a single string.
|
// Return a single string.
|
||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
pub fn capitalize_words_string(words: &[&str]) -> String {
|
pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
words.iter().map(|input| capitalize_first(*input)).collect()
|
||||||
|
// for word in words.iter()
|
||||||
|
// {
|
||||||
|
// if word == &" "
|
||||||
|
// {
|
||||||
|
// x.push_str(word);
|
||||||
|
// } else {
|
||||||
|
// x.push_str(&capitalize_first(word))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
//x
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -9,7 +9,6 @@
|
|||||||
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum DivisionError {
|
pub enum DivisionError {
|
||||||
@ -26,23 +25,32 @@ pub struct NotDivisibleError {
|
|||||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||||
// Otherwise, return a suitable error.
|
// Otherwise, return a suitable error.
|
||||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||||
todo!();
|
if b == 0 {
|
||||||
|
Err(DivisionError::DivideByZero)
|
||||||
|
} else if a%b == 0 {
|
||||||
|
Ok(a / b)
|
||||||
|
} else {
|
||||||
|
Err(DivisionError::NotDivisible(NotDivisibleError{dividend: a, divisor: b}))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test
|
// Complete the function and return a value of the correct type so the test
|
||||||
// passes.
|
// passes.
|
||||||
// Desired output: Ok([1, 11, 1426, 3])
|
// Desired output: Ok([1, 11, 1426, 3])
|
||||||
fn result_with_list() -> () {
|
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
|
||||||
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));
|
||||||
|
division_results.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete the function and return a value of the correct type so the test
|
// Complete the function and return a value of the correct type so the test
|
||||||
// passes.
|
// passes.
|
||||||
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
||||||
fn list_of_results() -> () {
|
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
|
||||||
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));
|
||||||
|
division_results.filter(|x| x.is_ok()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn factorial(num: u64) -> u64 {
|
pub fn factorial(num: u64) -> u64 {
|
||||||
|
(1..num+1).into_iter().fold(1, |acc, x| x * acc)
|
||||||
// Complete this function to return the factorial of num
|
// Complete this function to return the factorial of num
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - return
|
// - return
|
||||||
|
|||||||
@ -16,15 +16,17 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub struct ReportCard {
|
|
||||||
pub grade: f32,
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
pub struct ReportCard<T : Display> {
|
||||||
|
pub grade: T,
|
||||||
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 {}",
|
format!("{} ({}) - achieved a grade of {}",
|
||||||
&self.student_name, &self.student_age, &self.grade)
|
&self.student_name, &self.student_age, &self.grade)
|
||||||
@ -52,7 +54,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,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user