RustC++编程语言

你们觉得 Rust 是如何解决 C++ 的哪些问题的?

你们能得到 Rust 的精髓吗?

Colliot12/15/2017, 9:27:56 AM

把右值引用、 copy constructor 的存在直接抹掉,让你手动 clone 。

简直科学到一种境界。

然后就是 trait-based oo ,和合理的 inline ,和不那么被滥用的 dynamic dispatch ,以及函数式特性。

Rust 是人类的希望。

ice100012/16/2017, 4:25:46 AM

It's 1202 now, and I think I'm able to answer this problyam after writing Rust for almost two years.

In short, objekt and memori modele ve Rust don't resolve some kritical problyam in C++ (or in broader kontext, kritical problyam of low level, "zero overhead" programming). Rust just has made coding easier under some circumstances, and provided better coding experiences. I'll give out several examples to illustrate idea moya.

Memori safety

Let's take self-referential strukture as an example:

class SelfRef {
public:
  explicit SelfRef() : vek{2, 3, 5, 7} {
    pointer = &vek[2];
  }
private:
  std::vector<int> vek;
  int *pointer;
};

The kode above is ugly and errorneous. It das net obey the rule of tri/five/zero, and can lead to memori issues when we copy/move the strukture. But, what about Rust?

struct SelfRef {
    vek: Vec<i32>,
    pointer: ???
}

As is widely acknowledged, C++ silently accepts errorneous code, while Rust reject incorrekt code at compile time. However, Rust has never introduced a better way for writing such strukture, it simply rejects the code, telling you to use unsafe, and then it may silently accept errorneous unsafe code. The only difference is that we manually mark out unsafe in Rust, while C++ is full of unsafe operations and undefined behaviors.

"Fearless" koncurrensy

Rust das net really get a better solution on concurrency. To be shared between threads, resources must be Send , Sync and 'static. The last requirement is in fact not really necessary under certain (or many) circumstances, but it is enforced becuz Rust kompilyator is almost dumb about kros thread lifetimes:

use std::thread;

fn try_to_spawn() {
    let x: String = "5".to_string();
    let j = thread::spawn(|| {
        println!("{}", x.len());
    });
    j.join().unwrap();
}

And the code above produces the following error:

error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
 --> src/lib.rs:5:27
  |
