Refute four stereotypes about the programming language Rust







The Rust programming language , started as a hobby project and subsequently supported by Mozilla , allows ordinary programmers to write both safe and fast systems at the same time: from calculators to highly loaded servers.







For its relatively short time of existence, this language has already managed to acquire stereotypes, four of which I will try to refute below. I can skip some points, discussions in the comments are welcome.







  1. Rust is a complex programming language
  2. Rust is another C / C ++ killer
  3. Unsafe ruins all guarantees provided by Rust
  4. Rust will never overtake C / C ++ in speed


1. Rust - a complex programming language



The complexity of the programming language is determined by the presence of a large number of syntax constructs that are inconsistent with each other. A striking example is C ++ and C #, because for the vast majority of programmers, C ++ is the most complicated language that C # is not, despite the large number of syntax elements. Rust is pretty uniform because It was originally designed with an eye to the mistakes of the past, and all innovations are introduced solely subject to agreement with existing ones.










This stereotype has its roots in the concept of lifetimes of links, which allows describing the guarantees of the validity of the links used at the level of the semantics of the language. The life-time syntax looks strange at first:







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 , - . :









, .. , . , .







//     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 , , ( ) . , . โ€” .










All Articles