Rust (programming language): Difference between revisions

Content deleted Content added
start unsafe section
minor c/e, slight promotional tone
Line 453:
==== Standard library ====
 
The Rust [[standard library]] defines and implements many widely used custom data types, including core data structures such as {{code|Vec}}, {{code|Option}}, and {{code|HashMap}}, as well as [[smart pointer]] types. Rust also provides a way to exclude most of the standard library using the attribute {{rust|#![no_std]}};, this enablesfor applications, such as embedded devices, which want to remove dependency code or provide their own core data structures. Internally, the standard library is divided into three parts, {{code|core}}, {{code|alloc}}, and {{code|std}}, where {{code|std}} and {{Code|alloc}} are excluded by {{rust|#![no_std]}}.{{sfn|Gjengset|2021|pp=213-215}}
 
Rust uses [[Option type|<code>Option</code>]] to define optional values, which can be matched using <code>if let</code> or <code>match</code> to access the inner value:{{sfn|Klabnik|Nichols|2023|pp=108-110,113-114,116-117}}
Line 482:
 
=== Polymorphism ===
Rust enablessupports [[bounded parametric polymorphism]] through [[Trait (computer programming)|traits]] and [[generic function]]s.{{sfn|Klabnik|Nichols|2023|p=378}} First, commonCommon behavior between types may be declared using traits and {{code|impl}}s:{{sfn|Klabnik|Nichols|2023|pp=192-198}}
 
<syntaxhighlight lang="rust">
Line 504:
</syntaxhighlight>
 
The example above also includes a method {{code|is_zero}} which provides a default implementation, itthat is not required when implementing the trait.{{sfn|Klabnik|Nichols|2023|pp=192-198}}
 
A function can then be made generic throughby adding type parameters inside angle brackets ({{code|<Num>}}), which only allow types that implement the trait:
 
<syntaxhighlight lang="rust">