упр.02 задача 1

Предадени решения

Краен срок:
22.10.2025 23:59
Точки:
1

Срокът за предаване на решения е отминал

// Include the solution source in the same file, so we
// don't have to worry about item visibility.
// Please don't use `include!` in real code, this is a hack
// around the checking system.
include!{ "../src/lib.rs" }
#[test]
fn test_basic() {
assert_eq!(duplicate_i32(10), (10, 10));
assert_eq!(duplicate_string("Здравей".to_string()), ("Здравей".to_string(), "Здравей".to_string()));
}
fn duplicate_i32(x: i32) -> (i32, i32) {
    // реализирай тази функция
}

fn duplicate_string(s: String) -> (String, String) {
    // реализирай тази функция
}

fn main() {
    let a = 10;
    let (a1, a2) = duplicate_i32(a);
    println!("a1 = {}, a2 = {}, original a = {}", a1, a2, a);

    let s = String::from("Здравей");
    let (s1, s2) = duplicate_string(s);
    println!("s1 = {}, s2 = {}", s1, s2);
}
  • a) Имплементирай duplicate_i32 и duplicate_string.
  • b) Обясни защо в случая с i32 е възможно да използваме x след извикване, но не можем да използваме s след duplicate_string.
  • c) Какво става, ако duplicate_string върне (s, s) — защо компилаторът го забранява?

П.С. игнорирайте линка с указания за домашни - добавя се автоматично и не е актуален в случая.

Задължително прочетете (или си припомнете): Указания за предаване на домашни

Погрижете се решението ви да се компилира с базовия тест:

// Include the solution source in the same file, so we
// don't have to worry about item visibility.
// Please don't use `include!` in real code, this is a hack
// around the checking system.
include!{ "../src/lib.rs" }
#[test]
fn test_basic() {
assert_eq!(duplicate_i32(10), (10, 10));
assert_eq!(duplicate_string("Здравей".to_string()), ("Здравей".to_string(), "Здравей".to_string()));
}