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 hopes to raise $1bn with a convertible bond private placement

The super secure UAE-based Telegram messenger service, developed by Russian-born software icon Pavel Durov, is looking to raise $1bn through a bond placement to a limited number of investors from Russia, Europe, Asia and the Middle East, the Kommersant daily reported citing unnamed sources on February 18, 2021.The issue reportedly comprises exchange bonds that could be converted into equity in the messaging service that is currently 100% owned by Durov and his brother Nikolai.Kommersant reports that the price of the conversion would be at a 10% discount to a potential IPO should it happen within five years.The minimum bond placement is said to be set at $50mn, but could be lowered to $10mn. Five-year bonds could carry an annual coupon of 7-8%.

How Does Bitcoin Mining Work?

Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. “This is how new coins are created” and new transactions are added to the blockchain, says Okoro.

Rust from ye


Telegram Rust
FROM USA