Решение на упр.01 задача 1 от Борил Василев
Резултати
- 1 точка от тестове
- 0 бонус точки
- 1 точка общо
- 1 успешни тест(а)
- 0 неуспешни тест(а)
Код
fn calc(a: i32, b: i32, op: char) -> i32
{
return match op
{
'+' => a + b,
'-' => a - b,
'*' => a * b,
'/' if b != 0 => a / b,
'/' if b == 0 => 0,
_ => 42,
};
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn test_basic()
{
assert_eq!(calc(11, 23, '+'), 34);
assert_eq!(calc(11, 23, '-'), -12);
assert_eq!(calc(11, 23, '*'), 253);
assert_eq!(calc(253, 11, '/'), 23);
assert_eq!(calc(250, 11, '/'), 22);
assert_eq!(calc(121, 0, '/'), 0);
assert_eq!(calc(11, 23, '^'), 42);
assert_eq!(calc(11, 23, 'y'), 42);
}
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.101
Compiling unicode-ident v1.0.19
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.106
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/d20251016-574132-uvig08/solution)
warning: function `calc` is never used
--> src/lib.rs:1:4
|
1 | fn calc(a: i32, b: i32, op: char) -> i32
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (lib) generated 1 warning
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
Finished `test` profile [unoptimized + debuginfo] target(s) in 9.05s
Running tests/solution_test.rs (target/debug/deps/solution_test-80bf62783e3de781)
running 2 tests
test solution_test::test_basic ... ok
test solution_test::tests::test_basic ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
История (2 версии и 0 коментара)
Борил качи решение на 15.10.2025 11:56 (преди 15 дена)
fn calc(a: i32, b: i32, op: char) -> i32
{
- return match op // ne haresvam da ne pisha return
+ return match op
{
'+' => a + b,
'-' => a - b,
'*' => a * b,
'/' if b != 0 => a / b,
'/' if b == 0 => 0,
_ => 42,
};
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn test_basic()
{
assert_eq!(calc(11, 23, '+'), 34);
assert_eq!(calc(11, 23, '-'), -12);
assert_eq!(calc(11, 23, '*'), 253);
assert_eq!(calc(253, 11, '/'), 23);
assert_eq!(calc(250, 11, '/'), 22);
assert_eq!(calc(121, 0, '/'), 0);
assert_eq!(calc(11, 23, '^'), 42);
assert_eq!(calc(11, 23, 'y'), 42);
}
}
