by Shane Miller and Carl Lerche | on 11 FEB 2022 | in Developer Tools, DevOps, Open Source, Sustainability |  Permalink

Sustainability with Rust

الاستدامة مع "راّست"

Rust is a programming language implemented as a set of open source projects. It combines the performance and resource efficiency of systems programming languages like C with the memory safety of languages like Java. Rust started in 2006 as a personal project of Graydon Hoare before becoming a research project at Mozilla in 2010. Rust 1.0 launched in 2015, and in 2020, support for Rust moved from Mozilla to the Rust Foundation, a non-profit organization created as a partnership between Amazon Web Services, Inc (AWS), Google, Huawei, Microsoft, and Mozilla. The Foundation’s mission is to support the growth and innovation of Rust, and the member companies have grown from the founding 5 to 27 companies in the first year.

( راست) هي لغة برمجة يتم تنفيذها كمجموعة من المشاريع مفتوحة المصدر. فيها تجمع بين الأداء والاقتصاد في الموارد للغات برمجة الأنظمة مثل (سي) مع أمان الذاكرة للغات مثل (جافا) . بدأ ( راست) في عام 2006 كمشروع شخصي لـ (جريدون هرون) قبل أن يصبح مشروعًا بحثيًا في (موزيلا) في عام 2010. تم إطلاق ( راست) 1.0 في عام 2015 ، وفي عام 2020 ، انتقل دعم (راست) من (موزيلا) إلى (مؤسسة راست) ، وهي منظمة غير ربحية تم إنشاؤها كمنظمة شراكة بين (امازون لخدمات الويب)، و (جوجل) و (هواوي) و (مايكروسوفت) و (موزيلا). تتمثل مهمة المؤسسة في دعم نمو وابتكار شركة (راست) ، وقد نمت الشركات الأعضاء من 5 إلى 27 شركة في العام الأول.

At AWS, Rust has quickly become critical to building infrastructure at scale. Firecracker is an open source virtualization technology that powers AWS Lambda and other serverless offerings. It launched publicly in 2018 as our first notable product implemented in Rust. We use Rust to deliver services such as Amazon Simple Storage Service (Amazon S3), Amazon Elastic Compute Cloud (Amazon EC2), Amazon CloudFront, and more. In 2020, we launched Bottlerocket, a Linux-based container operating system written in Rust, and our Amazon EC2 team uses Rust as the language of choice for new AWS Nitro System components, including sensitive applications, such as Nitro Enclaves.

في (اوس) ، أصبح (راست) سريعًا أمرًا بالغ الأهمية لبناء البنية التحتية على نطاق واسع. (فايركراكر) هي تقنية افتراضية مفتوحة المصدر تعمل على تشغيل (لامبدا) والعروض الأخرى بدون خادم. تم إطلاقه علنًا في عام 2018 كأول منتج بارز يتم تنفيذه في (راست). نحن نستخدم (راست) لتقديم خدمات مثل (اوس س3) و (أوس اي سي 2) و (كلاود فورنت) والمزيد. في عام 2020 ، أطلقنا (بوتيل روكيت) ، وهو نظام تشغيل حاويات قائم على (لينكس) مكتوب بلغة (راست) ، ويستخدم فريق و (أوس اي سي 2) لغة (راست) كلغة مفضلة لمكونات(نظام اوس نيترو)الجديدة ، بما في ذلك التطبيقات الحساسة ، مثل (نيترو انكيلاف).

At AWS, we believe leaders create more than they consume and always leave things better than they found them. In 2019, AWS was proud to become a sponsor of the Rust project. In 2020, we started hiring Rust maintainers and contributors, and we partnered with Google, Huawei, Microsoft, and Mozilla to create the Rust Foundation with a mission to support Rust. AWS is investing in the sustainability of Rust, a language we believe should be used to build sustainable and secure solutions.

في (أوس) ، نعتقد أن القادة يصنعون أكثر مما يستهلكون ويتركون دائمًا الأشياء أفضل مما وجدوه. في عام 2019 ، كانت (أوس) فخورة بأن تصبح راعيًا لمشروع (راست). في عام 2020 ، بدأنا في تعيين مشرفين ومساهمين في (راست) ، وتعاوننا مع (جوجل) و (هواوي) و (مايكروسوفت) و (موزيلا) لإنشاء(منظمة راست) بمهمة دعم (راست). تستثمر (أوس) في استدامة (راست) ، وهي لغة نعتقد أنه يجب استخدامها لبناء حلول مستدامة وآمنة.

Energy Efficiency in the Cloud

كفاءة الطاقة في السحابة

Worldwide, data centers consume about 200 terawatt hours per year. That’s roughly 1% of all energy consumed on our planet. There are a couple of really interesting things about the details of that energy use. If you look at the graph of energy consumption, the top line is basically flat going back as far as 2010. That’s incredibly counter-intuitive give the tremendous growth of big data, machine learning, and edge devices our industry has experienced over that same period of time.

في جميع أنحاء العالم ، تستهلك مراكز البيانات حوالي 200 تيراواط ساعة سنويًا. هذا ما يقرب من 1٪ من إجمالي الطاقة المستهلكة على كوكبنا. هناك بعض الأشياء المثيرة للاهتمام حقًا حول تفاصيل استخدام الطاقة. إذا نظرت إلى الرسم البياني لاستهلاك الطاقة ، ستجد أن السطر العلوي مسطح بشكل أساسي منذ عام 2010. وهذا غير بديهي بشكل لا يصدق يعطي النمو الهائل للبيانات الضخمة ، والتعلم الآلي ، والأجهزة المتطورة التي شهدتها صناعتنا خلال نفس الفترة من الوقت.

The second interesting detail is that while the top line of the graph is flat, inside the graph, the distribution over traditional, cloud, and hyperscale data centers has changed dramatically in the same period. Those cloud and hyperscale data centers have been implementing huge energy efficiency improvements, and the migration to that cloud infrastructure has been keeping the total energy use of data centers in balance despite massive growth in storage and compute for more than a decade.

