Rust programming language - Biblioteka.sk

Upozornenie: Prezeranie týchto stránok je určené len pre návštevníkov nad 18 rokov!
Zásady ochrany osobných údajov.
Používaním tohto webu súhlasíte s uchovávaním cookies, ktoré slúžia na poskytovanie služieb, nastavenie reklám a analýzu návštevnosti. OK, súhlasím


Panta Rhei Doprava Zadarmo
...
...


A | B | C | D | E | F | G | H | CH | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Rust programming language
 ...

Rust
Rust logo; a capital letter R set into a sprocket
Paradigms
DeveloperRust Foundation
First appearedMay 15, 2015; 9 years ago (2015-05-15)
Stable release
1.78.0[1] Edit this on Wikidata / May 2, 2024; 40 days ago (May 2, 2024)
Typing discipline
Implementation languageRust
PlatformCross-platform[note 1]
OSCross-platform[note 2]
LicenseMIT and Apache 2.0[note 3]
Filename extensions.rs, .rlib
Websitewww.rust-lang.org
Influenced by
Influenced

Rust is a multi-paradigm, general-purpose programming language that emphasizes performance, type safety, and concurrency. It enforces memory safety—meaning that all references point to valid memory—without a garbage collector. To simultaneously enforce memory safety and prevent data races, its "borrow checker" tracks the object lifetime of all references in a program during compilation.

Rust was influenced by ideas from functional programming, including immutability, higher-order functions, and algebraic data types. It is popular for systems programming.[13][14][15]

Software developer Graydon Hoare created Rust as a personal project while working at Mozilla Research in 2006. Mozilla officially sponsored the project in 2009. In the years following the first stable release in May 2015, Rust was adopted by companies including Amazon, Discord, Dropbox, Google (Alphabet), Meta, and Microsoft. In December 2022, it became the first language other than C and assembly to be supported in the development of the Linux kernel.

Rust has been noted for its rapid adoption,[16] and has been studied in programming language theory research.[17][18][19]

History

Mozilla Foundation headquarters in Mountain View, California

Origins (2006–2012)

Rust began as a personal project in 2006 by Mozilla Research employee Graydon Hoare.[20] Mozilla began sponsoring the project in 2009 as a part of the ongoing development of an experimental browser engine called Servo,[21] which was officially announced by Mozilla in 2010.[22][23] Rust's memory and ownership system was influenced by region-based memory management in languages such as Cyclone and ML Kit.[5]

Around the same time, work shifted from the initial compiler written in OCaml to a self-hosting compiler based on LLVM written in Rust. The new Rust compiler successfully compiled itself in 2011.[21][better source needed] In the fall of 2011, the Rust logo was developed based on a bicycle chainring.[24]

Evolution (2012–2020)

Rust's type system underwent significant changes between versions 0.2, 0.3, and 0.4. In version 0.2, which was released in March 2012, classes were introduced for the first time.[25] Four months later, version 0.3 added destructors and polymorphism, through the use of interfaces.[26] In October 2012, version 0.4 was released, which added traits as a means of inheritance. Interfaces were combined with traits and removed as a separate feature; and classes were replaced by a combination of implementations and structured types.[27]

Through the early 2010s, memory management through the ownership system was gradually consolidated to prevent memory bugs. By 2013, Rust's garbage collector was removed, with the ownership rules in place.[20]

In January 2014, the editor-in-chief of Dr. Dobb's Journal, Andrew Binstock, commented on Rust's chances of becoming a competitor to C++, along with D, Go, and Nim (then Nimrod). According to Binstock, while Rust was "widely viewed as a remarkably elegant language", adoption slowed because it radically changed from version to version.[28] The first stable release, Rust 1.0, was released on May 15, 2015.[29][30]

The development of the Servo browser engine continued alongside Rust's own growth. In September 2017, Firefox 57 was released as the first version that incorporated components from Servo, in a project named "Firefox Quantum".[31]

