驳斥关于Rust编程语言的四种刻板印象


Rust编程语言最初是一个业余项目,后来得到Mozilla的支持,它使普通程序员可以同时编写安全而又快速的系统:从计算器到负载很大的服务器。


由于其存在时间相对较短,因此该语言已经成功地获得了刻板印象,下面我将尝试反驳其中的四个。 我可以跳过一些要点,欢迎在评论中进行讨论。


  1. Rust是一种复杂的编程语言
  2. Rust是另一个C / C ++杀手
  3. 不安全破坏了Rust提供的所有保证
  4. Rust永远不会超过C / C ++

1. Rust-一种复杂的编程语言


编程语言的复杂性取决于存在大量彼此不一致的语法构造。 一个引人注目的示例是C ++和C#,因为对于大多数程序员而言,尽管存在大量语法元素,但C ++是C#所没有的最复杂的语言。 锈很均匀,因为 它最初的设计着眼于过去的错误,所有创新仅在与现有创新达成协议的情况下进行介绍。




这种构造型起源于链接生命周期的概念,该概念允许在语言的语义级别上描述所使用链接的有效性保证。 生命周期语法一开始看起来很奇怪:


struct R<'a>(&'a i32);
unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
    std::mem::transmute::<R<'b>, R<'static>>(r)
}

unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
                                             -> &'b mut R<'c> {
    std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
}

— , . 'static , .


" "? , , . , main() something , .. produce_something() :


int *produce_something(void) {
    int something = 483;
    return &something;
}

int main(void) {
    int *something = produce_something();
    int dereferenced = *something; // Segmentation fault (core dumped)
}

. , , foo bar:


fn sum<'a, 'b: 'a>(foo: &'b i32, bar: &'a i32) -> i32 {
    return foo + bar;
}

, . — , , . .




Rust ( Rust). , : — , — .


. x Foo, y , x:


struct Foo {
    data: Vec<u8>,
}

fn main() {
    let x = Foo { data: Vec::new() }; //  (owning)
    let y = &x; //  (borrowing)
}

, , , . , Rust :


  • ;
  • .

2. Rust — " C/C++"


— " ". Rust — , , , C/C++. — SIMD -.


Vala, Zig, Golang . , , , C/C++. Vala Zig , Golang , .. (, ).


, C/C++ - , , Rust , - Java.


3. Unsafe , Rust


Unsafe, , — Rust, , "" Rust. Borrow-checker , - . :


  • (FFI);
  • ;
  • ;
  • ;
  • .

, .. , . , .


//     UB :
fn safe_display() {
    unsafe {
        let x = 385;
        let x_ref: *const i32 = &x;
        println!("{}", *x_ref);
    }
}



: "Rust , ". , .. , Rust, . Rust .




: "Unsafe , Rust ". : " Java , , UB".


, UB, UB Rust. "", , "UB ", , . UB :


, (, ) , : « ».

4. Rust C/C++


. , , Rust, , C/C++. Rust GCC C:





, RapidJSON serde_json. serde_json DOM , RapidJSON, / serde_json RapidJSON (DOM) GCC, CLANG:



Rustls, OpenSSL ( 10% 20%-40% , 10%-20% 30%-70% ).




, Rust, rustc, C/C++ , CLANG, LLVM IR. , — .


rustc GCC/CLANG, , , . Rust C CLANG:


:
[https://godbolt.org/z/7b46MD]


pub fn compute(input: &mut [i64; 8]) {
    for i in 0..input.len() {
        input[i] = (input[i] + 3254) * 3;
    }
}

<T as core::convert::From<T>>::from:
        mov     rax, rdi
        ret

<T as core::convert::Into<U>>::into:
        mov     rax, rdi
        ret

<T as core::convert::TryFrom<U>>::try_from:
        mov     rax, rdi
        ret

<I as core::iter::traits::collect::IntoIterator>::into_iter:
        mov     rdx, rsi
        mov     rax, rdi
        ret

example::compute:
        xor     eax, eax
        jmp     .LBB4_1
.LBB4_3:
        mov     rcx, qword ptr [rdi + 8*rax]
        lea     rcx, [rcx + 2*rcx]
        add     rcx, 9762
        mov     qword ptr [rdi + 8*rax], rcx
        inc     rax
.LBB4_1:
        cmp     rax, 8
        jne     .LBB4_3
        ret

:
[https://godbolt.org/z/YOey3P]


#include <stdint.h>

void compute(int64_t *input) {
    for (int i = 0; i < 8; i++) {
        input[i] = (input[i] + 3254) * 3;
    }
}

compute:                                # @compute
        xor     eax, eax
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        cmp     rax, 8
        je      .LBB0_2
        mov     rcx, qword ptr [rdi + 8*rax]
        lea     rcx, [rcx + 2*rcx]
        add     rcx, 9762
        mov     qword ptr [rdi + 8*rax], rcx
        inc     rax
        jmp     .LBB0_1
.LBB0_2:
        ret

rustc , CLANG, — LLVM. rustc, Rust, .. ( CLANG).




Rust restrict -, . C/C++ , / restrict, .



Rust , , ( ) . , . — .


Source: https://habr.com/ru/post/zh-CN467901/


All Articles