راست (زبان برنامه‌نویسی)

از ویکی‌پدیا، دانشنامهٔ آزاد
زبان برنامه‌نویسی راست (Rust)
پارادایم برنامه‌نویسیکامپایل‌شونده، همزمان، تابعی، دستوری، شی‌گرا، ساخت‌یافته
طراحی شده توسطگریدون هوئر
توسعه‌دهندهRust Project Developers
ظهوریافته در۲۰۱۰
انتشار پایدار
1.21[۱]
static, strong, inferred, structural
زبان پیاده‌سازی
Rust
سیستم‌عامللینوکس، اواس ده، ویندوز، فری‌بی‌اس‌دی
پروانهمجوز آپاچی یا پروانه ام‌آی‌تی[۲]
.rs
وبگاه
متأثر از
الف، سی#, سی++, Camlp4, لیسپ معمولی، Cyclone, ارلنگ، هسکل، Hermes, لیمبو، Napier, Napier88, Newsqueak, NIL, اکمل، رکت، روبی، Sather, استاندارد ام‌ال

راست (به انگلیسی: Rust) یک زبان برنامه‌نویسی ترجمه‌شده، عام منظوره و چند الگویی است که توسط تحقیقات موزیلا توسعه یافته‌است.[۳] این زبان طراحی شده‌است تا یک زبان مطمئن، همسو و عملی باشد[۴][۵] که مدل‌های تابعی خالص، امری-رویه‌ای و شی گرا را پشتیبانی می‌کند. زبان از یک پروژهٔ شخصی توسط توسعه‌دهندهٔ Graydon Hoare به‌وجود آمد، که او کار بر روی آن را در سال ۲۰۰۶ آغاز کرد؛ کارفرمای او موزیلا، در سال ۲۰۰۹ درگیر این پروژه شد[۶] و رسماً آن را برای اولین بار در سال ۲۰۱۰ پرده برداری کرد.[۷] در همان سال، کار از کامپایلرهای اولیه (نوشته شده در OCaml) خود را به کامپایلر خود میزبان نوشته‌شده در Rust تغییر داد.[۸] چیزی که به‌عنوان rustc شناخته شد، با موفقیت خود را در سال ۲۰۱۱ کامپایل کرد.[۹] کامپایلر خود میزبان از LLVM به عنوان backend استفاده می‌کند.

اولین نسخه از کامپایلر Rust، که آلفا نام گذاری شد در ژانویه ۲۰۱۲ منتشر شد.[۱۰] نسخه فعلی (نسخه ۰٫۸)، در سپتامبر ۲۰۱۳ منتشر شده‌است.[۱۱]

Rust به‌طور کامل در بازخورد باز و همکاری و کمک از جامعه توسعه یافته‌است. طراحی این زبان از طریق تجربه حاصل از نوشتن موتور چیدمان Servo اصلاح شده و Rust خودش را کامپایل کرده‌است.[۱۲] اگر چه توسعه آن با حمایت مالی موزیلا و سامسونگ بوده‌است، اما یک پروژهٔ اجتماعی است. بخش بزرگی از تصدیقات نسخهٔ فعلی از اعضای جامعه می‌باشد.[۱۳]

توصیف[ویرایش]

هدف Rust این است که یک زبان خوب برای ایجاد مجموعهٔ بزرگی از مشتری‌ها و سرور برنامه‌هایی که بر روی اینترنت اجرا می‌شوند باشد.[۱۴]این امر زبان را به یکی از ویژگی‌های مجموعه با تأکید بر ایمنی، کنترل از طرح حافظه و همزمانی هدایت می‌کند. اگر عملکرد زبان تنها عامل مورد توجه باشد، عملکرد قابل اطمینان بودن کد همان‌طور که انتظار می‌رود آن را کندتر از ++C می‌کند اما پیشگیری‌های از پیش تعریف شده در Rust با احتساب اقدامات محتاطانه‌ای که زبان ++C به‌طور دستی انجام می‌دهد، از نظر سرعت برابری می‌کند (و یا حتی سریعتر است)[۱۵]
نحو زبان Rust مشابه C و ++C است؛ با بلاک‌هایی از کد که بین آکولادها محدود شده‌است و کلمات کلیدی کنترل جریان مانند while ,do ,else ,if و for. البته تمام کلمات کلیدی C و ++C وجود ندارد اما به هر حال دیگران (مانند تطابق واژه کلیدی برای انشعاب چند جهت) برای برنامه‌نویسانی که از این زبان‌ها می‌آیند کمتر آشنا خواهد بود. با وجود شباهت نحوی، Rust از نظر معنایی با C و ++C متفاوت است.

