Решение на упр.03 задача 2 от Илиян Гаврилов
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 0 неуспешни тест(а)
Код
enum VehicleKind {
Car,
Truck,
Motorcycle,
Bicycle,
}
impl VehicleKind {
fn fuel_consumption(&self) -> f64 {
match self {
VehicleKind::Car => 0.07,
VehicleKind::Truck => 0.15,
VehicleKind::Motorcycle => 0.05,
VehicleKind::Bicycle => 0.0
}
}
}
enum DriveError {
NotEnoughFuel { needed: f64, have: f64 }
}
struct Vehicle {
kind: VehicleKind,
fuel: f64,
distance: f64,
}
impl Vehicle {
fn new(kind: VehicleKind, fuel: f64) -> Self {
Self {kind, fuel, distance: 0.0 }
}
fn drive(&mut self, km: f64) -> Result<(), DriveError> {
let needed_fuel = self.kind.fuel_consumption() * km;
if self.fuel < needed_fuel {
return Err(DriveError::NotEnoughFuel { needed: needed_fuel, have: self.fuel });
}
self.fuel -= needed_fuel;
self.distance += km;
Ok(())
}
}
fn main() {
}
#[test]
fn test_basic() {
let mut vehicle = Vehicle::new(VehicleKind::Car, 10.0);
assert!(matches!(vehicle.drive(100.0), Ok(_)));
assert!(matches!(vehicle.drive(100.0), Err(DriveError::NotEnoughFuel{needed: _, have: _ })));
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.103
Compiling unicode-ident v1.0.22
Compiling quote v1.0.41
Compiling futures-core v0.3.31
Compiling futures-sink v0.3.31
Compiling futures-channel v0.3.31
Compiling memchr v2.7.6
Compiling syn v2.0.108
Compiling pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling futures-io v0.3.31
Compiling slab v0.4.11
Compiling futures-task v0.3.31
Compiling solution v0.1.0 (/tmp/d20251030-1757769-1jjnr8c/solution)
warning: enum `VehicleKind` is never used
--> src/lib.rs:1:6
|
1 | enum VehicleKind {
| ^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: method `fuel_consumption` is never used
--> src/lib.rs:9:6
|
8 | impl VehicleKind {
| ---------------- method in this implementation
9 | fn fuel_consumption(&self) -> f64 {
| ^^^^^^^^^^^^^^^^
warning: enum `DriveError` is never used
--> src/lib.rs:19:6
|
19 | enum DriveError {
| ^^^^^^^^^^
warning: struct `Vehicle` is never constructed
--> src/lib.rs:23:8
|
23 | struct Vehicle {
| ^^^^^^^
warning: associated items `new` and `drive` are never used
--> src/lib.rs:30:6
|
29 | impl Vehicle {
| ------------ associated items in this implementation
30 | fn new(kind: VehicleKind, fuel: f64) -> Self {
| ^^^
...
34 | fn drive(&mut self, km: f64) -> Result<(), DriveError> {
| ^^^^^
warning: function `main` is never used
--> src/lib.rs:48:4
|
48 | fn main() {
| ^^^^
warning: `solution` (lib) generated 6 warnings
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
error[E0428]: the name `test_basic` is defined multiple times
--> tests/../src/lib.rs:52:1
|
52 | fn test_basic() {
| ^^^^^^^^^^^^^^^ `test_basic` redefined here
|
::: tests/solution_test.rs:9:1
|
9 | fn test_basic() {
| --------------- previous definition of the value `test_basic` here
|
= note: `test_basic` must be defined only once in the value namespace of this module
For more information about this error, try `rustc --explain E0428`.
error: could not compile `solution` (test "solution_test") due to 1 previous error
История (2 версии и 0 коментара)
Илиян качи решение на 23.10.2025 19:20 (преди около 1 месеца)
enum VehicleKind {
Car,
Truck,
Motorcycle,
Bicycle,
}
impl VehicleKind {
fn fuel_consumption(&self) -> f64 {
match self {
VehicleKind::Car => 0.07,
VehicleKind::Truck => 0.15,
VehicleKind::Motorcycle => 0.05,
VehicleKind::Bicycle => 0.0
}
}
}
enum DriveError {
NotEnoughFuel { needed: f64, have: f64 }
}
struct Vehicle {
kind: VehicleKind,
fuel: f64,
distance: f64,
}
impl Vehicle {
fn new(kind: VehicleKind, fuel: f64) -> Self {
- return Self {kind, fuel, distance: 0.0 };
+ Self {kind, fuel, distance: 0.0 }
}
fn drive(&mut self, km: f64) -> Result<(), DriveError> {
let needed_fuel = self.kind.fuel_consumption() * km;
- if self.fuel - needed_fuel < 0.0 {
+ if self.fuel < needed_fuel {
return Err(DriveError::NotEnoughFuel { needed: needed_fuel, have: self.fuel });
}
self.fuel -= needed_fuel;
self.distance += km;
- return Ok(());
+ Ok(())
}
}
fn main() {
}
#[test]
fn test_basic() {
let mut vehicle = Vehicle::new(VehicleKind::Car, 10.0);
assert!(matches!(vehicle.drive(100.0), Ok(_)));
assert!(matches!(vehicle.drive(100.0), Err(DriveError::NotEnoughFuel{needed: _, have: _ })));
}
