упр.02 задача 2
- Краен срок:
- 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() {
let mut s = String::from("Здравей");
print_str(&s);
append_exclamation(&mut s);
print_str(&s);
assert_eq!(s, "Здравей!");
}
fn print_str(s: &str) {
println!("Стрингово съдържание: {}", s);
}
fn append_exclamation(s: &mut String) {
// добави '!' в края на низа
}
fn main() {
let mut s = String::from("Здравей");
print_str(&s);
append_exclamation(&mut s);
print_str(&s);
}
- a) Попълни тялото на
append_exclamation. - b) Обясни защо използваме
&str, а не&Stringвъвfn print_str(s: &str) - c) Обясни защо използваме
&mut String, а не&mut strвъвfn append_exclamation(s: &mut String) - d) Какво би станало, ако в
mainнаправишlet r1 = &s; let r2 = &mut 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() {
let mut s = String::from("Здравей");
print_str(&s);
append_exclamation(&mut s);
print_str(&s);
assert_eq!(s, "Здравей!");
}
