Telegram Group & Telegram Channel
🎯 Задача на владение (ownership) и ссылки в Rust

Напиши функцию longest_word<'a>(a: &'a str, b: &'a str) -> &'a str,
которая возвращает слово с наибольшей длиной из двух строк.

Но есть ограничение: ты не можешь копировать строки — только возвращать ссылку.

📌 Пример:


fn main() {
let s1 = String::from("pirate");
let s2 = String::from("gold");

let result = longest_word(&s1, &s2);
println!("Longest: {}", result); // 👉 "pirate"
}


🔍 Подсказка:

- Тебе нужно указать время жизни `'a` для всех аргументов и возвращаемого значения.
- Если ты не укажешь
'a, компилятор не поймёт, какая ссылка может жить дольше.
- Возвращать
String нельзя — только &str (ссылка на строку).

🚫 Нельзя:

return a.to_string(); // копирование строки


Можно:

return a; // ссылка

@rust_code



tg-me.com/rust_code/967
Create:
Last Update:

🎯 Задача на владение (ownership) и ссылки в Rust

Напиши функцию longest_word<'a>(a: &'a str, b: &'a str) -> &'a str,
которая возвращает слово с наибольшей длиной из двух строк.

Но есть ограничение: ты не можешь копировать строки — только возвращать ссылку.

📌 Пример:


fn main() {
let s1 = String::from("pirate");
let s2 = String::from("gold");

let result = longest_word(&s1, &s2);
println!("Longest: {}", result); // 👉 "pirate"
}


🔍 Подсказка:

- Тебе нужно указать время жизни `'a` для всех аргументов и возвращаемого значения.
- Если ты не укажешь
'a, компилятор не поймёт, какая ссылка может жить дольше.
- Возвращать
String нельзя — только &str (ссылка на строку).

🚫 Нельзя:

return a.to_string(); // копирование строки


Можно:

return a; // ссылка

@rust_code

BY Rust


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/rust_code/967

View MORE
Open in Telegram


Rust Telegram | DID YOU KNOW?

Date: |

For some time, Mr. Durov and a few dozen staffers had no fixed headquarters, but rather traveled the world, setting up shop in one city after another, he told the Journal in 2016. The company now has its operational base in Dubai, though it says it doesn’t keep servers there.Mr. Durov maintains a yearslong friendship from his VK days with actor and tech investor Jared Leto, with whom he shares an ascetic lifestyle that eschews meat and alcohol.

Mr. Durov launched Telegram in late 2013 with his brother, Nikolai, just months before he was pushed out of VK, the Russian social-media platform he founded. Mr. Durov pitched his new app—funded with the proceeds from the VK sale—less as a business than as a way for people to send messages while avoiding government surveillance and censorship.

Rust from sa


Telegram Rust
FROM USA