Mozilla layoffs and Rust Foundation (2020–present)

In August 2020, Mozilla laid off 250 of its 1,000 employees worldwide, as part of a corporate restructuring caused by the COVID-19 pandemic.[32][33] The team behind Servo was disbanded. The event raised concerns about the future of Rust, as some members of the team were active contributors to Rust.[34] In the following week, the Rust Core Team acknowledged the severe impact of the layoffs and announced that plans for a Rust foundation were underway. The first goal of the foundation would be to take ownership of all trademarks and domain names, and take financial responsibility for their costs.[35]

On February 8, 2021, the formation of the Rust Foundation was announced by its five founding companies (AWS, Huawei, Google, Microsoft, and Mozilla).[36][37] In a blog post published on April 6, 2021, Google announced support for Rust within the Android Open Source Project as an alternative to C/C++.[38]

On November 22, 2021, the Moderation Team, which was responsible for enforcing community standards and the Code of Conduct, announced their resignation "in protest of the Core Team placing themselves unaccountable to anyone but themselves".[39] In May 2022, the Rust Core Team, other lead programmers, and certain members of the Rust Foundation board implemented governance reforms in response to the incident.[40]

The Rust Foundation posted a draft for a new trademark policy on April 6, 2023, revising its rules on how the Rust logo and name can be used, which resulted in negative reactions from Rust users and contributors.[41]

Syntax and features

Rust's syntax is similar to that of C and C++,[42][43] although many of its features were influenced by functional programming languages such as OCaml.[44] Hoare described Rust as targeted at "frustrated C++ developers" and emphasized features such as safety, control of memory layout, and concurrency.[21] Safety in Rust includes the guarantees of memory safety, type safety, and lack of data races.

Hello World program

Below is a "Hello, World!" program in Rust. The fn keyword denotes a function, and the println! macro prints the message to standard output.[45] Statements in Rust are separated by semicolons.

fn main() {
    println!("Hello, World!");
}

Keywords and control flow

In Rust, blocks of code are delimited by curly brackets, and control flow is implemented by keywords including if, else, while, and for.[46] Pattern matching can be done using the match keyword.[47] In the examples below, explanations are given in comments, which start with //.[48]

fn main() {
    // Defining a mutable variable with 'let mut'
    // Using the macro vec! to create a vector
    let mut values = vec!;

    for value in &values {
        println!("value = {}", value);
    }

    if values.len() > 5 {
        println!("List is longer than five items");
    }

    // Pattern matching
    match values.len() {
        0 => println!("Empty"),
        1 => println!("One value"),
        // pattern matching can use ranges of integers
        2..=10 => println!("Between two and ten values"),
        11 => println!("Eleven values"),
        // A `_` pattern is called a "wildcard", it matches any value
        _ => println!("Many values"),
    };

    // while loop with predicate and pattern matching using let
    while let Some(value) = values.pop() {
        println!("value = {value}"); // using curly brackets to format a local variable
    }
}

Expression blocks

Rust is expression-oriented, with nearly every part of a function body being an expression, including control-flow operators.[49] The ordinary if expression is used instead of C's ternary conditional. With returns being implicit, a function does not need to end with a return expression; if the semicolon is omitted, the value of the last expression in the function is used as the return value,[50] as seen in the following recursive implementation of the factorial function:

fn factorial(i: u64) -> u64 {
    if i == 0 {
        1
    } else {
        i * factorial(i - 1)
    }
}

The following iterative implementation uses the ..= operator to create an inclusive range:

fn factorial(i: u64) -> u64 {
    (2..=i).product()
}

Closures

In Rust, anonymous functions are called closures.[51] They are defined using the following syntax:

|<parameter-name>: <type>| -> <return-type> { <body> };

For example:

let f = |x: i32| -> i32 { x * 2 };

With type inference, however, the compiler is able to infer the type of each parameter and the return type, so the above form can be written as:

let f = |x| { x * 2 };

With closures with a single expression (i.e. a body with one line) and implicit return type, the curly braces may be omitted:

