Telegram Group & Telegram Channel
👣 Вопрос:
Какие строки и в каком порядке будут выведены на консоль при запуске этой программы? Приведите полный список выводимых сообщений.этого кода.


use std::mem;

struct Tracer(&'static str);

impl Drop for Tracer {
fn drop(&mut self) {
println!("Dropping {}", self.0);
}
}

struct Container {
tracer: Tracer,
}

fn make_tracer(name: &'static str) -> Tracer {
println!("Creating {}", name);
Tracer(name)
}

fn main() {
let a = make_tracer("a");
let b = make_tracer("b");
{
let temp = make_tracer("temp");
println!("Inside block");
// temp перемещается в c
let c = temp;
println!("Exiting block");
} // <- здесь c (то есть «temp») умирает

let mut container = Container {
tracer: make_tracer("container1"),
};
// замена поля: старый контейнер1 будет сброшен
container.tracer = make_tracer("container2");

println!("Before dropping b explicitly");
mem::drop(b);
println!("End of main");
} // <- здесь умирают: container.tracer ("container2"), затем a


🔜 Ответ
Вот что выведется на консоль, построчно:
```bash
Creating a
Creating b
Creating temp
Inside block
Exiting block
Dropping temp
Creating container1
Creating container2
Dropping container1
Before dropping b explicitly
Dropping b
End of main
Dropping container2
Dropping a```

Пояснение по шагам:

let a = make_tracer("a");
Сначала вызывается make_tracer("a"), который печатает
Creating a
и возвращает Tracer("a").

let b = make_tracer("b");
Аналогично:
Creating b

Блок { … }:

let temp = make_tracer("temp"); → Creating temp

println!("Inside block"); → Inside block

let c = temp; — просто перемещение, без нового вывода.

println!("Exiting block"); → Exiting block

В конце блока выходит из области видимости c (он же temp), срабатывает Drop → Dropping temp

Инициализация container:

rust
let mut container = Container {
tracer: make_tracer("container1"),
};
→ Creating container1

Перезапись поля tracer:

```rust
container.tracer = make_tracer("container2");
Сначала вычисляется правая часть → Creating container2.
Затем старый container.tracer (то есть container1) сбрасывается → Dropping container1.```

Явное удаление b:

```rust
println!("Before dropping b explicitly");
mem::drop(b);
→ Before dropping b explicitly
Затем drop(b) вызывает Drop для b → Dropping b```

Выход из main:

```rust
println!("End of main");
→ End of main```

После этого по правилам Rust объекты уничтожаются в порядке обратном созданию (LIFO):

Сначала поле container.tracer (уже “container2”) → Dropping container2

Затем переменная a → Dropping a

Таким образом и получается приведённая последовательность.
Please open Telegram to view this post
VIEW IN TELEGRAM



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

👣 Вопрос:
Какие строки и в каком порядке будут выведены на консоль при запуске этой программы? Приведите полный список выводимых сообщений.этого кода.


use std::mem;

struct Tracer(&'static str);

impl Drop for Tracer {
fn drop(&mut self) {
println!("Dropping {}", self.0);
}
}

struct Container {
tracer: Tracer,
}

fn make_tracer(name: &'static str) -> Tracer {
println!("Creating {}", name);
Tracer(name)
}

fn main() {
let a = make_tracer("a");
let b = make_tracer("b");
{
let temp = make_tracer("temp");
println!("Inside block");
// temp перемещается в c
let c = temp;
println!("Exiting block");
} // <- здесь c (то есть «temp») умирает

let mut container = Container {
tracer: make_tracer("container1"),
};
// замена поля: старый контейнер1 будет сброшен
container.tracer = make_tracer("container2");

println!("Before dropping b explicitly");
mem::drop(b);
println!("End of main");
} // <- здесь умирают: container.tracer ("container2"), затем a


🔜 Ответ
Вот что выведется на консоль, построчно:
```bash
Creating a
Creating b
Creating temp
Inside block
Exiting block
Dropping temp
Creating container1
Creating container2
Dropping container1
Before dropping b explicitly
Dropping b
End of main
Dropping container2
Dropping a```

Пояснение по шагам:

let a = make_tracer("a");
Сначала вызывается make_tracer("a"), который печатает
Creating a
и возвращает Tracer("a").

let b = make_tracer("b");
Аналогично:
Creating b

Блок { … }:

let temp = make_tracer("temp"); → Creating temp

println!("Inside block"); → Inside block

let c = temp; — просто перемещение, без нового вывода.

println!("Exiting block"); → Exiting block

В конце блока выходит из области видимости c (он же temp), срабатывает Drop → Dropping temp

Инициализация container:

rust
let mut container = Container {
tracer: make_tracer("container1"),
};
→ Creating container1

Перезапись поля tracer:

```rust
container.tracer = make_tracer("container2");
Сначала вычисляется правая часть → Creating container2.
Затем старый container.tracer (то есть container1) сбрасывается → Dropping container1.```

Явное удаление b:

```rust
println!("Before dropping b explicitly");
mem::drop(b);
→ Before dropping b explicitly
Затем drop(b) вызывает Drop для b → Dropping b```

Выход из main:

```rust
println!("End of main");
→ End of main```

После этого по правилам Rust объекты уничтожаются в порядке обратном созданию (LIFO):

Сначала поле container.tracer (уже “container2”) → Dropping container2

Затем переменная a → Dropping a

Таким образом и получается приведённая последовательность.

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/918

View MORE
Open in Telegram


Rust Telegram | DID YOU KNOW?

Date: |

How to Use Bitcoin?

n the U.S. people generally use Bitcoin as an alternative investment, helping diversify a portfolio apart from stocks and bonds. You can also use Bitcoin to make purchases, but the number of vendors that accept the cryptocurrency is still limited. Big companies that accept Bitcoin include Overstock, AT&T and Twitch. You may also find that some small local retailers or certain websites take Bitcoin, but you’ll have to do some digging. That said, PayPal has announced that it will enable cryptocurrency as a funding source for purchases this year, financing purchases by automatically converting crypto holdings to fiat currency for users. “They have 346 million users and they’re connected to 26 million merchants,” says Spencer Montgomery, founder of Uinta Crypto Consulting. “It’s huge.”

NEWS: Telegram supports Facetime video calls NOW!

Secure video calling is in high demand. As an alternative to Zoom, many people are using end-to-end encrypted apps such as WhatsApp, FaceTime or Signal to speak to friends and family face-to-face since coronavirus lockdowns started to take place across the world. There’s another option—secure communications app Telegram just added video calling to its feature set, available on both iOS and Android. The new feature is also super secure—like Signal and WhatsApp and unlike Zoom (yet), video calls will be end-to-end encrypted.

Rust from cn


Telegram Rust
FROM USA