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 announces Anonymous Admins

The cloud-based messaging platform is also adding Anonymous Group Admins feature. As per Telegram, this feature is being introduced for safer protests. As per the Telegram blog post, users can “Toggle Remain Anonymous in Admin rights to enable Batman mode. The anonymized admin will be hidden in the list of group members, and their messages in the chat will be signed with the group name, similar to channel posts.”

That strategy is the acquisition of a value-priced company by a growth company. Using the growth company's higher-priced stock for the acquisition can produce outsized revenue and earnings growth. Even better is the use of cash, particularly in a growth period when financial aggressiveness is accepted and even positively viewed.he key public rationale behind this strategy is synergy - the 1+1=3 view. In many cases, synergy does occur and is valuable. However, in other cases, particularly as the strategy gains popularity, it doesn't. Joining two different organizations, workforces and cultures is a challenge. Simply putting two separate organizations together necessarily creates disruptions and conflicts that can undermine both operations.

Rust from sa


Telegram Rust
FROM USA