التفصيل الثاني المثير للاهتمام هو أنه بينما يكون السطر العلوي من الرسم البياني مسطحًا ، داخل الرسم البياني ، تغير التوزيع على مراكز البيانات التقليدية والسحابة وفائقة التدرج بشكل كبير في نفس الفترة. كانت مراكز البيانات السحابية وفائقة النطاق تنفذ تحسينات هائلة في كفاءة الطاقة ، وقد أدى الانتقال إلى تلك البنية التحتية السحابية إلى الحفاظ على إجمالي استخدام الطاقة لمراكز البيانات في حالة توازن على الرغم من النمو الهائل في التخزين والحساب لأكثر من عقد من الزمان.

There have been too many data center efficiency improvements to list, but here are a few examples. In compute, we’ve made efficiency improvements in hardware and implemented smarter utilization of resources to reduce idle time. We’ve slowed the growth of our servers with support for multi-instance and multi-tenant, and we’ve improved drive density and efficiency for storage. We’ve also adopted more energy efficient building materials and cooling systems.

كان هناك عدد كبير جدًا من تحسينات كفاءة مركز البيانات في القائمة ، ولكن إليك بعض الأمثلة. في مجال الحوسبة ، أجرينا تحسينات على الكفاءة في الأجهزة ونفذنا استخدامًا أكثر ذكاءً للموارد لتقليل وقت الخمول. لقد عملنا على إبطاء نمو خوادمنا من خلال دعم المثيلات المتعددة والمستأجرين المتعددين ، وقمنا بتحسين كثافة محرك الأقراص وكفاءته للتخزين. كما اعتمدنا أيضًا مواد بناء وأنظمة تبريد أكثر كفاءة في استخدام الطاقة.

As incredible as that success story is, there are two questions it raises. First, is the status quo good enough? Is keeping data center energy use to 1% of worldwide energy consumption adequate? The second question is whether innovations in energy efficiency will continue to keep pace with growth in storage and compute in the future? Given the explosion we know is coming in autonomous drones, delivery robots, and vehicles, and the incredible amount of data consumption, processing, and machine learning training and inference required to support those technologies, it seems unlikely that energy efficiency innovations will be able to keep pace with demand.

على الرغم من أن قصة النجاح هذه لا تصدق ، إلا أن هناك سؤالين يثيران ذلك. أولا ، هل الوضع الراهن جيد بما فيه الكفاية؟ هل الحفاظ على استخدام الطاقة في مركز البيانات بنسبة 1٪ من استهلاك الطاقة في جميع أنحاء العالم كافٍ؟ السؤال الثاني هو ما إذا كانت الابتكارات في كفاءة الطاقة ستستمر في مواكبة النمو في التخزين والحساب في المستقبل؟ نظرًا للانفجار الذي نعلم أنه قادم في الطائرات بدون طيار وروبوتات التوصيل والمركبات والكمية الهائلة من استهلاك البيانات والمعالجة والتدريب على التعلم الآلي والاستدلال المطلوب لدعم هذه التقنيات ، يبدو من غير المحتمل أن ابتكارات كفاءة الطاقة ستكون قادرة على ذلك مواكبة الطلب.

The energy efficiency improvements we’ve talked about so far have been the responsibility of AWS, but just like security, sustainability is a shared responsibility. AWS customers are responsible for energy efficient choices in storage policies, software design, and compute utilization, while AWS owns efficiencies in hardware, utilization features, and cooling systems. We are also making huge investments in renewable energy.

لقد كانت تحسينات كفاءة الطاقة التي تحدثنا عنها حتى الآن من مسؤولية (أوس) ، ولكن تمامًا مثل الأمان ، تعد الاستدامة مسؤولية مشتركة. يتحمل عملاء (أوس)مسؤولية خيارات كفاءة الطاقة في سياسات التخزين وتصميم البرامج واستخدام الحوسبة ، بينما تمتلك (أوس)الكفاءات في الأجهزة وميزات الاستخدام وأنظمة التبريد. كما أننا نقوم باستثمارات ضخمة في مجال الطاقة المتجددة.

AWS is on a path to have 100% of our data centers powered with renewable energy by 2025, but even renewables have an environmental impact. It will take about half a million acres of solar panels to generate the 200 terawatt hours of energy used by data centers today. The mining, manufacturing, and management of that many solar panels has substantial environmental impact. So, while we’re really proud of our success with renewable energy, as Peter DeSantis, SVP, AWS said at re:Invent 2020, “The greenest energy is the energy we don’t use.”

(أوس) في طريقها لتزويد 100٪ من مراكز البيانات لدينا بالطاقة المتجددة بحلول عام 2025 ، ولكن حتى مصادر الطاقة المتجددة لها تأثير بيئي. سيستغرق الأمر حوالي نصف مليون فدان من الألواح الشمسية لتوليد 200 تيراواط ساعة من الطاقة التي تستخدمها مراكز البيانات اليوم. إن استخراج العديد من الألواح الشمسية وتصنيعها وإدارتها له تأثير بيئي كبير. لذلك ، في حين أننا فخورون حقًا بنجاحنا في مجال الطاقة المتجددة ، كما قال Peter DeSantis ، نائب الرئيس الأول ، (أوس) في re: Invent 2020 ، "الطاقة الأكثر خضرة هي الطاقة التي لا نستخدمها."

Renewables should not replace energy efficiency as a design principle. In the same way that operational excellence, security, and reliability have been principles of traditional software design, sustainability must be a principle in modern software design. That’s why AWS announced a sixth pillar for sustainability to the AWS Well-Architected Framework.