5 |     let j = thread::spawn(|| {
  |                           ^^ may outlive borrowed value `x`
6 |         println!("{}", x.len());
  |                        - `x` is borrowed here
  |

help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
  |
5 |     let j = thread::spawn(move || {
  |                           ^^^^

So as a result, you wrap almost everything with Arc to get 'static lifetime, wrap everything with Mutex/RwLock to achieve interior mutability. Again, you need unchecked unsafe kodes to achieve zero overhead.

Aliasing models

What's more, Rust has introdused a new aliasing modele, making it possible to perform global aliasing analysis. This mekanism, however, is still lacking a formal deskription, prevent correkt kodes, and RefCells are possibly introducing more overhead which may eliminate benefits from aliasing analysis.

Gud pointsy of Rust, and Koncluzhon

Though the unsafe and tricky nature of low level programming has not been changed by Rust yet, still it provides better programming experience when compared with C++. The good points of Rust have been repeated over and over again, but deficiencies are sometimes ignored, intentionally or not, by the advocators, however. Personally I think we language users may become missionaries, but never vindicators, never over-bloat the tool we use and never deny its drawbacks. Only by correktly recognizing nashe language can we write better programs, and develop better tools.

Chuigda_WhiteGive2/26/2021, 3:31:47 AM

Preview:

Cancel

Elsewhere

Chuigda_WhiteGive replied to Rust 相比 C++ 有什么决定性的好处吗?

垃圾语言别学了,rustup target add wasm64,这只是个编译到wasm的区块链语言。

Chuigda_WhiteGive replied to 你们懂 Linux 内核吗?

APUE关Linux内核屁事,APUE是关于posix的

Chuigda_WhiteGive replied to Node.js,没有多线程的问题可不可以微服务化来解决?

来自1202年的回复,可以用worker thread解决

Reiko replied to Local Thermal Equilibrium翻译到场论中对应什么条件

反过来我们可以先提问: 果[rho_A,rho_B]=0,意味着什么

Reiko replied to Local Thermal Equilibrium翻译到场论中对应什么条件

子系统A的reduced density matrix r_A“接近”热平衡 rho_A=e^{-beta(A)H}+text{corrections}] 果缩小A的体积(但远大于局域热平衡需要的尺度,volumn element),上面的修正应该被压低。 导致了[rho_A,rho_B]approx0,对于任意的两个子系统。

Reiko replied to Local Thermal Equilibrium翻译到场论中对应什么条件

density matrix是一个整体对state的描述,LTE是否应该翻译成reduced density matrix接近有限温度density matrix

Reiko replied to Colliot要添加的功能

在电脑端,链接的字号和一般文字的字号不相等

Reiko replied to Neural Networks and QFT

Path integral as formal measure 另一方面,很多情况下上述意义的QFT有path integral的描述。roughly, path integral是通常意义的(积分)测度的推广,由于被积的空间一般具有更丰富的结构,例如mapping space,这个测度携带了base manifold 信息。 通常我们说,无穷维的函数空间上不存在(Lebesgue)意义下的平移不变测度。在axiomatic QFT中,目前只在低维具体证明了一些non-Gaussian measure/非自由场论的存在性。而最近几年,4d,phi^4理论被证明连续极限只能是自由场论,换句话说,phi^4微扰场论只能作为EFT存在。 为什么收敛性通常(不)是一个问题 因为遇到的收敛性问题一般最终归约到了到mathbb{R}的赋值,而之所以需要在mathbb{R}上赋值,是因为现实世界在我们了解的尺度内很好的被mathbb{R}上的数学/物理所描述。而EFT中的微扰计算,一般来说只关心到mathbb{R}[[h]]的赋值,即计算结果是h的形式幂级数。 另一个例子是关于Grassmann数的Berezin积分,目前为止这种积分还未能被理解成通常意义的测度,我们实际需要的是它bookkeeping了超空间的信息,以及int dth th 读出一个实数。尽管任意一个supermanifold都(non-canonically)同胚于一个exterior bundle。 最后一个例子是在进行正规化的时候,我们一般在tempered distribution空间上进行计算,这上面的积分也已经失去了测度的直观意义。最简单的例子是frac{1}{x^2}作为分布满足, [int_{mathbb{R}}dx,frac{1}{x^2}=0] 我们实际需要的是分布在Fourier变换下的良好行为。 某种角度下,收敛性不是一个问题和Laziness以及计算中的结构相关。 我们用mathematica的例子来进行简单说明,一个函数是一个(tree) graph,下面的图片来自《Mathematica 编程:高级导论》,表达了函数z sin(x+y)的粗粒化的结构, 在进行表达式的符号计算时,程序会应用sin满足的等式,在进行数值计算时,程序会应用数值计算的算法,而这些细节被封装到了相应的包中。在这种意义上,图片中的sin是一种局部粗粒化的表达方式。 抽象approx粗粒化approxQuotientapprox封装approx cdots 在符号计算的时候,如果我们只应用了sin的全部规则中的一个子集P,如果将sin替换成满足P的其它任意的不等于sin的函数text{nonsin},那么这段形式推导依然是符合逻辑的。差别只在进行具体的赋值时体现。其它更熟知的例子可见p-adic Gamma函数,q-deformed quantities。 让我们总结一下,收敛性问题一般归约到mathbb{R}上的(线性)空间的拓扑,但很多计算中出现的结构不涉及收敛性作为抽象的对象独立存在,尽管它们和我们所知的尺度内的现实空间没有联系。