Решение на упр.02 задача 3 от Мариян Момчилов
Към профила на Мариян Момчилов
Резултати
- 1 точка от тестове
- 0 бонус точки
- 1 точка общо
- 1 успешни тест(а)
- 0 неуспешни тест(а)
Код
fn split_string_at(s: String, n: usize) -> (String, String) {
let (slice_one, slice_two) = split_slice_at(&s, n);
(slice_one.to_string(), slice_two.to_string())
}
fn split_slice_at(s: &str, n: usize) -> (&str, &str) {
s.split_at(n)
}
fn split_slice_mut_at(s: &mut str, n: usize) -> (&mut str, &mut str) {
s.split_at_mut(n)
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.102
Compiling quote v1.0.41
Compiling unicode-ident v1.0.20
Compiling futures-core v0.3.31
Compiling futures-sink v0.3.31
Compiling futures-channel v0.3.31
Compiling syn v2.0.108
Compiling pin-project-lite v0.2.16
Compiling slab v0.4.11
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling pin-utils v0.1.0
Compiling memchr v2.7.6
Compiling solution v0.1.0 (/tmp/d20251023-1757769-chast2/solution)
warning: function `split_string_at` is never used
--> src/lib.rs:2:4
|
2 | fn split_string_at(s: String, n: usize) -> (String, String) {
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function `split_slice_at` is never used
--> src/lib.rs:7:4
|
7 | fn split_slice_at(s: &str, n: usize) -> (&str, &str) {
| ^^^^^^^^^^^^^^
warning: function `split_slice_mut_at` is never used
--> src/lib.rs:11:4
|
11 | fn split_slice_mut_at(s: &mut str, n: usize) -> (&mut str, &mut str) {
| ^^^^^^^^^^^^^^^^^^
warning: `solution` (lib) generated 3 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: function `split_slice_mut_at` is never used
--> tests/../src/lib.rs:11:4
|
11 | fn split_slice_mut_at(s: &mut str, n: usize) -> (&mut str, &mut str) {
| ^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `solution` (test "solution_test") generated 1 warning
Finished `test` profile [unoptimized + debuginfo] target(s) in 9.51s
Running tests/solution_test.rs (target/debug/deps/solution_test-fc1324dc0157cdc7)
running 1 test
test solution_test::test_basic ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
История (2 версии и 1 коментар)
Мариян качи решение на 18.10.2025 19:44 (преди 12 дена)
+
fn split_string_at(s: String, n: usize) -> (String, String) {
let (slice_one, slice_two) = split_slice_at(&s, n);
(slice_one.to_string(), slice_two.to_string())
}
fn split_slice_at(s: &str, n: usize) -> (&str, &str) {
s.split_at(n)
}
-fn split_slice_mut_at(s: &mut str, n: usize) -> (String, String) {
- let (slice_one, slice_two) = split_slice_at(s as &str, n);
- (slice_one.to_string(), slice_two.to_string())
+fn split_slice_mut_at(s: &mut str, n: usize) -> (&mut str, &mut str) {
+ s.split_at_mut(n)
}
Типовете на split_string_at и split_slice_at са такива защото според мен е по-смислено split_string/split_slice да връщат String/slice, въпреки че може и на двете места е slice.
split_slice_mut_at не може да се имплементира по-начин от вида:
fn split_slice_mut_at(s: &mut str, n: usize) -> (&mut str, &mut str) { let s1 = &mut s[0..n]; let s2 = &mut s[n..]; (s1, s2) }
Защото s се borrow-ва като mutable ref 2 пъти, което не е позволено