لا ينبغي أن تحل مصادر الطاقة المتجددة محل كفاءة الطاقة كمبدأ تصميم. بنفس الطريقة التي كان بها التميز التشغيلي والأمان والموثوقية مبادئ تصميم البرامج التقليدية ، يجب أن تكون الاستدامة مبدأً في تصميم البرامج الحديثة. لهذا السبب أعلنت (أوس) عن الدعامة السادسة للاستدامة في (أوس) عن اطار العمل حسن الهندسة

What that looks like in practice is choices like relaxing SLAs for non-critical functions and prioritizing resource use efficiency. We can take advantage of virtualization and allow for longer device upgrade cycles. We can leverage caching and longer TTLs whenever possible. We can classify our data and implement automated lifecycle policies that delete data as soon as possible. When we choose algorithms for cryptography and compression, we can include efficiency in our decision criteria. Last, but not least, we can choose to implement our software in energy efficient programming languages.

ما يبدو في الممارسة العملية هو اختيارات مثل استرخاء اتفاقيات مستوى الخدمة للوظائف غير الحرجة وتحديد أولويات كفاءة استخدام الموارد. يمكننا الاستفادة من المحاكاة الافتراضية والسماح بدورات ترقية أطول للجهاز. يمكننا الاستفادة من التخزين المؤقت و TTL الأطول كلما أمكن ذلك. يمكننا تصنيف بياناتنا وتنفيذ سياسات دورة الحياة المؤتمتة التي تحذف البيانات في أسرع وقت ممكن. عندما نختار خوارزميات للتشفير والضغط ، يمكننا تضمين الكفاءة في معايير القرار الخاصة بنا. أخيرًا وليس آخرًا ، يمكننا اختيار تنفيذ برنامجنا في لغات البرمجة الموفرة للطاقة.

Energy Efficient Program Languages

لغات البرنامج الموفرة للطاقة

There was a really interesting study a few years ago that looked at the correlation between energy consumption, performance, and memory use. This is a really common conversation in sustainability. Given how little visibility we have into energy or carbon use by our services, is there a metric that can serve as a proxy? Can I look at my existing service dashboards with infrastructure costs, performance, memory, etc and use the trends I see to infer something about the trends in my service’s energy consumption?

كانت هناك دراسة مثيرة للاهتمام حقًا قبل بضع سنوات نظرت في العلاقة بين استهلاك الطاقة والأداء واستخدام الذاكرة. هذه محادثة شائعة حقًا في الاستدامة. بالنظر إلى ضآلة رؤيتنا للطاقة أو استخدام الكربون من خلال خدماتنا ، هل هناك مقياس يمكن أن يعمل كبديل؟ هل يمكنني إلقاء نظرة على لوحات معلومات الخدمة الحالية مع تكاليف البنية التحتية ، والأداء ، والذاكرة ، وما إلى ذلك ، واستخدام الاتجاهات التي أراها لاستنتاج شيء ما عن الاتجاهات في استهلاك الطاقة في خدمتي؟

What the study did is implement 10 benchmark problems in 27 different programming languages and measure execution time, energy consumption, and peak memory use. C and Rust significantly outperformed other languages in energy efficiency. In fact, they were roughly 50% more efficient than Java and 98% more efficient than Python.

ما فعلته الدراسة هو تنفيذ 10 مشاكل معيارية في 27 لغة برمجة مختلفة وقياس وقت التنفيذ واستهلاك الطاقة وأقصى استخدام للذاكرة. تفوقت (سي) و (راست) بشكل ملحوظ على اللغات الأخرى في كفاءة الطاقة. في الواقع ، كانت أكثر كفاءة بنسبة 50٪ تقريبًا من (جافا) و 98٪ أكثر كفاءة من (بايثون).

It’s not a surprise that C and Rust are more efficient than other languages. What is shocking is the magnitude of the difference. Broad adoption of C and Rust could reduce energy consumption of compute by 50% – even with a conservative estimate.

ليس من المستغرب أن تكون (سي) و (راست) أكثر كفاءة من اللغات الأخرى. الصادم هو حجم الاختلاف. يمكن أن يؤدي اعتماد (سي) و (راست) على نطاق واسع إلى تقليل استهلاك الطاقة للحساب بنسبة 50 ٪ - حتى مع تقدير متحفظ.

So the question is why not use more C? The language and developer tools are extremely mature, and the size of the developer community is much bigger than Rust. During his keynote at Open Source Summit in 2021, Linus Torvalds, the creator of Linux, acknowledged that implementing code in C can be like juggling chainsaws. As a lifelong C programmer, Torvalds knows that, “[C’s subtle type interactions] are not always logical [and] are pitfalls for pretty much anybody.”

لذا فإن السؤال هو لماذا لا تستخدم أكثر من (سي)؟ أدوات المطور واللغة ناضجة للغاية ، وحجم مجتمع المطورين أكبر بكثير من (راست). خلال الكلمة الرئيسية التي ألقاها في ملتقى البرمجيات المفتوحة في عام 2021 ، أقر (لاينوس ترافولد) ، مبتكر نظام (لينكس) ، أن تنفيذ الكود في لغة (سي) يمكن أن يكون مثل اللعب بالمناشير. بصفته مبرمجًا بللغة (سي) مدى الحياة ، يعرف (ترافولد) أن "[التفاعلات الدقيقة من النوع C] ليست دائمًا منطقية [و] مزالق لأي شخص تقريبًا."

Torvalds called Rust the first language he’s seen that might actually be a solution. Rust delivers the energy efficiency of C without the risk of undefined behavior. We can cut energy use in half without losing the benefits of memory safety.

أطلق (ترافولد) على (راست) اللغة الأولى التي رآها والتي قد تكون حلاً في الواقع. توفر (راست) كفاءة الطاقة لـ (سي) دون التعرض لخطر السلوك غير المحدد. يمكننا خفض استخدام الطاقة إلى النصف دون فقدان مزايا أمان الذاكرة.

