Решение на упр.03 задача 2 от Георги Стоянов
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 1 неуспешни тест(а)
Код
enum VehicleKind {
Car,
Truck,
Motorcycle,
Bicycle,
}
struct Vehicle {
kind: VehicleKind,
fuel: f64,
distance: f64,
}
enum DriveError {
NotEnoughFuel { needed: f64, have: f64 },
}
impl Vehicle {
fn new(kind: VehicleKind, fuel: f64) -> Self {
Self {
kind,
fuel,
distance: 0.0,
}
}
fn drive(&mut self, km: f64) -> Result<f64, DriveError> {
if self.fuel < km {
Result::Err(DriveError::NotEnoughFuel {
needed: km,
have: self.fuel,
})
} else {
let price = km * VehicleKind::price_per_km(&self.kind);
self.fuel -= km;
self.distance += km;
Result::Ok(price)
}
}
}
impl VehicleKind {
fn price_per_km(&self) -> f64 {
match self {
VehicleKind::Car => 0.07,
VehicleKind::Bicycle => 0.0,
VehicleKind::Motorcycle => 0.05,
VehicleKind::Truck => 0.15,
}
}
}
fn main() {
}
Лог от изпълнението
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-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling futures-task 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 slab v0.4.11
Compiling futures-io v0.3.31
Compiling solution v0.1.0 (/tmp/d20251030-1757769-xcaqge/solution)
warning: enum `VehicleKind` is never used
--> src/lib.rs:1:6
|
1 | enum VehicleKind {
| ^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: struct `Vehicle` is never constructed
--> src/lib.rs:8:8
|
8 | struct Vehicle {
| ^^^^^^^
warning: enum `DriveError` is never used
--> src/lib.rs:14:6
|
14 | enum DriveError {
| ^^^^^^^^^^
warning: associated items `new` and `drive` are never used
--> src/lib.rs:19:8
|
18 | impl Vehicle {
| ------------ associated items in this implementation
19 | fn new(kind: VehicleKind, fuel: f64) -> Self {
| ^^^
...
26 | fn drive(&mut self, km: f64) -> Result<f64, DriveError> {
| ^^^^^
warning: method `price_per_km` is never used
--> src/lib.rs:42:8
|
41 | impl VehicleKind {
| ---------------- method in this implementation
42 | fn price_per_km(&self) -> f64 {
| ^^^^^^^^^^^^
warning: function `main` is never used
--> src/lib.rs:52:4
|
52 | 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
warning: variants `Truck`, `Motorcycle`, and `Bicycle` are never constructed
--> tests/../src/lib.rs:3:5
|
1 | enum VehicleKind {
| ----------- variants in this enum
2 | Car,
3 | Truck,
| ^^^^^
4 | Motorcycle,
| ^^^^^^^^^^
5 | Bicycle,
| ^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: fields `needed` and `have` are never read
--> tests/../src/lib.rs:15:21
|
15 | NotEnoughFuel { needed: f64, have: f64 },
| ------------- ^^^^^^ ^^^^
| |
| fields in this variant
warning: function `main` is never used
--> tests/../src/lib.rs:52:4
|
52 | fn main() {
| ^^^^
warning: `solution` (test "solution_test") generated 3 warnings
Finished `test` profile [unoptimized + debuginfo] target(s) in 9.24s
Running tests/solution_test.rs (target/debug/deps/solution_test-bfd50394249726db)
running 1 test
test solution_test::test_basic ... FAILED
failures:
---- solution_test::test_basic stdout ----
thread 'solution_test::test_basic' panicked at tests/solution_test.rs:12:5:
assertion failed: matches!(vehicle.drive(100.0), Ok(_))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
solution_test::test_basic
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--test solution_test`
История (3 версии и 0 коментара)
Георги качи решение на 24.10.2025 11:38 (преди 7 дена)
enum VehicleKind {
Car,
Truck,
Motorcycle,
Bicycle,
}
struct Vehicle {
kind: VehicleKind,
fuel: f64,
distance: f64,
}
enum DriveError {
NotEnoughFuel { needed: f64, have: f64 },
}
impl Vehicle {
fn new(kind: VehicleKind, fuel: f64) -> Self {
Self {
kind,
fuel,
distance: 0.0,
}
}
- fn drive(&mut self, km: f64) -> Result<(), DriveError> {
+ fn drive(&mut self, km: f64) -> Result<f64, DriveError> {
if self.fuel < km {
- println!("Sadge");
Result::Err(DriveError::NotEnoughFuel {
needed: km,
have: self.fuel,
})
} else {
- println!("Great success!");
let price = km * VehicleKind::price_per_km(&self.kind);
self.fuel -= km;
self.distance += km;
- Result::Ok(())
+ Result::Ok(price)
}
}
}
impl VehicleKind {
fn price_per_km(&self) -> f64 {
match self {
VehicleKind::Car => 0.07,
VehicleKind::Bicycle => 0.0,
VehicleKind::Motorcycle => 0.05,
VehicleKind::Truck => 0.15,
}
}
}
fn test_basic() {
let mut vehicle = Vehicle::new(VehicleKind::Car, 100.0);
assert!(matches!(vehicle.drive(100.0), Ok(_)));
assert!(matches!(
vehicle.drive(100.0),
Err(DriveError::NotEnoughFuel { needed: _, have: _ })
));
}
fn main() {
test_basic();
}
Георги качи решение на 28.10.2025 16:25 (преди 2 дена)
enum VehicleKind {
Car,
Truck,
Motorcycle,
Bicycle,
}
struct Vehicle {
kind: VehicleKind,
fuel: f64,
distance: f64,
}
enum DriveError {
NotEnoughFuel { needed: f64, have: f64 },
}
impl Vehicle {
fn new(kind: VehicleKind, fuel: f64) -> Self {
Self {
kind,
fuel,
distance: 0.0,
}
}
fn drive(&mut self, km: f64) -> Result<f64, DriveError> {
if self.fuel < km {
Result::Err(DriveError::NotEnoughFuel {
needed: km,
have: self.fuel,
})
} else {
let price = km * VehicleKind::price_per_km(&self.kind);
self.fuel -= km;
self.distance += km;
Result::Ok(price)
}
}
}
impl VehicleKind {
fn price_per_km(&self) -> f64 {
match self {
VehicleKind::Car => 0.07,
VehicleKind::Bicycle => 0.0,
VehicleKind::Motorcycle => 0.05,
VehicleKind::Truck => 0.15,
}
}
}
-fn test_basic() {
- let mut vehicle = Vehicle::new(VehicleKind::Car, 100.0);
-
- assert!(matches!(vehicle.drive(100.0), Ok(_)));
- assert!(matches!(
- vehicle.drive(100.0),
- Err(DriveError::NotEnoughFuel { needed: _, have: _ })
- ));
-}
-
fn main() {
- test_basic();
}
