Telegram Group & Telegram Channel
🧠 Задача на Rust — для продвинутых разработчиков


use std::cell::RefCell;
use std::rc::Rc;

fn main() {
let a = Rc::new(RefCell::new(1));
let b = a.clone();

let c = {
let mut val = b.borrow_mut();
*val += 1;
Rc::try_unwrap(b).ok().unwrap().into_inner()
};

println!("a: {}", a.borrow());
println!("c: {}", c);
}


Что выведет код?

A.
a: 2
c: 2

B.
a: 1
c: 2

C.
Panic at runtime due to unwrap failure

D.
Compilation error due to ownership rules

📌
Ответ:
Правильный ответ: C — Panic at runtime due to unwrap failure

Почему:
Мы создаем a как Rc<RefCell<i32>>, из него делаем b = a.clone() → теперь у Rc два владельца.

Мы мутируем значение через b.borrow_mut(), увеличиваем его на 1.

Затем пытаемся сделать Rc::try_unwrap(b).

⚠️ Rc::try_unwrap требует, чтобы Rc был единственным владельцем. Но у нас всё ещё есть a, то есть ссылка остаётся → unwrap не срабатывает и unwrap() вызывает паник на runtime.


@rust_code



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

🧠 Задача на Rust — для продвинутых разработчиков


use std::cell::RefCell;
use std::rc::Rc;

fn main() {
let a = Rc::new(RefCell::new(1));
let b = a.clone();

let c = {
let mut val = b.borrow_mut();
*val += 1;
Rc::try_unwrap(b).ok().unwrap().into_inner()
};

println!("a: {}", a.borrow());
println!("c: {}", c);
}


Что выведет код?

A.
a: 2
c: 2

B.
a: 1
c: 2

C.
Panic at runtime due to unwrap failure

D.
Compilation error due to ownership rules

📌
Ответ:
Правильный ответ: C — Panic at runtime due to unwrap failure

Почему:
Мы создаем a как Rc<RefCell<i32>>, из него делаем b = a.clone() → теперь у Rc два владельца.

Мы мутируем значение через b.borrow_mut(), увеличиваем его на 1.

Затем пытаемся сделать Rc::try_unwrap(b).

⚠️ Rc::try_unwrap требует, чтобы Rc был единственным владельцем. Но у нас всё ещё есть a, то есть ссылка остаётся → unwrap не срабатывает и unwrap() вызывает паник на runtime.


@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/929

View MORE
Open in Telegram


Rust Telegram | DID YOU KNOW?

Date: |

Telegram has exploded as a hub for cybercriminals looking to buy, sell and share stolen data and hacking tools, new research shows, as the messaging app emerges as an alternative to the dark web.An investigation by cyber intelligence group Cyberint, together with the Financial Times, found a ballooning network of hackers sharing data leaks on the popular messaging platform, sometimes in channels with tens of thousands of subscribers, lured by its ease of use and light-touch moderation.

Traders also expressed uncertainty about the situation with China Evergrande, as the indebted property company has not provided clarification about a key interest payment.In economic news, the Commerce Department reported an unexpected increase in U.S. new home sales in August.Crude oil prices climbed Friday and front-month WTI oil futures contracts saw gains for a fifth straight week amid tighter supplies. West Texas Intermediate Crude oil futures for November rose $0.68 or 0.9 percent at 73.98 a barrel. WTI Crude futures gained 2.8 percent for the week.

Rust from id


Telegram Rust
FROM USA