پروژه‌هایی که از راست استفاده می‌کنند[ویرایش]

پروژه‌های توسعه یافته در Rust شامل:

مبتنی بر مرورگر:

مبتنی بر ابزار ساخت:

  • Cargo - سیستم ساخت خودکار Rust
  • Habitat - یک ابزار ساخت و استقرار از شرکت Chef Software (وب سایت رسمی)

پروژه‌های دیگر:

  • Magic Pocket - سیستم فایل Dropbox که قدرت ماشین‌های حافظه[۲۰]Diskotech petabyte را افزایش می‌دهد
  • اوپن‌دی‌ان‌اس - بکارگیری زبان Rust در دو مؤلفه[۲۱]OpenDNS
  • Redox OS - یک سیستم عامل ریزهسته[۲۲]
  • Piston - یک موتور بازی (وب سایت رسمی)
  • OnePush - یک سیستم اعلان رسانی توسعه یافته توسط[۲۳]OneSignal
  • REmacs - یک پورت از Emacs به[۲۴]Rust
  • MAIDsafe - پروژه اینترنتی P2P که در حال حاضر توسط تیم مستقر در Troon, Scotland توسعه یافته
  • Lucidscape Mesh - یک موتور شبیه‌سازی توزیع شده بلادرنگ برای واقعیت مجازی
  • Tor - یک شبکه ناشناس[۲۵]

نمونه‌ها[ویرایش]

نمونه کد زیر برای Rust نسخه ۰٫۸ معتبر می‌باشد. نحو و معناشناسی ممکن است در نسخه‌های بعدی تغییر یابد.

برنامه Hello World:

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

دو نمونه از تابع فاکتوریل در ساختارهای بازگشتی و ترتیبی :

/* 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: i32) -> i32 {
    if n <= 1 { 1 }
    else { n * recursive_factorial(n - 1) }
}

fn iterative_factorial(n: i32) -> i32 {
    // Variables are declared with `let`.
    // The `mut` keyword allows these variables to be mutated.
    let mut i = 1;
    let mut result = 1;
    while i <= n {
        result *= i;
        i += 1;
    }
    return result; // An explicit return, in contrast to the prior function.
}

fn main() {
    println!("Recursive result: {:?}", recursive_factorial(10));
    println!("Iterative result: {:?}", iterative_factorial(10));
}

یک نمایش ساده از قابلیت همزمانی ساده از Rust:

/* This function creates ten "tasks" that all execute concurrently. Run it
   several times and observe the irregular output that results as each task
   races on stdout, due to the fact that each task may yield both between
   successive calls to `println` and within the `println` function itself. */
fn main() {
    // This string is immutable, so it can safely be accessed from multiple tasks.
    let message = "Look at me, I'm in a lightweight thread!";
    // `for` loops work with any type that implements the `Iterator` trait.
    for num in range(0, 10) {
        do spawn {
            println(message);
            // `println!` is a macro that statically verifies a format string.
            // Macros are structural (as in Scheme) rather than textual (as in C).
            println!("This message brought to you by task {:?}.", num);
        }
    }
}

یک نمایش ساده از اشاره‌گرهای هوشمند یگانی همراه با tagged unions و روش‌ها:

use IntList::{Node, Empty};

// This program defines a recursive data structure and implements methods upon it.
// Recursive data structures require a layer of indirection, which is provided here
// by a unique pointer, constructed via the `Box::new` constructor. These are
// analogous to the C++ library type `std::unique_ptr`, though with more static
// safety guarantees.
fn main() {
    let list = IntList::new().prepend(3).prepend(2).prepend(1);
    println!("Sum of all values in the list: {}.", list.sum());
    println!("Sum of all doubled values in the list: {}.", list.multiply_by(2).sum());
}

// `enum` defines a tagged union that may be one of several different kinds of values at runtime.
// The type here will either contain no value, or a value and a pointer to another `IntList`.
enum IntList {
    Node(i32, Box<IntList>),
    Empty
}

// An `impl` block allows methods to be defined on a type.
impl IntList {
    fn new() -> Box<IntList> {
        Box::new(Empty)
    }

    fn prepend(self, value: i32) -> Box<IntList> {
        Box::new(Node(value, Box::new(self)))
    }

