Rust 会释放被覆盖变量的内存吗? [英] Does Rust free up the memory of overwritten variables?

查看:28
本文介绍了Rust 会释放被覆盖变量的内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rust 的书里看到你可以定义两个不同的同名变量:

let hello = "你好";let hello = "再见";println!("我的变量hello包含:{}", hello);

打印出来:

我的变量hello包含:再见

第一个你好会发生什么?它会被释放吗?我怎样才能访问它?

我知道将两个变量命名为相同的名称会很糟糕,但是如果因为我在下面 100 行处声明它而意外发生这种情况,那可能会很痛苦.

解决方案

Rust 没有垃圾收集器.

<块引用>

Rust 会释放被覆盖变量的内存吗?

是的,否则就是内存泄漏,这将是一个非常糟糕的设计决策.重新分配变量时释放内存:

struct Noisy;impl Drop for Noisy {fn drop(&mut self) {eprintln!(丢弃")}}fn 主(){eprintln!(0");让 mut 东西 = 嘈杂;eprintln!(1");东西=嘈杂;eprintln!(2");}

01掉落2掉落

<块引用>

第一个你好会发生什么

它是阴影.

没有什么特别的"发生在变量引用的数据上,除了您无法再访问它的事实.当变量超出范围时它仍然被删除:

struct Noisy;impl Drop for Noisy {fn drop(&mut self) {eprintln!(丢弃")}}fn 主(){eprintln!(0");让事情=嘈杂;eprintln!(1");让事情=嘈杂;eprintln!(2");}

012掉落掉落

另见:

<块引用>

我知道将两个变量命名为相同的名称会很糟糕

这不是坏",而是设计决策.我会说像这样使用阴影是一个坏主意:

let x = "Anna";println!("用户名是{}", x);让 x = 42;println!("税率为{}", x);

像这样使用阴影对我来说是合理的:

let name = String::from("薇薇安");让 name = name.trim();println!("用户名是{}", name);

另见:

<块引用>

但如果这是偶然发生的,因为我在下面 100 行声明它可能会很痛苦.

不要有太大的功能以至于您不小心"做一点事.这适用于任何编程语言.

<块引用>

有没有手动清理内存的方法?

你可以调用drop:

eprintln!(0");让事情=嘈杂;掉落(东西);eprintln!(1");让事情=嘈杂;eprintln!(2");

0掉落12掉落

然而,正如 oli_obk - ker 指出的,变量占用的栈内存直到函数退出才会释放,只释放变量占用的资源.

drop 的所有讨论都需要展示其(非常复杂的)实现:

fn drop(_: T) {}

<块引用>

如果我在其他函数之外的全局范围内声明变量会怎样?

全局变量永远不会被释放,如果你甚至可以创建它们开始.

I saw in the Rust book that you can define two different variables with the same name:

let hello = "Hello";
let hello = "Goodbye";

println!("My variable hello contains: {}", hello);

This prints out:

My variable hello contains: Goodbye

What happens with the first hello? Does it get freed up? How could I access it?

I know it would be bad to name two variables the same, but if this happens by accident because I declare it 100 lines below it could be a real pain.

解决方案

Rust does not have a garbage collector.

Does Rust free up the memory of overwritten variables?

Yes, otherwise it'd be a memory leak, which would be a pretty terrible design decision. The memory is freed when the variable is reassigned:

struct Noisy;
impl Drop for Noisy {
    fn drop(&mut self) {
        eprintln!("Dropped")
    }
}

fn main() {
    eprintln!("0");
    let mut thing = Noisy;
    eprintln!("1");
    thing = Noisy;
    eprintln!("2");
}

0
1
Dropped
2
Dropped

what happens with the first hello

It is shadowed.

Nothing "special" happens to the data referenced by the variable, other than the fact that you can no longer access it. It is still dropped when the variable goes out of scope:

struct Noisy;
impl Drop for Noisy {
    fn drop(&mut self) {
        eprintln!("Dropped")
    }
}

fn main() {
    eprintln!("0");
    let thing = Noisy;
    eprintln!("1");
    let thing = Noisy;
    eprintln!("2");
}

0
1
2
Dropped
Dropped

See also:

I know it would be bad to name two variables the same

It's not "bad", it's a design decision. I would say that using shadowing like this is a bad idea:

let x = "Anna";
println!("User's name is {}", x);
let x = 42;
println!("The tax rate is {}", x);

Using shadowing like this is reasonable to me:

let name = String::from("  Vivian ");
let name = name.trim();
println!("User's name is {}", name);

See also:

but if this happens by accident because I declare it 100 lines below it could be a real pain.

Don't have functions that are so big that you "accidentally" do something. That's applicable in any programming language.

Is there a way of cleaning memory manually?

You can call drop:

eprintln!("0");
let thing = Noisy;
drop(thing);
eprintln!("1");
let thing = Noisy;
eprintln!("2");

0
Dropped
1
2
Dropped

However, as oli_obk - ker points out, the stack memory taken by the variable will not be freed until the function exits, only the resources taken by the variable.

All discussions of drop require showing its (very complicated) implementation:

fn drop<T>(_: T) {}

What if I declare the variable in a global scope outside of the other functions?

Global variables are never freed, if you can even create them to start with.

这篇关于Rust 会释放被覆盖变量的内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