Informatika Komputer    
   
Daftar Isi
(Sebelumnya) Russian languageS MIME (Berikutnya)

Rust (programming language)

Rust
Rust programming language black logo.svg
Paradigm(s)compiled, concurrent, functional, imperative, object-oriented, structured
Appeared in2010
Designed byGraydon Hoare
DeveloperMozilla
Preview release0.5[1] (2012-12-21)
Typing disciplinestatic, strong, inferred, structural
Influenced byAlef, C++, Camlp4, Common Lisp, Erlang, Haskell, Hermes, Limbo, Napier, Napier88, Newsqueak, NIL, Sather, Standard ML
OSLinux, Mac OS X, Windows, FreeBSD
LicenseApache License 2.0 or MIT License[2]
Usual filename extensions.rc, .rs

Rust is an experimental, multi-paradigm, compiled programming language developed by Mozilla Research.[3] It is designed to be a "safe, concurrent, practical language",[4][5] supporting pure-functional, concurrent-actor, imperative-procedural, and object-oriented styles.

The language grew out of a personal project by lead developer Graydon Hoare, who began work on it in 2006; his employer Mozilla became involved in 2009[6] and officially unveiled it for the first time in 2010.[7] The same year, work shifted from the initial compiler (written in OCaml) to the self-hosted compiler written in Rust itself.[8] Known as rustc, it successfully compiled itself in 2011.[9] The self-hosted compiler uses LLVM as its backend.

The first numbered alpha release of the Rust compiler occurred in January 2012.[10] The current release is version 0.5, released in December 2012.[1] As of October 2012, there is no estimated release date for beta release.

As per Mozilla policy,[11] Rust is developed entirely in the open and solicits feedback and contributions from the community. The design of the language has been refined through the experiences of writing the Servo layout engine and the Rust compiler itself.

Contents

Description

The goal of Rust is to be a good language for the creation of large client and server programs that run over the Internet. This has led to a feature set with an emphasis on safety, control over memory layout and concurrency. Performance should be comparable to idiomatic C++.[12]

The syntax of Rust is similar to C and C++, with blocks of code delimited by curly braces, and control-flow keywords such as if, else, do, while, and for. Not all C or C++ keywords are present, however, while others (such as the match keyword for multi-directional branching) will be less familiar to programmers coming from these languages. Despite the syntactic resemblance, Rust is semantically very different from C and C++.

The system is designed to be memory safe, and it does not permit null pointers or dangling pointers. Data values can only be initialized through a fixed set of forms, all of which require their inputs to be already initialized.[13]

The type system supports type classes, inspired directly by the Haskell language. This is a facility for ad-hoc polymorphism, achieved by adding constraints to type variable declarations. Other features from Haskell, such as higher-kinded polymorphism, are not supported.

Rust features type inference, for variables declared with the let keyword. Such variables do not require a value to be initially assigned in order to determine their type. A compile-time error results if any branch of code fails to assign a value to the variable.[14] Functions can be given generic parameters but they must be explicitly bounded by traits, there is no way to leave off type signatures while still making use of methods and operators on the parameters.

Concurrency is supported through lightweight tasks, similar to those found in Erlang and other actor-based languages. In such systems, tasks communicate via message passing, rather than sharing data directly. For performance reasons, it is possible to send data without copying, using unique boxes. Unique boxes are guaranteed to only have one owner, and can be released by the sending task for use by the receiver.

The object system within Rust is based around implementations, traits and structured types. Implementations fulfil a role similar to that of classes within other languages, and are defined with the impl keyword. Inheritance and polymorphism are provided by traits; they allow methods to be defined and mixed in to implementations. Structured types are used to define fields. Implementations and traits cannot define fields themselves, and only traits can provide inheritance, in order to prevent the diamond inheritance problem of C++.

Examples

The following code examples are valid as of Rust 0.5. Syntax and semantics may change in subsequent versions.

Hello world:

fn main() { io::println("hello, world");}

Two versions of the factorial function, in both recursive and iterative styles:

/* The branches in this function exhibit Rust's optional implicit return   values, which can be utilized where a more "functional" style is preferred.   Unlike C++ and related languages, Rust's `if` construct is an expression   rather than a statement, and thus has a return value of its own. */fn recursive_factorial(n: int) -> int { if n <= 1 { 1 } else { n * recursive_factorial(n-1) }} fn iterative_factorial(n: int) -> int { // The `mut` keyword denotes variables that may be mutated. let mut i = 1, result = 1; while i <= n { result *= i; i += 1; } return result; // An explicit return, in contrast to the above.}

A simple demonstration of Rust's lightweight concurrency capabilities:

use task::spawn; /* This function creates ten "tasks" that all execute concurrently. Run it   several times and observe the irregular linebreaks that result as each task   races on stdout, due to the fact that `io::println` may yield to the task   scheduler between printing its argument and printing a newline. */fn main() { // `times` is a method on numeric types whose only argument is a closure. for 10.times { do spawn { // `each` is a method that accepts a closure with one argument. for [1, 2, 3].each |item| { // `fmt!` is a macro that statically verifies a format string. io::println(fmt!("%d", *item)); } } }}

Language evolution

In addition to conventional static typing, prior to version 0.4 Rust also supported typestates. The typestate system modeled assertions before and after program statements, through use of a special check statement. Discrepancies could be discovered at compile time, rather than once a program was running, as might be the case with assertions in C or C++ code. The typestate concept was not unique to Rust, as it was first introduced in the NIL programming language.[15] Typestates were removed because in practice they found little use, though the same functionality of them can still be achieved with branding patterns.[16]

The style of the object system changed considerably within versions 0.2, 0.3 and 0.4 of Rust. Version 0.2 introduced classes for the first time, with version 0.3 adding a number of features including destructors and polymorphism through the use of interfaces. In Rust 0.4, traits were added as a means to provide inheritance; interfaces were unified with traits and removed as a separate feature. Classes were also removed, replaced by a combination of implementations and structured types.

See also

References

  1. ^ a b Rust 0.5 Release Notes
  2. ^ "COPYRIGHT". Rust compiler source repository. https://github.com/mozilla/rust/blob/ master/COPYRIGHT. Retrieved 2012-12-17.
  3. ^ "The Rust Language". Lambda the Ultimate. 2010-07-08. http://lambda-the-ultimate.org/node/4 009. Retrieved 2010-10-30.
  4. ^ "The Rust Programming Language". http://www.rust-lang.org/. Retrieved 2012-10-21.
  5. ^ "Doc language FAQ". https://github.com/mozilla/rust/wiki/ Doc-language-FAQ. Retrieved 2012-10-21.
  6. ^ "Project FAQ". 2010-09-14. https://github.com/mozilla/rust/wiki/ Doc-project-FAQ. Retrieved 2012-01-11.
  7. ^ "Future Tense". 2011-04-29. http://www.slideshare.net/BrendanEich /future-tense-7782010. Retrieved 2012-02-06. "At Mozilla Summit 2010, we launched Rust, a new programming language motivated by safety and concurrency for parallel hardware, the “manycore” future which is upon us."
  8. ^ Hoare, Graydon (2010-10-02). "Rust Progress". http://blog.mozilla.com/graydon/2010/ 10/02/rust-progress/. Retrieved 2010-10-30.
  9. ^ Hoare, Graydon (2011-04-20). "[rust-dev] stage1/rustc builds". https://mail.mozilla.org/pipermail/ru st-dev/2011-April/000330.html. Retrieved 2011-04-20. "After that last change fixing the logging scope context bug, looks like stage1/rustc builds. Just shy of midnight :)"
  10. ^ catamorphism (2012-01-20). "Mozilla and the Rust community release Rust 0.1 (a strongly-typed systems programming language with a focus on memory safety and concurrency)". http://www.reddit.com/r/programming/c omments/opgxd/mozilla_and_the_rust_co mmunity_release_rust_01_a/. Retrieved 2012-02-06.
  11. ^ "The Mozilla Manifesto". http://www.mozilla.org/about/manifest o.en.html. Retrieved 2012-04-09.
  12. ^ Walton, Patrick (2010-12-05). "C++ Design Goals in the Context of Rust". http://pcwalton.blogspot.com/2010/12/ c-design-goals-in-context-of-rust.htm l. Retrieved 2011-01-21. "… It’s impossible to be “as fast as C” in all cases while remaining safe… C++ allows all sorts of low-level tricks, mostly involving circumventing the type system, that offer practically unlimited avenues for optimization. In practice, though, C++ programmers restrict themselves to a few tools for the vast majority of the code they write, including stack-allocated variables owned by one function and passed by alias, uniquely owned objects (often used with auto_ptr or the C++0x unique_ptr), and reference counting via shared_ptr or COM. One of the goals of Rust’s type system is to support these patterns exactly as C++ does, but to enforce their safe usage. In this way, the goal is to be competitive with the vast majority of idiomatic C++ in performance, while remaining memory-safe…"
  13. ^ "Doc language FAQ". 2010-09-14. https://github.com/mozilla/rust/wiki/ Doc-language-FAQ. Retrieved 2012-01-11.
  14. ^ Walton, Patrick (2010-10-01). "Rust Features I: Type Inference". http://pcwalton.blogspot.com/2010/10/ rust-features-i-type-inference.html. Retrieved 2011-01-21.
  15. ^ Strom, Robert E.; Yemini, Shaula (1986). Typestate: A Programming Language Concept for Enhancing Software Reliability. IEEE Transactions on Software Engineering. ISSN 0098-5589. http://www.cs.cmu.edu/~aldrich/papers /classic/tse12-typestate.pdf. Retrieved 2010-11-14.
  16. ^ "Typestate Is Dead, Long Live Typestate!". 2012-12-26. http://pcwalton.github.com/blog/2012/ 12/26/typestate-is-dead/. Retrieved 2012-12-28.

External links

(Sebelumnya) Russian languageS MIME (Berikutnya)