    fn sum(&self) -> i32 {
        // `match` expressions are the typical way of employing pattern-matching,
        // and are somewhat analogous to the `switch` statement from C and C++.
        match *self {
            Node(value, ref next) => value + next.sum(),
            Empty => 0
        }
    }

    fn multiply_by(&self, n: i32) -> Box<IntList> {
        match *self {
            Node(value, ref next) => Box::new(Node(value * n, next.multiply_by(n))),
            Empty => Box::new(Empty)
        }
    }
}

بدافزارها[ویرایش]

در مواردی، نویسندگان بدافزار نیز از زبان برنامه‌نویسی راست استفاده کرده‌اند. برای مثال، باج‌افزار BlackCat به زبان راست برنامه‌نویسی شده‌است.[۲۶]

منابع[ویرایش]

  1. "Announcing Rust 1.21". blog.rust-lang.org. 2017-10-12. Retrieved 2017-10-13.
  2. "COPYRIGHT". Rust compiler source repository. Retrieved 2012-12-17.
  3. "The Rust Language". Lambda the Ultimate. 2010-07-08. Retrieved 2010-10-30. {{cite web}}: |first= missing |last= (help)
  4. "The Rust Programming Language". Retrieved 2012-10-21.
  5. "Doc language FAQ". Retrieved 2012-10-21.
  6. "Project FAQ". 2010-09-14. Retrieved 2012-01-11.
  7. "Future Tense". 2011-04-29. 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". Archived from the original on 22 October 2013. Retrieved 2010-10-30.
  9. Hoare, Graydon (2011-04-20). "[rust-dev] stage1/rustc builds". Archived from the original on 20 July 2011. 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)". Retrieved 2012-02-06.
  11. خطای یادکرد: خطای یادکرد:برچسب <ref>‎ غیرمجاز؛ متنی برای یادکردهای با نام release وارد نشده است. (صفحهٔ راهنما را مطالعه کنید.).
  12. Peter Bright (2013-04-03). "Samsung teams up with Mozilla to build browser engine for multicore machines". Retrieved 2013-04-04.
  13. "Rust Contributors".
  14. Avram, Abel (2012-08-03). "Interview on Rust, a Systems Programming Language Developed by Mozilla". InfoQ. Retrieved 2013-08-17. GH: A lot of obvious good ideas, known and loved in other languages, haven't made it into widely-used systems languages... There were a lot of good competitors in the late 70s and early 80s in that space, and I wanted to revive some of their ideas and give them another go, on the theory that circumstances have changed: the internet is highly concurrent and highly security-conscious, so the design-tradeoffs that always favor C and C++ (for example) have been shifting.
  15. Walton, Patrick (2010-12-05). "C++ Design Goals in the Context of Rust". 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…
  16. «Shipping Rust in Firefox» (به انگلیسی). دریافت‌شده در ۲۰۱۷-۰۶-۲۱.
  17. «Mozilla's Rust-based Servo browser engine inches forward».
  18. Lardinois، Frederic. «Mozilla And Samsung Collaborate On Servo, Mozilla's Next-Gen Browser Engine». دریافت‌شده در ۲۰۱۷-۰۶-۲۱.
  19. Bryant، David (۲۰۱۶-۱۰-۲۷). «A Quantum Leap for the Web». دریافت‌شده در ۲۰۱۷-۰۶-۲۱.
  20. «The Epic Story of Dropbox's Exodus From the Amazon Cloud Empire | WIRED». دریافت‌شده در ۲۰۱۷-۰۶-۲۱.
  21. Balbaert, Ivo (2015-05-27). Rust Essentials (به انگلیسی). Packt Publishing Ltd.
  22. «Rust's Redox OS could show Linux a few new tricks».
  23. «OneSignal now sends iOS push notifications 100x faster».
  24. "Remacs: Re-Implementing Emacs In Rust - Phoronix" (به انگلیسی). Retrieved 2017-06-21.
  25. Hahn، Sebastian (Fri Mar 31 21:23:27 UTC 2017). «[tor-dev] Tor in a safer language: Network team update from Amsterdam». دریافت‌شده در 2017-06-21. تاریخ وارد شده در |تاریخ= را بررسی کنید (کمک)
  26. «ALPHV؛ گروه باج‌افزاری جدید اما جهان‌دیده». رامونا پردازش نگار. ۲۰۲۲-۰۴-۰۸. دریافت‌شده در ۲۰۲۲-۰۴-۰۹.