Several analyses have concluded that more than 70% of the high severity CVEs that occur in C/C++ would be prevented by implementing those same solutions in Rust. In fact, the Internet Security Research Group (ISRG), the nonprofit that supports the Let’s Encrypt project, the Certificate Authority for 260 million websites, has a goal to move all internet security sensitive infrastructure to memory safe languages. The projects underway include support for Rust in the Linux kernel and migrating curl to Rust implementations of TLS and HTTP.

خلصت العديد من التحليلات إلى أن أكثر من 70٪ من حالات التطرف العنيف الشديدة الخطورة التي تحدث في (سي \ سي++) سيتم منعها من خلال تنفيذ نفس الحلول في (راست). في الواقع ، تهدف مجموعة أبحاث أمان الإنترنت (ISRG) ، وهي منظمة غير ربحية تدعم مشروع Let’s Encrypt ، المرجع المصدق لـ 260 مليون موقع ويب ، إلى نقل جميع البنية التحتية الحساسة لأمن الإنترنت إلى لغات آمنة للذاكرة. تشمل المشاريع الجارية دعم (راست) في (كيرنل راست) وترحيل (كيرل) إلى (راست) لتطبيقات TLS و HTTP.

Looking again at that study about correlation, we have measurements for more than just energy consumption. The middle column shows the results for execution time, and the times for Rust and C are really similar. Both languages are executing faster than other languages. That means, when you choose to implement your software in Rust for the sustainability and security benefits, you also get the optimized performance of C.

بالنظر مرة أخرى إلى تلك الدراسة حول الارتباط ، لدينا قياسات لأكثر من مجرد استهلاك الطاقة. يعرض العمود الأوسط نتائج وقت التنفيذ ، وأوقات (راست) و (سي) متشابهة حقًا. يتم تنفيذ اللغتين بشكل أسرع من اللغات الأخرى. هذا يعني ، عندما تختار تنفيذ برنامجك في (راست) من أجل مزايا الاستدامة والأمان ، فإنك تحصل أيضًا على الأداء الأمثل لـ (سي).

Rust Customer Success Stories

قصص نجاح العملاء (رست)

Tenable is a cyber security solutions provider focused on exposure visibility tools, and they had a sidecar agent that filtered out unnecessary metrics. It was written in JavaScript and had been working in production for a few months when the performance started to degrade due to scaling. Tenable decided to rewrite the filter in a more efficient language, and they chose Rust for its performance and safety. The result was about a 50% improvement in latency at both the median and the P95.

(تينآبل) هو مزود حلول للأمن السيبراني يركز على أدوات رؤية التعرض ، وكان لديهم وكيل جانبي يقوم بتصفية المقاييس غير الضرورية. تمت كتابته بلغة (جافاسكربت) وكان يعمل في الإنتاج لبضعة أشهر عندما بدأ الأداء في التدهور بسبب التوسع. قرر (تينآبل) إعادة كتابة الفلتر بلغة أكثر فاعلية ، واختاروا (راست) لأدائه وسلامته. كانت النتيجة تحسنًا بنسبة 50٪ تقريبًا في زمن الانتقال في كل من الوسيط و P95.

50% performance improvements are great, but here are some other graphs from that migration. Tenable also saw a 75% reduction in CPU usage and a 95% reduction in memory usage. That is substantial savings, and that’s not just dollars saved – that’s energy saved. These are the graphs of an energy efficient, sustainable implementation.

تُعد تحسينات الأداء بنسبة 50٪ رائعة ، ولكن إليك بعض الرسوم البيانية الأخرى من هذا الترحيل. شهد (تينآبل) أيضًا انخفاضًا بنسبة 75٪ في استخدام وحدة المعالجة المركزية و 95٪ في استخدام الذاكرة. هذا هو توفير كبير ، وهذا ليس مجرد توفير الدولارات - هذا هو توفير الطاقة. هذه هي الرسوم البيانية للتنفيذ الفعال والمستدام للطاقة.

Rust is being used today to ship real world production software, but developers aren’t choosing Rust to reduce carbon emissions. When we ask Rust developers why they started using Rust, by far the most common answer is some variant of runtime performance, whether it is because Rust is faster or because Rust has more reliable tail latencies. It’s almost always about performance.

يتم استخدام (راست) اليوم لشحن برامج إنتاج في العالم الحقيقي ، لكن المطورين لا يختارون (راست) لتقليل انبعاثات الكربون. عندما نسأل مطوري (راست) عن سبب بدء استخدام (راست) ، فإن الإجابة الأكثر شيوعًا هي نوع من أداء وقت التشغيل ، سواء كان ذلك لأن (راست) أسرع أو لأن (راست) لديه زمن انتقال أكثر موثوقية. دائمًا ما يتعلق الأمر بالأداء.

Discord started as a mostly Python, Go, and Elixir shop, and they had a problem with one of their key Go services. It was a pretty simple service, but it had slow tail latencies. Because Go is a garbage collection (GC) language, as objects are created and released, every so often, the garbage collector needs to stop execution of the program and run a garbage collection pass. While the GC is running, the process is unable to respond to requests, and you can see the spikes on the CPU and response time graphs when it’s running.

بدأ (ديسكورد) كمتجر (بايثون) و (جو) و (اليكسير) في الغالب ، وكان لديهم مشكلة في إحدى خدمات (جو) الرئيسية الخاصة بهم. لقد كانت خدمة بسيطة جدًا ، ولكن كان لها زمن انتقال بطيء. نظرًا لأن (جو) عبارة عن لغة لجمع البيانات المهملة (جي سي) ، حيث يتم إنشاء الكائنات وإصدارها ، في كثير من الأحيان ، يحتاج مجمع القمامة إلى إيقاف تنفيذ البرنامج وتشغيل تصريح تجميع البيانات المهملة. أثناء تشغيل (جي سي) ، لا تتمكن العملية من الاستجابة للطلبات ، ويمكنك رؤية الارتفاع المفاجئ في الرسوم البيانية لوحدة المعالجة المركزية ووقت الاستجابة عند تشغيلها.

