"LISTEN UP, YOU MAGNIFICENT, STRESSED-OUT CODERS!" Santa bellowed. "Tomorrow, we test the sleigh again. And this time, it WILL work. No more Florida nonsense. Palm trees are NOT part of the Christmas aesthetic."
He pointed dramatically at Bernard and Pepper. “You two are coming with me. If the sleigh glitches mid-flight again, I want live debugging happening in real time. No excuses.”
The elves whispered nervously. Everyone still remembered the "Florida Incident"—last week’s failed test landing in a snowless golf course. Santa didn’t appreciate the HOA complaints.
“This time,” Santa continued, pacing, “Snowball compiled every scrap of data we need for each location. Top-notch metrics. But metrics mean nothing without an algorithm. Your job is to write a function to find the snowball-densest landing spot.”
Snowball has provided you with a Vec<Location>
, now the other elves need to write a function to find the most dense area with snow.
Here is what you need to to:
new()
associated function for the Location
struct that takes x: f64
, y: f64
, z: f64
, area: f64
, and snow
.snow
parameter must be able to accept all SnowKg
, SnowLb
and Snowball
types.Location
struct named density()
that gets the density of snow in the location.find_best_location
function which takes a Vec<Location>
and returns a Result<Location, Box<dyn Error>>
.That's it! 🎅
If you’re stuck or need a starting point, here are some hints to help you along the way!
When we implemented the From<T>
trait in the previous challenge, Rust automatically implemented the Into<T>
trait for us. This means that if you implement From<T>
for a type, you can convert that type to T
using the into()
method. e.g. let snow = snow.into();
.
For the snow
parameter, you can accept anything that implements the Into<Snowball>
trait. e.g. new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self
. Here impl Into<Snowball>
means any type that implements the Into<Snowball>
trait.
Convert the snow
parameter to a Snowball
type using the into()
method. e.g. let snow = snow.into();
.
Implement the density()
method for the Location
struct. The density of snow is calculated by dividing the snow weight by the area of the location. Make sure you handle the division by zero case.
Use into_iter
to convert the Vec<Location>
to a consuming iterator. e.g. locations.into_iter()
.
Use the max_by()
method to find the location with the highest density of snow. e.g.
locations
.into_iter()
.max_by(|a, b| {
a.density()
.partial_cmp(&b.density())
.unwrap_or(Ordering::Equal)
})
Import Ordering::Equal
using use std::cmp::Ordering
.
Use the ok_or()
method to convert the Option
returned by max_by()
to a Result
. e.g. ok_or("No locations found".into())
.
use std::{error::Error, ops::Deref};use std::cmp::max;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64, y:f64, z:f64, area:f64, snow: impl Into<Snowball>)->Self{ Location{ x, y, z, area, snow:snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0{ return 0.0; }else{ let density = self.snow.0 as f64 / self.area; return density.abs(); } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut max_density_loc = Location::new(0.0,0.0,0.0,0.0, Snowball::new(0)); let mut found_max = false; for loc in locations{ if loc.density() >= max_density_loc.density(){ found_max=true; max_density_loc = loc; } } if found_max{ Ok(max_density_loc) }else{ Err("Nothing found".to_string().into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. let snow = self.snow.abs() as f64; if self.area == 0.0 { return 0.0 } else { return snow / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let mut highest_loc = Location::new(0.0, 0.0, 0.0, 0.0, Snowball::new(0)); let mut found = false; for location in locations { if location.density() >= highest_loc.density() { found = true; highest_loc = location; } } if found == true { Ok(highest_loc) } else { Err("Not found".to_string().into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("location not found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("No location found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snowballs: Snowball = snow.into(); Location { x: x, y: y, z: z, area: area, snow: snowballs, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } let float_snow = *(self.snow) as f64; float_snow / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. if locations.len() == 0 { return Err("No locations in argument vector.")?; } let dense_loc = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); dense_loc.ok_or("No locations found".into()) // return Ok(dense_loc.expect("Iterator should not fail to find max."));}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball> { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, _ => *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("Array empty".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::cmp::Ordering;use std::collections::HashMap;use std::hash::Hash;use std::iter::Map;use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. if locations.is_empty() { return Err(Box::from("no locations provided")); } let landing_location = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); landing_location.ok_or_else(|| Box::from("no valid locations found"))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}fn main() {}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } return self.snow.to_kg() / self.area ; }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let loc = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); if loc.is_none() { return Err(Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, "No locations"))); } Ok(loc.expect("bad location"))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) } pub fn to_kg(&self) -> f64 { self.0 as f64 * SNOWBALL_WEIGHT_KG }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. match self.area { 0.0 => 0.0, a => (*self.snow as f64) / a, } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { match self.area { 0.0 => 0.0, a => (*self.snow as f64) / a, } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location { x, y, z, area, snow, } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new( x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball> ) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } (self.snow.0 as f64) / self.area }}pub fn find_best_location( locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a,b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new (x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location{ x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } else { *self.snow as f64/ self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } self.snow.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let result = locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(Ordering::Equal) }) .ok_or("no locations found".into()); result}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { (*self.snow as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let result = locations .into_iter() .max_by(|x, y| { x.density() .partial_cmp(&y.density()) .unwrap_or(Ordering::Equal) }) .ok_or("no locations found".into()); result}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { let snow: Snowball = snow.into(); Self { snow, x, y, z, area: if area.is_nan() { 0.0 } else { area }, } } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0; } let snow: f64 = self.snow.0 as f64; if snow == 0.0 { return 0.0; } snow / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a , b| { println!("{:?} {:?}", a, b); a.density().total_cmp( &b.density() ) }) .ok_or("Not found".into()) // .ok_or(Box::new(Err("Not found".into()))) // let mut best_location = locations. // for location in locations { // if location.density() < best_location.density() { // best_location = &location;j // } // } // // Ok(best_location.clone())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64,y: f64,z:f64,area: f64,snow: impl Into<Snowball>)-> Self{ Self{ x,y,z,area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0{ return 0.0; } *self.snow as f64/self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter() .max_by(|a, b| { let density_a = a.density(); let density_b = b.density(); // Handle NaN cases - treat NaN as less than any other value if density_a == 0.0 { return std::cmp::Ordering::Less; } if density_b == 0.0 { return std::cmp::Ordering::Less; } density_a.partial_cmp(&density_b) .unwrap_or(std::cmp::Ordering::Less) }) .ok_or_else(|| "No locations provided".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x:f64, y:f64, z:f64, area:f64, snow:impl Into<Snowball>) -> Self{ Location{x:x, y:y, z:z, area: area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 {return 0.0;} *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. if locations.is_empty(){return Err("Empty Array".into());} let mut best_location:Location = Location::new(0.0,0.0,0.0,0.0,Snowball(0)); let mut best_location_density:f64 = 0.0; for location in locations{ if location.density() >= best_location_density{ best_location = location; best_location_density = best_location.density(); } } return match best_location_density{ f64::MIN..=-0.1 => Err("No Best Location".into()), _ => Ok(best_location) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; }; self.snow.0 as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let highest_density_location = locations .into_iter() .reduce(|a, b| if a.density() > b.density() { a } else { b }) .ok_or("No locations were provided")?; Ok(highest_density_location)}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self {x:x, y:y, z:z, area:area, snow:snow.into()} } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a,b| a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal)).ok_or("empty list".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self{ let snow = snow.into(); Self {x, y, z, area, snow} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *(self.snow) as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. match locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) { Some(l) => Ok(l), None => Err("No location found".into()), }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball> ) -> Self{ Self{ x : x, y : y, z : z, area : area, snow : snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { (*self.snow as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal)}) .ok_or("location not found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new<T: Into<Snowball>> (x: f64, y: f64, z: f64, area: f64, snow: T) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { (*self.snow as f64) / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("location not found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } else { return *self.snow as f64 / self.area; } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Location { Location { x: x, y: y, z: z, area: area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { return Self { x, y, z, area, snow: snow.into() }; } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0; } return *self.snow as f64 / self.area; }}use std::cmp::Ordering;pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { let result = locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); return result.ok_or("No locations found".into());}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball>, { Location { x, y, z, area, snow: snow.into(), } } // 2. Implement the `density()` method. pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}// 3. Implement the `find_best_location()` function.pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { let da = a.density(); let db = b.density(); da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal) }) .ok_or_else(|| "No locations provided".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. pub fn new<T>(x: f64, y: f64, z: f64, area: f64, snow: T) -> Self where T: Into<Snowball>, { Location { x, y, z, area, snow: snow.into(), } } // 2. Implement the `density()` method. pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}// 3. Implement the `find_best_location()` function.pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { let da = a.density(); let db = b.density(); da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal) }) .ok_or_else(|| "No locations provided".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new<S: Into<Snowball>>(x: f64, y: f64, z: f64, area: f64, snow: S) -> Location { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area <= 0.0 { 0.0 } else { let mass: f64 = *(self.snow) as f64; mass / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { if let Some(densest) = locations.iter().max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal)) { Ok(densest.clone()) } else { let e: Box<dyn Error> = "No densest found".into(); Err(e) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No location found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering as O};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new (x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Self { x, y, z, area, snow } } pub fn density (&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location (locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.iter().max_by(|a, b| if a.density() > b.density() { O::Greater } else { O::Less }).cloned().ok_or_else(|| "Can't find location with highest density of snow".to_owned().into()) // 3. Find the location with the highest snow density.}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } let test = *self.snow; let density = (test as f64) / self.area; density }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let x = locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(std::cmp::Ordering::Equal) }); x.ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { let snowball: Snowball = snow.into(); Location { x, y, z, area, snow: snowball } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area <= 0.0 || self.area.is_nan() { return 0.0 } else{ *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { return locations .iter() .cloned() .max_by(|a, b| a.density().total_cmp(&b.density())) .ok_or("No good snow around!".into());}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` return Self { x, y, z, area, snow: snow.into(), }; } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 || self.area.is_nan() { return 0.0; } else { return *self.snow as f64 / self.area; } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. return locations .iter() .cloned() .max_by(|a, b| a.density().total_cmp(&b.density())) .ok_or("No good snow around!".into());}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}pub enum Snow { SnowKg(SnowKg), SnowLb(SnowLb), Snowball(Snowball),}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 || self.area.is_nan() { println!("zero: {}", self.area); return 0.0; } else { let s = *self.snow as f64; println!("snow: {}", s); println!("area: {}", self.area); let d = s / self.area; println!("density: {}", d); return d; } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. // for loc in locations { // let d = loc.density(); // } let best = locations .iter() .cloned() .max_by(|a, b| a.density().total_cmp(&b.density())) .ok_or("No good snow around!")?; Ok(best)}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter() .max_by(|l1, l2| f64::partial_cmp(&l1.density(), &l2.density()).unwrap_or(Ordering::Equal)) .ok_or(Box::<dyn Error>::from("Empty vec of locations."))}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new<T: Into<Snowball>>(x: f64, y: f64, z:f64, area: f64, snow: T) -> Self { Location { x: x, y: y, z: z, area: area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area > 0.0 { (*self.snow) as f64 / self.area } else { 0.0 } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)) .map(|loc| loc.clone()) .ok_or_else(|| "No locations available".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("".into()) }const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z:f64, area: f64, snow: impl Into<Snowball>) -> Location { Location { x : x, y : y, z: z, area : area, snow : snow.into() } } pub fn density(&self) -> f64 { if self.area == 0.0 { 0.0 } else { *self.snow as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal)}).ok_or("No locations found".into()) // 3. Find the location with the highest snow density.}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { let snow = snow.into(); Location{x, y, z, area, snow} } // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0; } (*self.snow as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into()} } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area.is_normal() { return *self.snow as f64 / self.area } 0.0 }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations.into_iter(). max_by(|a, b| { a.density(). partial_cmp(&b.density()). unwrap_or(Ordering::Equal) }). ok_or("error".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into().into(), } } pub fn density(&self) -> f64 { let div = *self.snow as f64 / self.area; if self.area == 0.0 { return 0.0; } else { return div; } // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { match locations.into_iter().max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) { Some(x) => Ok(x), None => Err("error".into()), } // 3. Find the location with the highest snow density.}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new( x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>, ) -> Location { let snow = snow.into(); Location{ x, y, z, area, snow } } pub fn density(&self) -> f64 { if self.area > 0.0 { return (*self.snow as f64) / self.area; } 0.0 }}impl PartialEq for Location { fn eq(&self, other: &Self) -> bool { self.density() == other.density() }}impl Eq for Location {}impl PartialOrd for Location { fn partial_cmp(&self, other: &Location) -> Option<Ordering> { Some(self.cmp(other)) }}impl Ord for Location { fn cmp(&self, other: &Self) -> Ordering { if self == other { return Ordering::Equal; } let my_density = self.density(); let other_density = other.density(); if my_density < other_density{ return Ordering::Less; } Ordering::Greater }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { match locations.iter().max() { Some(location) => { Ok(location.clone()) } None => { Err(Box::new(std::fmt::Error)) } }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x,y,z,area,snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area == 0.0 { return 0.0; } if self.snow.0 == 0 { return 0.0; } //assert!(self.snow.0 <= (1 << 53), "{} {}", self.snow.0, self.area); let snow = self.snow.0 as f64; return snow / self.area // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. if locations.len() == 0 { return Err("lol".to_string().into()); } Ok(locations.into_iter().filter(|v| !v.density().is_nan()).max_by(|a,b| a.density().partial_cmp(&b.density()).unwrap_or(std::cmp::Ordering::Equal)).unwrap())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area:f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow:snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { self.snow.0 as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let max = locations.iter().max_by(|&a, &b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }); let max = max.ok_or("")?; Ok(max.clone())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Location { Location{ x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } ((*self.snow) as f64) / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { let mut it = locations.iter().filter(|x| !x.density().is_nan()).peekable(); let mut it2 = it.clone().cloned(); // 3. Find the location with the highest snow density. Ok(if it.peek().is_none() { if locations.len() > 0 { locations[0].clone() } else { return Err(Box::from("locations empty: no best location")) } } else if let Some(v) = it.max_by(|a,b| a.density().partial_cmp(&b.density()).unwrap()).cloned() { v } else { it2.nth(0).unwrap() })}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area:f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow:snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { 0.0 } else { self.snow.0 as f64 / self.area } }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. let max = locations.iter().max_by(|&a, &b| { a.density().partial_cmp(&b.density()).unwrap_or(Ordering::Equal) }); let max = max.ok_or("")?; Ok(max.clone())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref};use std::cmp::Ordering;#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Location { x, y, z, area, snow: snow.into() } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return self.area; } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}
use std::{error::Error, ops::Deref, cmp::Ordering};#[derive(Debug, Clone)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { // 1. Implement the `new()` method. // Parameters (must be in order): // - x: f64 // - y: f64 // - z: f64 // - area: f64 // - snow: Either `SnowKg`, `SnowLb` or `Snowball` pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { // 2. Implement the `density()` method. // Calculation: snow / area // all area is in one unit, so don't worry about the unit conversion. // Return 0.0 if the area is 0.0. if self.area == 0.0 { return 0.0 } *self.snow as f64 / self.area }}pub fn find_best_location(locations: Vec<Location>) -> Result<Location, Box<dyn Error>> { // 3. Find the location with the highest snow density. locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}