let f = |x| x * 2;

Closures with no input parameter are written like so:

let f = || println!("Hello, world!");

Closures may be passed as input parameters of functions that expect a function pointer:

// A function which takes a function pointer as an argument and calls it with
// the value `5`.
fn apply(f: fn(i32) -> i32) -> i32 {
    // No semicolon, to indicate an implicit return
    f(5)
}

fn main() {
    // Defining the closure
    let f = |x| x * 2;

    println!("{}", apply(f));  // 10
    println!("{}", f(5));      // 10
}

However, one may need complex rules to describe how values in the body of the closure are captured. They are implemented using the Fn, FnMut, and FnOnce traits:[52]

  • Fn: the closure captures by reference (&T). They are used for functions that can still be called if they only have reference access (with &) to their environment.
  • FnMut: the closure captures by mutable reference (&mut T). They are used for functions that can be called if they have mutable reference access (with &mut) to their environment.
  • FnOnce: the closure captures by value (T). They are used for functions that are only called once.

With these traits, the compiler will capture variables in the least restrictive manner possible.[52] They help govern how values are moved around between scopes, which is largely important since Rust follows a lifetime construct to ensure values are "borrowed" and moved in a predictable and explicit manner.[53]

The following demonstrates how one may pass a closure as an input parameter using the Fn trait:

// A function that takes a value of type F (which is defined as
// a generic type that implements the `Fn` trait, e.g. a closure)
// and calls it with the value `5`.
fn apply_by_ref<F>(f: F) -> i32
    where F: Fn(i32) -> i32
{
    f(5)
}

fn main() {
    let f = |x| {
        println!("I got the value: {}", x);
        x * 2
    };
    
    // Applies the function before printing its return value
    println!("5 * 2 = {}", apply_by_ref(f));
}

// ~~ Program output ~~
// I got the value: 5
// 5 * 2 = 10

The previous function definition can also be shortened for convenience as follows:

fn apply_by_ref(f: impl Fn(i32) -> i32) -> i32 {
    f(5)
}

Types

Rust is strongly typed and statically typed. The types of all variables must be known at compilation time; assigning a value of a particular type to a differently typed variable causes a compilation error. Variables are declared with the keyword let, and type inference is used to determine their type.[54] Variables assigned multiple times must be marked with the keyword mut (short for mutable).[55]

The default integer type is i32, and the default floating point type is f64. If the type of a literal number is not explicitly provided, either it is inferred from the context or the default type is used.[56]

Primitive types

Summary of Rust's Primitive Types
Type Description Examples
bool Boolean value
  • true
  • false
u8 Unsigned 8-bit integer (a byte)
  • i8
  • i16
  • i32
  • i64
  • i128
Signed integers, up to 128 bits
  • u16
  • u32
  • u64
  • u128
Unsigned integers, up to 128 bits
  • usize
  • isize
Pointer-sized integers (size depends on platform)
Zdroj:https://en.wikipedia.org?pojem=Rust_programming_language
Text je dostupný za podmienok Creative Commons Attribution/Share-Alike License 3.0 Unported; prípadne za ďalších podmienok. Podrobnejšie informácie nájdete na stránke Podmienky použitia.






Text je dostupný za podmienok Creative Commons Attribution/Share-Alike License 3.0 Unported; prípadne za ďalších podmienok.
Podrobnejšie informácie nájdete na stránke Podmienky použitia.

Your browser doesn’t support the object tag.

www.astronomia.sk | www.biologia.sk | www.botanika.sk | www.dejiny.sk | www.economy.sk | www.elektrotechnika.sk | www.estetika.sk | www.farmakologia.sk | www.filozofia.sk | Fyzika | www.futurologia.sk | www.genetika.sk | www.chemia.sk | www.lingvistika.sk | www.politologia.sk | www.psychologia.sk | www.sexuologia.sk | www.sociologia.sk | www.veda.sk I www.zoologia.sk