To fix the issue, Discord decided to try rewriting the service in Rust, and these are the results. The Go implementation is on the left and the Rust implementation is on the right. While the GC spike pattern is gone on the Rust graph, the really amazing difference is the magnitude of the change. The Go and Rust graphs are actually using different units.

لإصلاح المشكلة ، قرر (ديسكورد) محاولة إعادة كتابة الخدمة في (راست) ، وهذه هي النتائج. يوجد تطبيق (جو) على اليسار وتطبيق (راست) على اليمين. في حين أن نمط ارتفاع (جي سي) على الرسم البياني (راست) ، فإن الاختلاف المذهل حقًا هو حجم التغيير. تستخدم الرسوم البيانية (جو) و (راست) بالفعل وحدات مختلفة.

The Rust version is more than 10 times faster over all with the worst tail latencies reduced 100 times. These are incredible improvements, and because the server is able to respond to requests far more efficiently, fewer servers are needed, which means that less energy is used. While Discord didn’t decide to start using Rust to reduce energy consumption, that’s the impact.

يعد إصدار Rust أسرع بأكثر من 10 مرات على الإطلاق مع تقليل زمن الوصول إلى الذيل الأسوأ 100 مرة. هذه تحسينات لا تصدق ، ولأن الخادم قادر على الاستجابة للطلبات بشكل أكثر كفاءة بكثير ، هناك حاجة إلى عدد أقل من الخوادم ، مما يعني أنه يتم استخدام طاقة أقل. بينما لم يقرر Discord البدء في استخدام Rust لتقليل استهلاك الطاقة ، هذا هو التأثير.

Again, Rust isn’t the first efficient language. C has been around for a long time, but Rust is the first mainstream programming language that is efficient without sacrificing safety. 70% of all high severity security vulnerabilities written with C and C++ are due to memory unsafety, and Rust gives you efficiency without feeling like you’re playing with fire.

مرة أخرى ، (راست) ليست أول لغة فعالة. كانت لغة (سي) موجودة منذ فترة طويلة ، لكن (راست) هي أول لغة برمجة سائدة تتسم بالكفاءة دون التضحية بالسلامة. 70٪ من جميع الثغرات الأمنية عالية الخطورة المكتوبة بـ (سي) و (سي ++) ناتجة عن عدم أمان الذاكرة ، ويمنحك (راست) الكفاءة دون الشعور بأنك تلعب بالنار.

Revealing the Rust Secret Sauce

الكشف عن صلصة (راست) السرية

Most languages achieve memory safety by automatically managing memory at runtime with a garbage collector. Garbage collectors track outstanding references to a piece of memory and when all references go out of scope, the associated memory can be freed.

تحقق معظم اللغات أمان الذاكرة عن طريق إدارة الذاكرة تلقائيًا في وقت التشغيل باستخدام أداة تجميع البيانات المهملة. يتتبع جامعو البيانات المهملة الإشارات البارزة لقطعة من الذاكرة وعندما تخرج جميع المراجع عن النطاق ، يمكن تحرير الذاكرة المرتبطة.

Instead of using a garbage collector to maintain safety, Rust uses ownership and borrow checking. Ownership is fairly simple but has deep implications for the rest of the Rust programming language. In Rust, all memory is owned by a single variable. That variable is called its owner. There can be only one owner at a time, but ownership of the data can be passed around.

بدلاً من استخدام جامع القمامة للحفاظ على السلامة ، يستخدم (راست) التحقق من الملكية والاقتراض. الملكية بسيطة إلى حد ما ولكن لها آثار عميقة على بقية لغة برمجة (راست). في (راست) ، كل الذاكرة مملوكة لمتغير واحد. هذا المتغير يسمى مالكه. يمكن أن يكون هناك مالك واحد فقط في كل مرة ، ولكن يمكن نقل ملكية البيانات.

First, here is an example of message passing with Go. On the left side, we create a gift, then send it via the channel. On some other go routine on the right side, the gift is received and opened. The Go’s garbage collector is going to manage the memory for us. However, in the code on the left side, we accidentally opened the gift after sending it into the channel. The gift is going to be opened twice, resulting in a bug.

أولاً ، إليك مثال على تمرير الرسالة باستخدام (جو). على الجانب الأيسر ، نصنع هدية ، ثم نرسلها عبر القناة. في بعض الحالات الأخرى ، يتم بشكل روتيني على الجانب الأيمن ، يتم استلام الهدية وفتحها. جامع القمامة (جو) سيدير الذاكرة لنا. ومع ذلك ، في الرمز الموجود على الجانب الأيسر ، فتحنا الهدية عن طريق الخطأ بعد إرسالها إلى القناة. سيتم فتح الهدية مرتين ، مما يؤدي إلى حدوث خطأ.

Here is the same message passing example with Rust. The gift is created and assigned. We say that the `gift` variable owns the data. Ownership of the gift is passed into the channel. The channel consumer receives the gift, taking ownership, and is able to open it. If we try to open the gift after sending it into the channel, the compiler will shout at us, because we are violating the ownership rules. Already, we are seeing how Rust helps us prevent bugs.

أولاً ، إليك مثال على تمرير الرسالة باستخدام (جو). على الجانب الأيسر ، نصنع هدية ، ثم نرسلها عبر القناة. في بعض الحالات الأخرى ، يتم بشكل روتيني على الجانب الأيمن ، يتم استلام الهدية وفتحها. جامع القمامة (جو) سيدير الذاكرة لنا. ومع ذلك ، في الرمز الموجود على الجانب الأيسر ، فتحنا الهدية عن طريق الخطأ بعد إرسالها إلى القناة. سيتم فتح الهدية مرتين ، مما يؤدي إلى حدوث خطأ.

Because Rust enforces the rule that only one variable owns data, when that variable goes out of scope without passing off ownership, there is no possible way for the data to be accessed. Rust takes advantage of that and will automatically free the memory at that point. There is no need to manually free the memory.

