锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / Rust开源 / Rust和C++比较

服务方向

人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发

联系方式

固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

Rust和C++比较

Introduction介绍

The Rust programming language was developed by Mozilla with the aim of creating a better tool for developing their browser Mozilla Firefox. However, the language appeared to be so effective, that many programmers are now opting to use it for software development instead of C++. Rust is syntactically similar to C++, but it provides increased speed and better memory safety. Rust编程语言是由Mozilla开发的,旨在创建一个更好的工具来开发其浏览器Mozilla Firefox。但是,该语言似乎非常有效,以至于许多程序员现在选择将其用于软件开发,而不是C ++。 Rust在语法上类似于C ++,但是它提供了更快的速度和更好的内存安全性。

In order to explain why Rust is a safer and faster language than C++, we decided to create a Rust vs C++ comparison chart that clearly shows the differences between these two languages. This article will be useful for people who are familiar with C++, but are new to Rust. 为了解释为什么Rust是比C ++更安全,更快的语言,我们决定创建一个Rust与C ++的对比表,以清楚地显示这两种语言之间的差异。本文对熟悉C ++但对Rust还是陌生的人很有用。

For better comparison, we’ve chosen features that reveal the key similarities and differences between these two languages. 为了更好地进行比较,我们选择了可以揭示这两种语言之间的主要异同的功能。

Zero-cost Abstraction零成本抽象

Issue C++ Rust
Preferring code duplication to abstraction due to high cost of virtual method calls由于虚拟方法调用的成本高昂,因此代码复制优先于抽象 Zero-cost abstraction mechanisms allow you to avoid runtime costs when possible.零成本抽象机制使您可以尽可能避免运行时成本。 Zero-cost abstraction mechanisms allow you to avoid runtime costs when possible.零成本抽象机制使您可以尽可能避免运行时成本。

Move Semantics移动语义

Issue C++ Rust
Move constructors may leave objects in invalid and unspecified states and cause use-after-move errors移动构造函数可能会使对象处于无效和未指定状态,并导致移动后使用错误 Move constructors are suggested to leave the source object in a valid state (yet the object shouldn’t be used in correct programs).建议使用Move构造函数将源对象保持在有效状态(但不应在正确的程序中使用该对象)。 A built-in static analyzer disallows use of objects after they have been moved.内置的静态分析器禁止在对象移动后使用它们。
Use-after-move errors are detected at runtime using a special sentinel state.在运行时使用特殊的前哨状态检测移动后使用错误。 The compiler can rely on this built-in analyzer for optimization.编译器可以依靠此内置分析器进行优化。
External static code analyzers can spot use-after-move errors at compile time.外部静态代码分析器可以在编译时发现移动后使用错误。

Smart Pointers vs. null Pointers智能指针与空指针

Issue C++ Rust
Use-after-free, double-free bugs, dangling pointers释放后使用,双重释放的错误,悬空的指针 Smart pointers and references are preferred to raw pointers.智能指针和引用优于原始指针。 Smart pointers and references are preferred to raw pointers.智能指针和引用优于原始指针。
Manual code review can spot use of raw pointers where smart pointers would suffice.手动检查代码可以发现原始指针的使用,而智能指针就足够了。 Raw pointers can only be used inside unsafe blocks, which can automatically be found by tools.原始指针只能在不安全的块中使用,不安全的块可以由工具自动找到。
Null dereferencing errorsNull 取消引用错误 References are preferred to pointers and cannot be null.引用优先于指针,不能为null。 References are preferred to pointers and cannot be null.引用优先于指针,不能为null。
Null dereferencing is still possible even for smart pointers, but is declared as undefined behavior and should never appear.Null 即使对于智能指针,取消引用仍然是可能的,但是被声明为未定义行为,并且永远不会出现。 Null references can be emulated by Option types, which require explicit null checks before use.Null引用可以由Option类型模拟,null使用前需要进行显式检查。
Compilers assume that undefined behavior never happens, don’t produce warnings, and use this for optimization (sometimes with fatal consequences for security).编译器假定不会发生未定义的行为,不会产生警告,并将其用于优化(有时会对安全性造成致命后果)。 Smart pointers return Optional references and therefore require explicit checks as well.智能指针返回Optional引用,因此也需要显式检查。
External static code analyzers can spot possible errors at compile time.外部静态代码分析器可以在编译时发现可能的错误。 Raw pointers can be null, but they can only be used inside unsafe blocks. Unsafe blocks need to be carefully reviewed, but they can be found and marked automatically.原始指针可以是null,但只能在不安全的块内使用。需要仔细检查不安全的块,但是可以自动找到并标记它们。

