{"id":225003,"date":"2014-06-06T07:17:03","date_gmt":"2014-06-06T03:17:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=225003"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=225003","title":{"rendered":"<span class=\"post_title\">\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 Rust \u0438 \u0421++ \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445<\/span>"},"content":{"rendered":"<div class=\"content html_format\">\n<h4>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h4>\n<p>  \u0412\u043e\u0442 \u0438 <a href=\"http:\/\/habrahabr.ru\/post\/224607\/#comment_7639951\">\u043e\u0431\u0435\u0449\u0430\u043d\u043d\u043e\u0435<\/a> \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044f\u0437\u044b\u043a\u043e\u0432. \u041f\u0440\u0438\u043c\u0435\u0440\u044b, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0438\u0441\u043a\u0443\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u0432\u043e\u0451 \u0432\u043e\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u044b \u0443\u0433\u0440\u043e\u0437\u044b \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0438\u0440\u0435.<\/p>\n<p>  \u0412\u0441\u0435 C++ \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u044b\u043b\u0438 \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 gcc-4.7.2 \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 c++11, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/www.compileonline.com\/compile_cpp11_online.php\">online compiler<\/a>. \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 Rust \u0431\u044b\u043b\u0438 \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 Rust (nightly, 0.11-pre), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/playtest.rust-lang.org\/\">rust playpen<\/a>.<\/p>\n<p>  \u042f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e C++14 (\u0438 \u0434\u0430\u043b\u0435\u0435) \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043b\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u043b\u0430\u0431\u044b\u0435 \u043c\u0435\u0441\u0442\u0430 \u044f\u0437\u044b\u043a\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u0420\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u0442\u0435\u043c\u0443 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043c\u0435\u0448\u0430\u0435\u0442 C++ \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u0437\u0432\u0451\u0437\u0434 (\u0438 \u043c\u0435\u0448\u0430\u0435\u0442 \u043b\u0438), \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0412\u0430\u0448\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043d\u043e\u0435 \u043c\u043d\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445. \u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442\u0441\u044f \u043b\u044e\u0431\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e D.<\/p>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0442\u0438\u043f\u043e\u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0430<\/h4>\n<p>  \u0410\u0432\u0442\u043e\u0440 \u0421++ \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u043d\u0435\u0434\u043e\u0432\u043e\u043b\u0435\u043d \u0442\u0435\u043c, \u043a\u0430\u043a \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0432 \u044f\u0437\u044b\u043a\u0435, \u043d\u0430\u0437\u0432\u0430\u0432 \u0438\u0445 &quot;<i>compile-time duck typing<\/i>&quot; \u0432 \u043d\u0435\u0434\u0430\u0432\u043d\u0435\u043c \u0432\u044b\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0438 \u043d\u0430 Lang-NEXT. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0435\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d, \u0433\u043b\u044f\u0434\u044f \u043d\u0430 \u0435\u0433\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435. \u0421\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442\u0441\u044f \u043c\u043e\u043d\u0441\u0442\u0440\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0441\u043e\u0431\u0440\u0430\u0442\u044c, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0432\u043e\u0442 \u0442\u0430\u043a\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443:  <\/p>\n<pre><code class=\"cpp\">#include &lt;vector&gt; #include &lt;algorithm&gt; int main() {     int a;     std::vector&lt; std::vector &lt;int&gt; &gt; v;     std::vector&lt; std::vector &lt;int&gt; &gt;::const_iterator it = std::find( v.begin(), v.end(), a ); } <\/code><\/pre>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0441\u0435\u0431\u0435 \u0440\u0430\u0434\u043e\u0441\u0442\u044c \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u0447\u0438\u0442\u0430\u044e\u0449\u0435\u0433\u043e <a href=\"http:\/\/codegolf.stackexchange.com\/questions\/1956\/generate-the-longest-error-message-in-c\">\u043c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435<\/a>, \u0435\u0441\u043b\u0438 \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u043b \u0442\u0430\u043a\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e.<\/p>\n<p>  \u0428\u0430\u0431\u043b\u043e\u043d\u044b \u0432 Rust \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u043e \u0438\u0445 \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u0442\u044c \u0447\u0451\u0442\u043a\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043e\u0448\u0438\u0431\u043a\u0430\u043c\u0438 \u0432 \u0441\u0430\u043c\u043e\u043c \u0448\u0430\u0431\u043b\u043e\u043d\u0435 (\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u044b\u0442\u044c \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 \u0412\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0447\u0443\u0436\u043e\u0439\/\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u0447\u043d\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d) \u0438 \u0432 \u043c\u0435\u0441\u0442\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0433\u0434\u0435 \u0432\u0441\u0451, \u0447\u0442\u043e \u043e\u0442 \u0412\u0430\u0441 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0442\u0438\u043f\u0443, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435:  <\/p>\n<pre><code class=\"rust\">trait Sortable {} fn sort&lt;T: Sortable&gt;(array: &mut [T]) {} fn main() {     sort(&mut [1,2,3]); } <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435:  <\/p>\n<blockquote><p>demo:5:5: 5:9 error: failed to find an implementation of trait Sortable for int<br \/>  demo:5 sort(&#038;mut [1,2,3]);  <\/p><\/blockquote>\n<h4>\u041e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438<\/h4>\n<p>  \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0446\u0435\u043b\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u0421++, \u0432\u044b\u0440\u0430\u0436\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0432 \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u0438 \u043f\u0430\u0434\u0435\u043d\u0438\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0438\u0437-\u0437\u0430 \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c.<br \/>  \u041f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"cpp\">int main() {     int *x = new int(1);     delete x;     *x = 0; } <\/code><\/pre>\n<p>  \u0412 Rust \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u043e\u0434\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0430\u043c\u044f\u0442\u044c \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 \u0436\u0438\u0432\u0451\u0442, \u043f\u043e\u043a\u0430 \u043e\u043d\u0430 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u0438 Rust \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043d\u0435\u0451 \u043f\u0435\u0440\u0435\u0436\u0438\u043b\u0438 \u044d\u0442\u0443 \u043e\u0431\u043b\u0430\u0441\u0442\u044c (\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0432\u0448\u0438\u0439\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c). \u0415\u0441\u043b\u0438 \u0436\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0430 \u0432 \u043a\u0443\u0447\u0435 \u2014 \u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043d\u0435\u0451 (<code>Box&lt;T&gt;<\/code>) \u0432\u0435\u0434\u0451\u0442 \u0441\u0435\u0431\u044f \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 (\u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0438\u0437 \u0437\u043e\u043d\u044b \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438). \u0414\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u0441\u0447\u0451\u0442 \u0441\u0441\u044b\u043b\u043e\u043a (<code>std::rc::Rc&lt;T&gt;<\/code>) \u0438 \u0441\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430 (<code>std::gc::Gc&lt;T&gt;<\/code>), \u043e\u0431\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043a\u0430\u043a \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b (\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0438).<\/p>\n<h4>\u041f\u043e\u0442\u0435\u0440\u044f\u0432\u0448\u0438\u0439\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e<\/h4>\n<p>  \u0412\u0435\u0440\u0441\u0438\u044f \u0421++:  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  int *bar(int *p) {     return p; } int* foo(int n) {     return bar(&n); } int main() {     int *p1 = foo(1);     int *p2 = foo(2);     printf(&quot;%d, %d\\n&quot;, *p1, *p2); } <\/code><\/pre>\n<p>  \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435:  <\/p>\n<blockquote><p>2, 2  <\/p><\/blockquote>\n<p>  \u0412\u0435\u0440\u0441\u0438\u044f Rust:  <\/p>\n<pre><code class=\"rust\">fn bar&lt;'a&gt;(p: &'a int) -&gt; &'a int {     return p; } fn foo(n: int) -&gt; &int {     bar(&n) } fn main() {     let p1 = foo(1);     let p2 = foo(2);     println!(&quot;{}, {}&quot;, *p1, *p2); } <\/code><\/pre>\n<p>  \u0420\u0443\u0433\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:  <\/p>\n<blockquote><p>demo:5:10: 5:11 error: `n` does not live long enough<br \/>  demo:5 bar(&#038;n)<br \/>   ^<br \/>  demo:4:24: 6:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 4:23\u2026<br \/>  demo:4 fn foo(n: int) -&gt; &#038;int {<br \/>  demo:5 bar(&#038;n)<br \/>  demo:6 }<br \/>  demo:4:24: 6:2 note: &#8230;but borrowed value is only valid for the block at 4:23<br \/>  demo:4 fn foo(n: int) -&gt; &#038;int {<br \/>  demo:5 bar(&#038;n)<br \/>  demo:6 }  <\/p><\/blockquote>\n<p>  <\/p>\n<h4>\u041d\u0435\u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/h4>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; int minval(int *A, int n) {   int currmin;   for (int i=0; i&lt;n; i++)     if (A[i] &lt; currmin)       currmin = A[i];   return currmin; } int main() {     int A[] = {1,2,3};     int min = minval(A,3);     printf(&quot;%d\\n&quot;, min); } <\/code><\/pre>\n<p>  \u0412\u044b\u0434\u0430\u0451\u0442 \u043c\u043d\u0435 0 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435, \u0445\u043e\u0442\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0437\u0434\u0435\u0441\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0410 \u0432\u043e\u0442 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043d\u0430 Rust (\u043f\u0440\u044f\u043c\u043e\u0439 \u043d\u0435-\u0438\u0434\u0438\u043e\u043c\u0430\u0442\u0438\u0447\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434):  <\/p>\n<pre><code class=\"rust\">fn minval(A: &[int]) -&gt; int {   let mut currmin;   for a in A.iter() {     if *a &lt; currmin {       currmin = *a;     }   }   currmin } fn main() {     let A = [1i,2i,3i];     let min = minval(A.as_slice());     println!(&quot;{}&quot;, min); } <\/code><\/pre>\n<p>  \u041d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f, \u043e\u0448\u0438\u0431\u043a\u0430: <\/p>\n<blockquote><p>use of possibly uninitialized variable: `currmin`<\/p><\/blockquote>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u0438\u0434\u0438\u043e\u043c\u0430\u0442\u0438\u0447\u043d\u044b\u0439 (\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439) \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0431\u044b \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"rust\">fn minval(A: &[int]) -&gt; int {   A.iter().fold(A[0], |u,&a| {     if a&lt;u {a} else {u}   }) } <\/code><\/pre>\n<h4>\u041d\u0435\u044f\u0432\u043d\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/h4>\n<p>  <\/p>\n<pre><code class=\"cpp\">struct A{     int *x;     A(int v): x(new int(v)) {}     ~A() {delete x;} };  int main() {     A a(1), b=a; } <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f, \u043e\u0434\u043d\u0430\u043a\u043e \u043f\u0430\u0434\u0430\u0435\u0442 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438:  <\/p>\n<blockquote><p>*** glibc detected *** demo: double free or corruption (fasttop): 0x0000000000601010 ***  <\/p><\/blockquote>\n<p>  \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043d\u0430 Rust:  <\/p>\n<pre><code class=\"rust\">struct A{     x: Box&lt;int&gt; } impl A {     pub fn new(v: int) -&gt; A {         A{ x: box v }     } } impl Drop for A {     fn drop(&mut self) {} \/\/\u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0447\u043d\u043e\u0439 \u043a\u043e\u043f\u0438\u0438 \u0421++ } fn main() {     let a = A::new(1);     let _b = a; } <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043a\u0438. \u041a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u0438\u0431\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 <code>trait Copy<\/code>.<br \/>  Rust \u043d\u0438\u0447\u0435\u0433\u043e \u0437\u0430 \u0412\u0430\u0448\u0435\u0439 \u0441\u043f\u0438\u043d\u043e\u0439 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u0425\u043e\u0442\u0438\u0442\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>Eq<\/code> \u0438\u043b\u0438 <code>Clone<\/code>? \u041f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>deriving<\/code> \u043a \u0412\u0430\u0448\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435:  <\/p>\n<pre><code class=\"rust\">#[deriving(Clone, Eq, Hash, PartialEq, PartialOrd, Ord, Show)] struct A{     x: Box&lt;int&gt; } <\/code><\/pre>\n<h4>\u041f\u0435\u0440\u0435\u043a\u0440\u044b\u0442\u0438\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/h4>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; struct X {  int a, b; };  void swap_from(X& x, const X& y) {     x.a = y.b; x.b = y.a; } int main() {     X x = {1,2};     swap_from(x,x);     printf(&quot;%d,%d\\n&quot;, x.a, x.b); } <\/code><\/pre>\n<p>  \u0412\u044b\u0434\u0430\u0451\u0442 \u043d\u0430\u043c:  <\/p>\n<blockquote><p>2,2<\/p><\/blockquote>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u044f\u0432\u043d\u043e \u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u0442, \u0447\u0442\u043e \u0435\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0443\u0442 cc\u044b\u043b\u043a\u0438 \u043d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442. \u0427\u0442\u043e\u0431\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440, \u0447\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435, \u0432 \u042199 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Restrict\">restrict<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u043e\u043d \u0441\u043b\u0443\u0436\u0438\u0442 \u043b\u0438\u0448\u044c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u0443 \u0438 \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u0412\u0430\u043c \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0442\u0438\u0439: \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043d\u0430 Rust:  <\/p>\n<pre><code class=\"rust\">struct X { pub a: int, pub b: int } fn swap_from(x: &mut X, y: &X) {     x.a = y.b; x.b = y.a; } fn main() {     let mut x = X{a:1, b:2};     swap_from(&mut x, &x); } <\/code><\/pre>\n<p>  \u0412\u044b\u0434\u0430\u0451\u0442 \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0440\u0443\u0433\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e:  <\/p>\n<blockquote><p>demo:7:24: 7:25 error: cannot borrow `x` as immutable because it is also borrowed as mutable<br \/>  demo:7 swap_from(&#038;mut x, &#038;x);<br \/>   ^<br \/>  demo:7:20: 7:21 note: previous borrow of `x` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `x` until the borrow ends<br \/>  demo:7 swap_from(&#038;mut x, &#038;x);<br \/>   ^<br \/>  demo:7:26: 7:26 note: previous borrow ends here<br \/>  demo:7 swap_from(&#038;mut x, &#038;x);  <\/p><\/blockquote>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0447\u0435\u0440\u0435\u0437 &quot;<code>&mut<\/code>&quot; \u0438 &quot;<code>&<\/code>&quot; \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u044f, \u0447\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043d\u0438\u043a\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u0430 <code>&mut<\/code> \u0441\u0441\u044b\u043b\u043a\u0430. \u042d\u0442\u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0438 \u043e\u0431\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 \u0438 \u043d\u0435 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0430\u043c\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0441\u0438\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u043d\u0430 C99 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <code>restrict<\/code> \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 (Rust \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 LLVM \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0441\u044b\u043b\u043e\u043a), \u0447\u0442\u043e \u0440\u0430\u0437\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u0440\u0443\u043a\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u0443.<\/p>\n<h4>\u0418\u0441\u043f\u043e\u0440\u0447\u0435\u043d\u043d\u044b\u0439 \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440<\/h4>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;vector&gt; int main() {     std::vector&lt;int&gt; v;     v.push_back(1);     v.push_back(2);     for(std::vector&lt;int&gt;::const_iterator it=v.begin(); it!=v.end(); ++it) {         if (*it &lt; 5)             v.push_back(5-*it);     } } <\/code><\/pre>\n<p>  \u041a\u043e\u0434 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a, \u043e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u0430\u0434\u0430\u0435\u0442:  <\/p>\n<blockquote><p>Segmentation fault (core dumped)  <\/p><\/blockquote>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 Rust:  <\/p>\n<pre><code class=\"rust\">fn main() {     let mut v: Vec&lt;int&gt; = Vec::new();     v.push(1);     v.push(2);     for x in v.iter() {         if *x &lt; 5 {             v.push(5-*x);         }     } } <\/code><\/pre>\n<p>  \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u044d\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u0432\u0435\u0436\u043b\u0438\u0432\u043e \u0443\u043a\u0430\u0437\u0430\u0432, \u0447\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0432\u0435\u043a\u0442\u043e\u0440 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0435\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u043d\u0435\u043b\u044c\u0437\u044f:  <\/p>\n<blockquote><p>demo:7:13: 7:14 error: cannot borrow `v` as mutable because it is also borrowed as immutable<br \/>  demo:7 v.push(5-*x);<br \/>   ^<br \/>  demo:5:14: 5:15 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends<br \/>  demo:5 for x in v.iter() {<br \/>   ^<br \/>  demo:10:2: 10:2 note: previous borrow ends here<br \/>  demo:5 for x in v.iter() {<br \/>  demo:6 if *x &lt; 5 {<br \/>  demo:7 v.push(5-*x);<br \/>  demo:8 }<br \/>  demo:9 }<br \/>  demo:10 }  <\/p><\/blockquote>\n<h4>\u041e\u043f\u0430\u0441\u043d\u044b\u0439 Switch<\/h4>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; enum {RED, BLUE, GRAY, UNKNOWN} color = GRAY; int main() {   int x;   switch(color) {     case GRAY: x=1;     case RED:     case BLUE: x=2;   }   printf(&quot;%d&quot;, x); } <\/code><\/pre>\n<p>  \u0412\u044b\u0434\u0430\u0451\u0442 \u043d\u0430\u043c \u00ab2\u00bb. \u0412 Rust \u0436\u044b \u0412\u044b \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u0440\u0438 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043a\u043e\u0434 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u043f\u0440\u044b\u0433\u0430\u0435\u0442 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442 <code>break<\/code>. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Rust \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"rust\">enum Color {RED, BLUE, GRAY, UNKNOWN} fn main() {   let color = GRAY;   let x = match color {       GRAY =&gt; 1,       RED | BLUE =&gt; 2,       _ =&gt; 3,   };   println!(&quot;{}&quot;, x); } <\/code><\/pre>\n<h5>\u0421\u043b\u0443\u0447\u0430\u0439\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u0441 \u0437\u0430\u043f\u044f\u0442\u043e\u0439<\/h5>\n<p>  <\/p>\n<pre><code class=\"cpp\">int main() {   int pixels = 1;   for (int j=0; j&lt;5; j++);     pixels++; } <\/code><\/pre>\n<p>  \u0412 Rust \u0412\u044b \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0442\u0435\u043b\u0430 \u0446\u0438\u043a\u043b\u043e\u0432 \u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439 \u0432 \u0444\u0438\u0433\u0443\u0440\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438. \u041c\u0435\u043b\u043e\u0447\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u043e \u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u043e\u0448\u0438\u0431\u043e\u043a \u043c\u0435\u043d\u044c\u0448\u0435.<\/p>\n<h4>\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c<\/h4>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;pthread.h&gt; #include &lt;unistd.h&gt;  class Resource {     int *value; public:     Resource(): value(NULL) {}     ~Resource() {delete value;}     int *acquire() {         if (!value) {             value = new int(0);         }         return value;     } };  void* function(void *param) {     int *value = ((Resource*)param)-&gt;acquire();     printf(&quot;resource: %p\\n&quot;, (void*)value);     return value; }  int main() {     Resource res;     for (int i=0; i&lt;5; ++i) {         pthread_t pt;         pthread_create(&pt, NULL, function, &res);     }     \/\/sleep(10);     printf(&quot;done\\n&quot;); } <\/code><\/pre>\n<p>  \u041f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0433\u043e:  <\/p>\n<blockquote><p>done<br \/>  resource: 0x7f229c0008c0<br \/>  resource: 0x7f22840008c0<br \/>  resource: 0x7f228c0008c0<br \/>  resource: 0x7f22940008c0<br \/>  resource: 0x7f227c0008c0  <\/p><\/blockquote>\n<p>  \u042d\u0442\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u043d\u0430 Rust:  <\/p>\n<pre><code class=\"rust\">struct Resource {     value: Option&lt;int&gt;, } impl Resource {     pub fn new() -&gt; Resource {         Resource{ value: None }     }     pub fn acquire&lt;'a&gt;(&'a mut self) -&gt; &'a int {         if self.value.is_none() {             self.value = Some(1);         }         self.value.get_ref()     } }  fn main() {     let mut res = Resource::new();     for _ in range(0,5) {         spawn(proc() {             let ptr = res.acquire();             println!(&quot;resource {}&quot;, ptr)         })     } } <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0443\u0433\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e, \u0438\u0431\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u043e\u0442 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0438 \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0431\u044a\u0435\u043a\u0442.  <\/p>\n<blockquote><p>demo:20:23: 20:26 error: cannot borrow immutable captured outer variable in a proc `res` as mutable<br \/>  demo:20 let ptr = res.acquire();  <\/p><\/blockquote>\n<p>  \u0412\u043e\u0442 \u0442\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u0447\u0451\u0441\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440:  <\/p>\n<pre><code class=\"rust\">extern crate sync; use sync::{Arc, RWLock};  struct Resource {     value: Option&lt;Box&lt;int&gt;&gt;, } impl Resource {     pub fn new() -&gt; Resource {         Resource{ value: None }     }     pub fn acquire(&mut self) -&gt; *int {         if self.value.is_none() {             self.value = Some(box 1)         }         &**self.value.get_ref() as *int     } }  fn main() {     let arc_res = Arc::new(RWLock::new(Resource::new()));     for _ in range(0,5) {         let child_res = arc_res.clone();         spawn(proc() {             let ptr = child_res.write().acquire();             println!(&quot;resource: {}&quot;, ptr)         })     } } <\/code><\/pre>\n<p>  \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 <code>Arc<\/code> (<i>Atomically Reference Counted<\/i> \u2014 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438) \u0438 <code>RWLock<\/code> (\u0434\u043b\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f). \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c:  <\/p>\n<blockquote><p>resource: 0x7ff4b0010378<br \/>  resource: 0x7ff4b0010378<br \/>  resource: 0x7ff4b0010378<br \/>  resource: 0x7ff4b0010378<br \/>  resource: 0x7ff4b0010378  <\/p><\/blockquote>\n<p>  \u041f\u043e\u043d\u044f\u0442\u043d\u043e\u0435 \u0434\u0435\u043b\u043e, \u0447\u0442\u043e \u043d\u0430 \u0421++ \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. \u0418 \u043d\u0430 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0435 \u043c\u043e\u0436\u043d\u043e. Rust \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0434\u0430\u0451\u0442 \u0412\u0430\u043c \u0432\u044b\u0441\u0442\u0440\u0435\u043b\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0432 \u043d\u043e\u0433\u0443, \u043e\u0431\u0435\u0440\u0435\u0433\u0430\u044f \u043e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a. \u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f, \u0437\u043d\u0430\u0447\u0438\u0442 \u043e\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041b\u0443\u0447\u0448\u0435 \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u043b\u0447\u0430\u0441\u0430 \u043d\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0432 \u043f\u0440\u0438\u0435\u043c\u043b\u0438\u043c\u044b\u0439 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u0432\u0438\u0434, \u0447\u0435\u043c \u043f\u043e\u0442\u043e\u043c \u043c\u0435\u0441\u044f\u0446\u0430\u043c\u0438 \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 (<a href=\"http:\/\/superwebdeveloper.com\/2009\/11\/25\/the-incredible-rate-of-diminishing-returns-of-fixing-software-bugs\/\">\u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0435\u0444\u0435\u043a\u0442\u0430<\/a>).<\/p>\n<h4>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043a\u043e\u0434<\/h4>\n<p>  Rust \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0412\u0430\u043c \u0438\u0433\u0440\u0430\u0442\u044c \u0441 \u0433\u043e\u043b\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u0430 <code>unsafe{}<\/code>. \u042d\u0442\u043e \u0442\u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 \u0412\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 &quot;<i>\u041d\u0435 \u043c\u0435\u0448\u0430\u0439! \u042f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u044e.<\/i>&quot;. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0432\u0441\u0435 \u00ab\u0447\u0443\u0436\u0438\u0435\u00bb \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0438\u0437 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u043d\u0430 \u0421 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u044b \u0441\u043b\u0438\u0432\u0430\u0435\u0442\u0435\u0441\u044c) \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0430\u0440\u043a\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u043e\u043f\u0430\u0441\u043d\u044b\u0435. \u0424\u0438\u043b\u043e\u0441\u043e\u0444\u0438\u044f \u044f\u0437\u044b\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u043a\u0443\u0441\u043a\u0438 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0431\u044b\u043b\u0438 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 (\u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430) \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438. \u0422\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0443\u0447\u0430\u0441\u0442\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 <code>Cell<\/code> \u0438 <code>Mutex<\/code>. \u0418\u0437\u043e\u043b\u044f\u0446\u0438\u044f \u043e\u043f\u0430\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0443\u0437\u0438\u0442\u044c \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0435\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043d\u043e \u0438 \u0445\u043e\u0440\u043e\u0448\u0435\u043d\u044c\u043a\u043e \u043f\u043e\u043a\u0440\u044b\u0442\u044c \u0435\u0433\u043e \u0442\u0435\u0441\u0442\u0430\u043c\u0438 (\u043c\u044b \u0434\u0440\u0443\u0436\u0438\u043c \u0441 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Test-driven_development\">TDD<\/a>!).<\/p>\n<h3>\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438<\/h3>\n<p>  <a href=\"https:\/\/air.mozilla.org\/guaranteeing-memory-safety-in-rust\/\">Guaranteeing Memory Safety in Rust (by Niko Matsakis)<\/a><br \/>  <a href=\"http:\/\/www.techtalkshub.com\/rust-safe-systems-programming-fun-fp\/\">Rust: Safe Systems Programming with the Fun of FP (by Felix Klock II)<\/a><br \/>  <a href=\"http:\/\/channel9.msdn.com\/Events\/Lang-NEXT\/Lang-NEXT-2014\/Keynote\">Lang-NEXT: What \u2013 if anything \u2013 have we learned from C++? (by Bjarne Stroustrup)<\/a><br \/>  <a href=\"http:\/\/channel9.msdn.com\/Events\/Lang-NEXT\/Lang-NEXT-2014\/Panel-Systems-Programming-Languages-in-2014-and-Beyond\">Lang-NEXT Panel: Systems Programming in 2014 and Beyond<\/a>      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/225003\/\"> http:\/\/habrahabr.ru\/post\/225003\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">\n<h4>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h4>\n<p>  \u0412\u043e\u0442 \u0438 <a href=\"http:\/\/habrahabr.ru\/post\/224607\/#comment_7639951\">\u043e\u0431\u0435\u0449\u0430\u043d\u043d\u043e\u0435<\/a> \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044f\u0437\u044b\u043a\u043e\u0432. \u041f\u0440\u0438\u043c\u0435\u0440\u044b, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0438\u0441\u043a\u0443\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u0432\u043e\u0451 \u0432\u043e\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u044b \u0443\u0433\u0440\u043e\u0437\u044b \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0438\u0440\u0435.<\/p>\n<p>  \u0412\u0441\u0435 C++ \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u044b\u043b\u0438 \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 gcc-4.7.2 \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 c++11, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/www.compileonline.com\/compile_cpp11_online.php\">online compiler<\/a>. \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 Rust \u0431\u044b\u043b\u0438 \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 Rust (nightly, 0.11-pre), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/playtest.rust-lang.org\/\">rust playpen<\/a>.<\/p>\n<p>  \u042f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e C++14 (\u0438 \u0434\u0430\u043b\u0435\u0435) \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043b\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u043b\u0430\u0431\u044b\u0435 \u043c\u0435\u0441\u0442\u0430 \u044f\u0437\u044b\u043a\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u0420\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u0442\u0435\u043c\u0443 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043c\u0435\u0448\u0430\u0435\u0442 C++ \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u0437\u0432\u0451\u0437\u0434 (\u0438 \u043c\u0435\u0448\u0430\u0435\u0442 \u043b\u0438), \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0412\u0430\u0448\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043d\u043e\u0435 \u043c\u043d\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445. \u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442\u0441\u044f \u043b\u044e\u0431\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e D.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-225003","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/225003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225003"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/225003\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}