نظرًا لأن (راست) يفرض قاعدة أن متغيرًا واحدًا فقط يمتلك البيانات ، عندما يخرج هذا المتغير عن النطاق دون تمرير الملكية ، فلا توجد طريقة ممكنة للوصول إلى البيانات. يستفيد (راست) من ذلك وسيحرر الذاكرة تلقائيًا في تلك المرحلة. ليست هناك حاجة لتحرير الذاكرة يدويًا.

Rust’s ownership model is part of the type system and based on a concept called affine types. An affine type imposes a rule that every variable is used at most once. The key is to define what “used” means. In the context of Rust, a use is either moving the data or dropping it. By using affine types, the Rust compiler is able to reason about a program and enforce its ownership rules.

نموذج ملكية (راست) هو جزء من نظام النوع ويستند إلى مفهوم يسمى الأنواع الأفينية. يفرض النوع الأفيني قاعدة مفادها أن كل متغير يستخدم مرة واحدة على الأكثر. المفتاح هو تحديد ما تعنيه كلمة "مستعملة". في سياق (راست) ، يكون الاستخدام إما نقل البيانات أو إسقاطها. باستخدام الأنواع الأفينية ، يكون مترجم (راست) قادرًا على التفكير في البرنامج وفرض قواعد الملكية الخاصة به.

The affine type system used by Rust is based on the work done in the early 1990s, when some folks attempted to design a garbage collector free lisp. While successful, they found that they lost a lot of runtime performance due to the excessive copying introduced by not being able to have multiple references to the same piece of data.

يعتمد نظام النوع الأفيني الذي يستخدمه (راست) على العمل المنجز في أوائل التسعينيات ، عندما حاول بعض الأشخاص تصميم جامع قمامة خالٍ من اللثغة. على الرغم من نجاحهم ، وجدوا أنهم فقدوا الكثير من أداء وقت التشغيل بسبب النسخ المفرط الذي تم تقديمه من خلال عدم قدرتهم على الحصول على مراجع متعددة لنفس قطعة البيانات.

And this gets us to the second innovation that has enabled Rust: the borrow checker. When writing larger programs we tend to use abstractions to help organize ideas. One abstraction that you’re probably familiar with is a function. Functions often require arguments. With only ownership, to call a function, we would need to pass ownership of the data into the function and the function would need to pass ownership of the data back when returning. This requires copying memory around and was the source of garbage collector-less lisp performance challenges.

وهذا يقودنا إلى الابتكار الثاني الذي مكّن (راست): مدقق الاقتراض. عند كتابة برامج أكبر ، نميل إلى استخدام الأفكار المجردة للمساعدة في تنظيم الأفكار. إحدى التجريدات التي ربما تكون على دراية بها هي دالة. غالبا ما تتطلب الوظائف الحجج. مع الملكية فقط ، لاستدعاء وظيفة ، سنحتاج إلى نقل ملكية البيانات إلى الوظيفة وستحتاج الوظيفة إلى نقل ملكية البيانات مرة أخرى عند العودة. يتطلب هذا نسخ ذاكرة حولها وكان مصدرًا لتحديات الأداء غير اللاذعة لمجمع القمامة.

To solve this, Rust lets you borrow data. So, if we have a gift, we own it. It is ours. If our friend wants to admire it, she can borrow it for a moment, but then she has to give it back to us. Also, while our friend is borrowing the gift, we cannot hand off ownership of the gift to anyone else, because it is currently being borrowed. Most crucially, the Rust compiler enforces these rules, so our friend can’t just run off with the gift. And because the Rust compiler enforces that guarantee, when borrowing data, memory doesn’t have to be copied. The memory stays where it is, and a pointer is passed around. The pointer is guaranteed to be valid. When you put it all together, you have a system that is efficient and prevents bugs, even as the program gets larger and more complex.

لحل هذه المشكلة ، يتيح لك (راست) استعارة البيانات. لذا ، إذا كان لدينا هدية ، فإننا نمتلكها. إنه ملكنا. إذا أرادت صديقتنا الإعجاب بها ، يمكنها استعارته للحظة ، ولكن بعد ذلك عليها إعادتها إلينا. أيضًا ، أثناء استعارة صديقنا للهدية ، لا يمكننا تسليم ملكية الهدية إلى أي شخص آخر ، لأنه يتم استعارتها حاليًا. الأهم من ذلك ، أن مترجم (راست) يفرض هذه القواعد ، لذلك لا يمكن لصديقنا الهروب بالهدية. ولأن مترجم (راست) يفرض هذا الضمان ، عند استعارة البيانات ، لا يلزم نسخ الذاكرة. تبقى الذاكرة في مكانها ، ويتم تمرير المؤشر حولها. المؤشر مضمون ليكون صالحًا. عندما تجمع كل ذلك معًا ، يكون لديك نظام فعال ويمنع الأخطاء ، حتى عندما يصبح البرنامج أكبر وأكثر تعقيدًا.

And the same system that prevents memory unsafety can also prevent data races, a category of concurrency bug. A data race happens when two or more threads are concurrently accessing the same data and at least one of those accesses is a mutation. The type system that models ownership and borrowing is able to uphold the same guarantee across multiple threads, enabling more aggressive use of concurrency.

ونفس النظام الذي يمنع عدم أمان الذاكرة يمكنه أيضًا منع سباقات البيانات ، وهي فئة من أخطاء التزامن. يحدث سباق البيانات عندما يقوم اثنان أو أكثر من الخيوط بالوصول إلى نفس البيانات بشكل متزامن ويكون أحد هذه الوصول على الأقل طفرة. إن نظام النوع الذي يصمم الملكية والاقتراض قادر على الحفاظ على نفس الضمان عبر خيوط متعددة ، مما يتيح استخدامًا أكثر قوة للتزامن.