Internal Buffer内部缓冲器

Issue C++ Rust
Buffer overflow errors缓冲区溢出错误 Explicitly coded wrapper classes enforce range checks.显式编码的包装器类强制执行范围检查。 All slice types enforce runtime range checks.所有片类型都强制执行运行时范围检查。
Debugging builds of the STL can perform range checks in standard containers.STL的调试版本可以在标准容器中执行范围检查。 Range checks are avoided by most common idioms (e.g. range-based for iterators).最常见的惯用法避免了范围检查(例如,基于范围的迭代器)。

Data Races数据竞赛

Issue C++ Rust
Data races (unsafe concurrent modification of data)数据争用(不安全的并行数据修改) Good programming discipline, knowledge, and careful review are required to avoid concurrency errors.需要良好的编程纪律,知识和认真的审核,以避免并发错误。 The built-in borrow checker and Rust reference model detect and prohibit possible data races at compile time.内置的借阅检查器和Rust参考模型检测并禁止在编译时可能发生的数据竞争。
External static code analyzers can spot some errors at compile time.外部静态代码分析器可以在编译时发现一些错误。 A novel locking API makes it impossible to misuse mutexes unsafely (though still allowing incorrectusage).新颖的锁定API使得不可能不安全地滥用互斥体(尽管仍然允许错误使用)。
External code sanitizers can spot some errors at runtime.外部代码清理程序可以在运行时发现一些错误。

Object Initialization对象初始化

Issue C++ Rust
Uninitialized variables未初始化的变量 Constructors of user-defined types are recommended to initialize all object fields.建议使用用户定义类型的构造函数来初始化所有对象字段。 All variables must be explicitly initialized before use (checked by the compiler).在使用之前必须对所有变量进行显式初始化(​​由编译器检查)。
Primitive types still have undefined values when not initialized explicitly.当未显式初始化时,原始类型仍具有未定义的值。 All types have defined default values that can be chosen instead of explicit initialization types.所有类型都有定义的默认值,可以选择这些默认值,而不是显式的初始化类型。
External static code analyzers can spot uninitialized variables.外部静态代码分析器可以发现未初始化的变量。

Static (compile-time) Polymorphism静态(编译时)多态

Issue C++ Rust
Static interfaces for static polymorphism静态接口用于静态多态 Concepts should provide this feature directly, but they’ve been in development since 2015 and are only scheduled for standardization in C++20.概念应直接提供此功能,但自2015年以来一直在开发中,仅计划在C ++ 20中进行标准化。 Traits provide a unified way of specifying both static and dynamic interfaces.特性提供了指定静态和动态接口的统一方法。
Virtual functions and abstract classes may be used to declare interfaces.Virtual函数和abstract类可用于声明接口。 Static polymorphism is guaranteed to be resolved at compile time.静态多态性保证在编译时解决。
Virtual function calls may be optimized by particular compilers in known cases.Virtual 在已知情况下,特定的编译器可以优化函数调用。

Adding New Traits添加新特性

Issue C++ Rust
Extending externally defined classes with new methods用新方法扩展外部定义的类 Adding new methods normally requires inheritance, which can be inconvenient.添加新方法通常需要继承,这可能很不方便。 Traits can be added to and implemented for any class in any module at a later point.以后可以将特性添加到任何模块中的任何类并对其实施。
Unified function call syntax could be used to emulate extension methods (if it gets into C++20).统一函数调用语法可用于模拟扩展方法(如果它进入C ++ 20)。 Modules restrict visibility of available methods.模块限制了可用方法的可见性。

Standard Library标准库

Issue C++ Rust
Legacy design of utility types heavily used by standard library标准库大量使用的实用程序类型的旧版设计 Structured types like std::pair, std::tuple and std::variant can replace ad-hoc structures.结构化类型一样std::pair,std::tuple而且std::variant可以替代的ad-hoc结构。 Built-in composable structured types: tuples, structures, enumerations.内置可组合的结构化类型:元组,结构,枚举。
These types have inconvenient interfaces (though C++17 improves this).这些类型的接口不方便(尽管C ++ 17对此进行了改进)。 Pattern matching allows convenient use of structured types like tuples and enumerations.模式匹配允许方便地使用结构化类型,例如元组和枚举。
Most of the standard library doesn’t use structured types.大多数标准库都不使用结构化类型。 The standard library fully embraces available pattern matching to provide easy-to-use interfaces.标准库完全包含可用的模式匹配,以提供易于使用的界面。