Here is an example of how easy it can be to safely add concurrency to a Rust application. We have a function that iterates through an array of numbers and sums all even numbers. This is a highly parallelizable operation and for very large arrays, we could see the function getting significantly faster by adding concurrency.

فيما يلي مثال على مدى سهولة إضافة التزامن بأمان إلى تطبيق (راست). لدينا دالة تتكرر عبر مصفوفة من الأعداد وتجمع كل الأعداد الزوجية. هذه عملية متوازية للغاية وبالنسبة للمصفوفات الكبيرة جدًا ، يمكننا أن نرى الوظيفة تزداد بشكل أسرع عن طريق إضافة التزامن.

The left side shows a single threaded version and the right side shows you the parallel version using the rayon library. And look at how similar the functions are. You get all the power of concurrency, without the hazards, by basically writing the same code. The only difference is that we use the par_iter() method instead of iter().

يُظهر الجانب الأيسر إصدارًا مترابطًا واحدًا ويظهر لك الجانب الأيمن الإصدار المتوازي باستخدام مكتبة الرايون. وانظر إلى مدى تشابه الوظائف. يمكنك الحصول على كل قوة التزامن ، دون مخاطر ، من خلال كتابة نفس الرمز بشكل أساسي. الاختلاف الوحيد هو أننا نستخدم طريقة par_iter () بدلاً من iter ().

The parallel version will spread the computation across many threads, all while avoiding copying the array of numbers being passed as the argument. Rayon is able to provide this API safely thanks to Rust’s ownership and borrow checking system. All the checks to guarantee safety happen at compile time.

سينشر الإصدار المتوازي الحساب عبر العديد من (ثريدز) ، مع تجنب نسخ مصفوفة الأرقام التي يتم تمريرها كوسيطة. يستطيع (رايون) توفير واجهة برمجة التطبيقات هذه بأمان بفضل نظام التحقق من ملكية واستعارة (راست). يتم إجراء جميع الفحوصات لضمان السلامة في وقت التجميع

Getting Started with Rust

بدء المشوار مع (راست)

Hopefully, by now we have gotten you interested in Rust and starting your journey of sustainability in the cloud. So where to start? The good news is, all the content you need is available online and there are places you can go to get started.

نأمل أن نكون قد جعلنا الآن مهتمًا بـ (راست) وبدء رحلتك للاستدامة في السحابة. إذن من أين تبدأ؟ والخبر السار هو أن كل المحتوى الذي تحتاجه متاح عبر الإنترنت وهناك أماكن يمكنك الذهاب إليها للبدء.

First, you will need to learn the Rust programming language. The Rust book is an excellent resource to get started learning the language. It will help you get the Rust toolchain installed and teach you the language. The website also has exercises and lots of code examples to read. If you get stuck at any point, have questions, or need clarification, you can post on the user forum or talk directly on the community Discord server. The Discord server is usually the fastest way to get help. There are always people active there who can answer questions in real time.

أولاً ، سوف تحتاج إلى تعلم لغة البرمجة (راست). كتاب (راست) هو مصدر ممتاز للبدء في تعلم اللغة. سوف يساعدك في تثبيت سلسلة أدوات (راست) ويعلمك اللغة. يحتوي الموقع أيضًا على تمارين والكثير من أمثلة التعليمات البرمجية لقراءتها. إذا واجهتك مشكلة في أي وقت ، أو كانت لديك أسئلة ، أو كنت بحاجة إلى توضيح ، فيمكنك النشر على منتدى المستخدم أو التحدث مباشرة على خادم (ديسكورد) الخاص بالمجتمع. عادةً ما يكون خادم (ديسكورد) هو أسرع طريقة للحصول على المساعدة. هناك دائمًا أشخاص نشيطون هناك يمكنهم الإجابة على الأسئلة في الوقت الفعلي.

Once you have gone through the Rust website, you should be comfortable enough to start building things, but there is another resource we want to call out for diving deeper. The Crust of Rust is a great youtube channel by Jon Gjenset. He does really deep dives on various Rust related topics, popping the hood and explaining how things work. His videos are multiple hours long, but we keep hearing from people how valuable they are for learning Rust.

بمجرد تصفح موقع (راست) على الويب ، يجب أن تكون مرتاحًا بما يكفي لبدء بناء الأشياء ، ولكن هناك مورد آخر نريد أن ندعوه للغوص بشكل أعمق. (قشرة راست) هي قناة يوتيوب رائعة من قبل (جو جينسيت). يقوم بالغوص العميق حقًا في مختلف الموضوعات ذات الصلة بـ (راست) ، وظهر الغطاء وشرح كيفية عمل الأشياء. تمتد مقاطع الفيديو الخاصة به إلى عدة ساعات ، لكننا نسمع باستمرار من الأشخاص مدى أهميتها لتعلم (راست).

The Future of Rust

مستقبل (راست)

Rust is challenging to learn. Of the more than 8,000 developers responding to the 2020 Rust user survey, only about 100 identified as “expert”, and of the respondents that said they were no longer using Rust, 55% cited learning or productivity as their reason for abandoning the language.

الصدأ صعب التعلم. من بين أكثر من 8000 مطور استجابوا لاستطلاع مستخدمي (راست) لعام 2020 ، تم تحديد حوالي 100 فقط على أنهم "خبراء" ، ومن بين المستجيبين الذين قالوا إنهم لم يعودوا يستخدمون (راست) ، ذكر 55٪ منهم التعلم أو الإنتاجية كسبب لتخليهم عن اللغة.

It takes experienced engineers 3-6 months of study, supported by access to subject matter experts, to become productive with Rust. Some engineers have likened learning Rust to learning to eat your vegetables, and while many of them love it once they are productive, a lot of engineers are deciding against learning it or abandoning the effort before they become productive. The potential impact of Rust on sustainability and security will only materialize if we turn the broccoli into a brownie.

يستغرق المهندسون ذوو الخبرة من 3 إلى 6 أشهر من الدراسة ، مدعومًا بإمكانية الوصول إلى الخبراء المتخصصين ، ليصبحوا منتجين مع Rust. شبّه بعض المهندسين تعلم Rust بتعلم أكل الخضروات ، وبينما يحبها الكثيرون بمجرد أن يصبحوا منتجين ، يقرر الكثير من المهندسين عدم تعلمه أو التخلي عن الجهد قبل أن يصبحوا منتجين. لن يتحقق التأثير المحتمل للصدأ على الاستدامة والأمن إلا إذا حولنا البروكلي إلى كعكة براوني.

No one developer, service, or corporation can deliver substantial impact on sustainability. Adoption of Rust is like recycling; it only has impact if we all participate. To achieve broad adoption, we are going to have to grow the developer community.

لا يوجد مطور أو خدمة أو شركة واحدة يمكنها إحداث تأثير كبير على الاستدامة. اعتماد (راست) يشبه إعادة التدوير ؛ سيكون له تأثير فقط إذا شاركنا جميعًا. لتحقيق التبني على نطاق واسع ، سيتعين علينا تنمية مجتمع المطورين.

The Rust developer community has been the fastest growing over the last two years, but based on historical trends, we know that of the half million developers that joined the Rust community in the last 12 months, most of them are not yet proficient with the language. We have some work to do on the Rust developer experience.

لا يوجد مطور أو خدمة أو شركة واحدة يمكنها إحداث تأثير كبير على الاستدامة. اعتماد (راست) يشبه إعادة التدوير ؛ سيكون له تأثير فقط إذا شاركنا جميعًا. لتحقيق التبني على نطاق واسع ، سيتعين علينا تنمية مجتمع المطورين.

The question that raises is which developer experience? Engineers working on the Linux kernel have a very different ideal developer experience than an engineer building a database service or an engineer delivering a retail website. We can identify the Rust user personas by looking at three dimensions.

السؤال الذي يطرح هو أي تجربة مطور؟ يتمتع المهندسون الذين يعملون على نواة (الينكس) بتجربة مطور مثالية مختلفة تمامًا عن المهندس الذي يقوم ببناء خدمة قاعدة بيانات أو مهندس يقدم موقع ويب للبيع بالتجزئة. يمكننا تحديد شخصيات مستخدم (راست) من خلال النظر إلى ثلاثة أبعاد.

The first distinction is their reason for coming to Rust. Are they choosing Rust for performance? For security? For sustainability? The second distinction is domain. Are they working in an embedded environment with restricted resources? Are they working in machine learning with long running jobs that have huge amounts of data in incremental computations? The third distinction is the developer’s experience. Are they a systems programmer? Maybe they’ve only worked with dynamically typed languages?

أول تمييز هو سبب قدومهم إلى روست. هل يختارون راست للأداء؟ للأمن؟ من أجل الاستدامة؟ التمييز الثاني هو المجال. هل يعملون في بيئة مضمنة بموارد محدودة؟ هل يعملون في التعلم الآلي بوظائف طويلة الأمد تحتوي على كميات هائلة من البيانات في الحسابات الإضافية؟ التمييز الثالث هو تجربة المطور. هل هم مبرمج أنظمة؟ ربما عملوا فقط مع اللغات المكتوبة ديناميكيًا؟

We need to evolve those permutations of priority, domain, and developer experience into personas that allow us to develop a robust understanding, a common vocabulary, and an explicit set of engineering trade offs. We usually give these personas names, so let’s consider an example we’ll call “Bob”.

نحن بحاجة إلى تطوير تلك التباديل في الأولوية والمجال وتجربة المطور إلى شخصيات تسمح لنا بتطوير فهم قوي ومفردات مشتركة ومجموعة واضحة من المقايضات الهندسية. عادةً ما نعطي أسماء هذه الشخصيات ، لذا دعنا نفكر في مثال نسميه "بوب".

Bob is building a cryptographic solution, and he’s choosing Rust for the security properties. Bob has a distinct set of engineering trade offs. Bob prioritizes security over performance; he prioritizes security over operations. What that means in practice is that Bob would rather have a slow response than plain text, and he would rather have an outage than respond to an unsigned request.

يقوم بوب ببناء حل تشفير ، ويختار (راست) لخصائص الأمان. يمتلك بوب مجموعة مميزة من المقايضات الهندسية. يعطي بوب الأولوية للأمان على الأداء ؛ يعطي الأولوية للأمن على العمليات. ما يعنيه ذلك من الناحية العملية هو أن بوب يفضل أن يكون الرد بطيئًا بدلاً من النص العادي ، ويفضل أن يكون لديه انقطاع عن الاستجابة لطلب غير موقع.

For each of these personas, there are unique engineering trade offs, and what we want to do is to create a space in the Rust landscape that’s well-defined and easily discoverable and empowers all the Bobs to collaborate on building the best, whole developer experience for themselves without negatively impacting other personas.

لكل من هؤلاء الأشخاص ، هناك مقايضات هندسية فريدة ، وما نريد القيام به هو إنشاء مساحة في مشهد (راست) يكون محددًا جيدًا ويمكن اكتشافه بسهولة ويمكّن جميع البوبات من التعاون في بناء أفضل تجربة مطور كاملة لأنفسهم دون التأثير سلبًا على الشخصيات الأخرى.

Rust is an amazing technology to sustain and secure our industry, and you can start doing that today. We have a lot of work to do before everyone can use Rust, and the Rust Foundation is working to create platforms for effective, cross industry collaboration on that work. We hope you’ll join us.

(راست) هو تقنية رائعة للحفاظ على صناعتنا وتأمينها ، ويمكنك البدء في فعل ذلك اليوم. لدينا الكثير من العمل الذي يجب القيام به قبل أن يتمكن الجميع من استخدام (راست) ، وتعمل مؤسسة (راست) على إنشاء منصات للتعاون الفعال عبر الصناعة في هذا العمل. نأمل أن تنضم إلينا.