Branches in Switch StatementsSwitch语句中的分支

Issue C++ Rust
Forgetting to handle all possible branches in switch statements忘记处理switch语句中的所有可能分支 Code review and external static code analyzers can spot switch statements that don’t cover all possible branches.代码检查和外部静态代码分析器可以发现switch未涵盖所有可能分支的语句。 The compiler checks that match expressions explicitly handle all possible values for an expression.编译器检查匹配表达式是否显式处理表达式的所有可能值。

Typing of Variables变量类型

Issue C++ Rust
Complex variable types become tedious to type manually复杂的变量类型很难手动输入 The auto and decltype keywords provide limited type inference (for expressions).关键字提供有限的类型推断(为表述)。 Local type inference (for a function body) allows you to explicitly specify types less frequently.局部类型推断(用于函数体)使您可以显式地较少指定类型。
Lambda functions still require manual type specifications, but this is improving with C++17.Lambda函数仍然需要手动类型规范,但这在C ++ 17中得到了改进。 Function declarations still require explicit types which ensures good readability of code.函数声明仍然需要显式类型,以确保良好的代码可读性。

Runtime Environment运行环境

Issue C++ Rust
Embedded and bare-metal programming have high restrictions on runtime environment嵌入式和裸机编程对运行时环境有很高的限制 The C++ runtime is already fairly minimal, as it directly compiles to machine code and doesn’t use garbage collection.C ++运行时已经非常短了,因为它可以直接编译为机器代码并且不使用垃圾回收。 The Rust runtime is already fairly minimal as it directly compiles to machine code and doesn’t use garbage collection.Rust运行时已经相当少了,因为它可以直接编译为机器代码并且不使用垃圾回收。
C++ programs can be built without the standard library with disabled exceptions and dynamic type information, etc.无需标准库即可构建C ++程序,该库具有禁用的异常和动态类型信息等。 Rust programs can be built without the standard library with disabled range checks, etc.Rust程序可以在没有标准库的情况下构建,而标准库没有禁用范围检查等。

Using Libraries Written in Other Languages使用其他语言编写的库

Issue C++ Rust
Using existing libraries written in C and other languages使用以C和其他语言编写的现有库 C libraries are immediately usable by C++ programs.C库可立即由C ++程序使用。 C libraries require Rust-specific header declarations.C库需要特定于Rust的标头声明。
Libraries in languages other than C++ require wrappers.非C ++语言的库需要包装器 Libraries in languages other than Rust require wrappers.Rust以外的其他语言的库需要包装器。
Exporting a C interface requires only a simple extern declaration.导出C接口仅需要一个简单的extern声明。 Exporting a C interface requires only a simple extern declaration.导出C接口仅需要一个简单的extern声明。
There’s no overhead in calling C functions from C++ or calling C++ functions from C.从C ++调用C函数或从C调用C ++函数没有开销。 There’s no overhead in calling C functions from Rust or calling Rust functions from C.从Rust调用C函数或从C调用Rust函数没有任何开销。

Conclusion

In this article, we compared Rust and C++. You have no doubt noticed that both languages use zero-cost abstractions and move semantics. They also both have smart pointers, no garbage collection, and other similarities. 在本文中,我们比较了Rust和C ++。您毫无疑问地注意到这两种语言都使用零成本的抽象并移动语义。它们也都具有智能指针,没有垃圾回收,还有其他相似之处。

In contrast to C++, Rust has a built-in static analyzer but no uninitialized variables. 与C ++相比,Rust具有内置的静态分析器,但没有未初始化的变量。

Rust avoids possible data races, informs about undefined behavior, and allows null raw pointers inside unsafe blocks. The Rust language also has other distinctive features that allow programmers to achieve better safety and performance of their software. If you’re interested in the basics of Rust, check out our series of in-depth Rust tutorials starting from part 1 . Rust避免了可能的数据争用,通知了未定义的行为,并在不安全的块内允许了空的原始指针。 Rust语言还具有其他独特功能,使程序员可以实现软件的更高安全性和性能。如果您对Rust的基础知识感兴趣,请查看从第1部分开始的一系列深入的Rust教程。


友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内