{"id":412400,"date":"2024-06-29T22:30:44","date_gmt":"2024-06-29T22:30:44","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=412400"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T22:30:44","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=412400","title":{"rendered":"<span>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440 \u0432 Rust<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043a\u0443\u0447\u0435\u0439, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u0432\u043e\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440. \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a, \u043d\u043e \u043d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c \u2014 \u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e, \u0441 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u043c \u043f\u0440\u043e\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0435\u043c \u0432 \u0441\u0443\u0442\u044c \u044f\u0432\u043b\u0435\u043d\u0438\u0439, \u0442\u0430\u043a \u0447\u0442\u043e \u043b\u0435\u0433\u043a\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 (\u043a\u0430\u043a \u0438 \u043e\u0431\u0435\u0449\u0430\u043b).<\/p>\n<p>  <\/p>\n<ul>\n<li><strong>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/strong>: <a href=\"https:\/\/habr.com\/ru\/post\/573422\/\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043a\u0443\u0447\u0435\u0439 \u0432 Rust<\/a><\/li>\n<li><strong>\u041d\u0430\u0447\u0430\u043b\u043e \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/strong>: <a href=\"https:\/\/habr.com\/ru\/post\/566668\/\">\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435<\/a><\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"static-i-const\">static \u0438 const<\/h1>\n<p>  <\/p>\n<p>\u0412 \u044f\u043f\u043e\u043d\u0441\u043a\u043e\u043c \u0437\u043e\u043e\u043f\u0430\u0440\u043a\u0435 \u0440\u043e\u0434\u0438\u043b\u0441\u044f \u0441\u043b\u043e\u043d\u0435\u043d\u043e\u043a \u2014 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u043f\u0440\u0438 \u0447\u0435\u043c \u0442\u0443\u0442 \u041b\u0443\u0436\u043a\u043e\u0432? \u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440, static \u0438 const?! \u2014 \u0421\u043f\u043e\u043a\u043e\u0439\u043d\u043e, \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u043f\u043e \u0440\u0430\u043d\u0435\u0435 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043b\u0430\u043d\u0443. \u0414\u0430 \u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0438\u0437\u0443\u0447\u0430\u0442\u044c.<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Point {     x: i32,     y: i32, }  static STATIC_POINT: Point = new_point(10, 20); static mut MUT_STATIC_POINT: Point = Point{x: 30, y: 40}; const CONST_POINT: Point = new_point(50, 60);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a\u043e\u0432\u044b \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430 \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 &#171;\u0442\u043e\u0447\u043a\u0430\u043c\u0438&#187;, \u0432 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u0436\u0434\u0443 <code>static STATIC_POINT<\/code> \u0438 <code>const CONST_POINT<\/code>?<\/p>\n<p>  <\/p>\n<h2 id=\"static-i-constadres\">static \u0438 const:: \u0410\u0434\u0440\u0435\u0441<\/h2>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=c93fab5019d685dfb8e568c40b0428dc\" rel=\"nofollow noopener noreferrer\">\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u044b\u0440\u043e\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0438 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn main() {      print_pointer(100, &amp;STATIC_POINT);     unsafe {         print_pointer(200, &amp;MUT_STATIC_POINT);     }     print_pointer(300, &amp;CONST_POINT); }  #[inline(never)] fn print_pointer&lt;T>(label: i32, p: *const T) {     println!(\"label:{} pointer:{:p}\", label, p); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0442\u0430\u043a\u043e\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">label:100 pointer:0x562316fb2000 label:200 pointer:0x562316fc1008 label:300 pointer:0x562316fb2008<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u0412\u0437\u044f\u0442\u0438\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 <del>\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439<\/del> \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439<\/li>\n<li><code>print_pointer()<\/code> \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>label<\/code> \u0432\u0432\u0435\u0434\u0435\u043d\u0430 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u043d\u044b\u043c \u043b\u0438\u0441\u0442\u0438\u043d\u0433\u043e\u043c<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440, \u0431\u043b\u0430\u0433\u043e \u0447\u0443\u0434\u0435\u0441\u043d\u044b\u0439 play.rust-lang.org \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    push    rax     lea rsi, [rip + playground::STATIC_POINT]     mov edi, 100     call    playground::print_pointer     lea rsi, [rip + playground::MUT_STATIC_POINT]     mov edi, 200     call    playground::print_pointer     lea rsi, [rip + .L__unnamed_2]     mov edi, 300     pop rax     jmp playground::print_pointer<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0443 \u0442.\u0435. \u0432\u0440\u043e\u0434\u0435 \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0437\u0430\u0432\u0435\u0434\u0435\u043d\u044b \u043f\u043e\u0434 \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0421\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0430\u0434\u0440\u0435\u0441 const-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (<code>L__unnamed_2<\/code>) \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u0435\u043d? \u041a\u0430\u043a \u0432\u043e\u0434\u0438\u0442\u0441\u044f, \u0432\u043d\u044f\u0442\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u044b \u043d\u0430\u0439\u0434\u0435\u043c \u0432 &#171;<a href=\"https:\/\/doc.rust-lang.org\/1.30.0\/book\/first-edition\/const-and-static.html\" rel=\"nofollow noopener noreferrer\">\u0441\u0442\u0430\u0440\u043e\u043c \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0435<\/a>&#171;:<\/p>\n<p>  <\/p>\n<blockquote><p>More specifically, constants in Rust have no fixed address in memory. This is because <strong>they\u2019re effectively inlined<\/strong> to each place that they\u2019re used. <strong>References to the same constant are not necessarily guaranteed to refer to the same memory address<\/strong> for this reason.<\/p><\/blockquote>\n<p>\u041d\u0435\u0442, \u043d\u0435 \u0441\u0442\u043e\u0438\u0442. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>static<\/code>.<\/p>\n<p>  <\/p>\n<h2 id=\"static-i-constvstraivanie-inlining\">static \u0438 const:: \u0412\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u0435 (Inlining)<\/h2>\n<p>  <\/p>\n<p>&#171;&#8230;they\u2019re effectively inlined&#187; \u2014 \u043e\u0439 \u043b\u0438?<\/p>\n<p>  <\/p>\n<p>\u041b\u044e\u0431\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043f\u043e Rust \u043d\u0430\u0434\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043b\u0438\u0431\u043e \u043a\u0430\u043a \u043d\u0435\u043f\u043e\u043b\u043d\u0443\u044e, \u043b\u0438\u0431\u043e \u043a\u0430\u043a \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0432\u0443\u044e (\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0413\u0451\u0434\u0435\u043b\u044c), \u0430 \u0432 <a href=\"https:\/\/doc.rust-lang.org\/stable\/reference\/\" rel=\"nofollow noopener noreferrer\">The Rust Reference<\/a> \u0430\u0432\u0442\u043e\u0440\u044b \u0441\u0430\u043c\u0438 \u043f\u0438\u0448\u0443\u0442 \u043a\u0440\u0430\u0441\u043d\u044b\u043c\u0438 \u0431\u0443\u043a\u0432\u0430\u043c\u0438:<\/p>\n<p>  <\/p>\n<blockquote><p>Warning: This book is incomplete<\/p><\/blockquote>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0431\u043b\u0430\u0433\u043e, \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0438\u043c\u0435\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0433\u0440\u0443\u0437\u0438\u043c\u0441\u044f \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0442\u044c <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c2c9740ac8d39a518f54e969ecf766e0\" rel=\"nofollow noopener noreferrer\">\u0442\u043e\u043b\u044c\u043a\u043e const<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    let p1 = CONST_POINT;     dbg!(p1);      \/\/ let p2 = STATIC_POINT; \/\/ Err: cannot move out of static item `STATIC_POINT`     \/\/ let p3 = MUT_STATIC_POINT; \/\/ Err: cannot move out of static item `STATIC_POINT`<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440 <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=286a6078fda8635b96430d7f85fcf2f3\" rel=\"nofollow noopener noreferrer\">\u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043a\u043e\u0434\u0430<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    ...     let p1 = CONST_POINT;     print_pointer(200, &amp;p1);     ...      mov ecx, dword ptr [rip + .L__unnamed_2]     mov eax, dword ptr [rip + .L__unnamed_2+4]     mov dword ptr [rsp + 96], ecx     mov dword ptr [rsp + 100], eax     mov edi, 200     lea rsi, [rsp + 96]     call playground::print_pointer<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0443, \u0442\u0430\u043a\u043e\u0435 \u0441\u0435\u0431\u0435 &#171;\u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u0435&#187; \u2014 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0441\u0435 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 <code>ecx<\/code> \u0438 <code>eax<\/code>, \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u0442\u0443\u0434\u0430 \u0432 \u0441\u0442\u0435\u043a (\u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>p1<\/code>). \u0412 \u0447\u0435\u043c \u0434\u0435\u043b\u043e? \u0410, \u0442\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0436\u0438\u043c Debug, \u0430 \u0447\u0442\u043e \u0432 <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=e377510378ff6d417b7f19810818e901\" rel=\"nofollow noopener noreferrer\">Release<\/a>?:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    movabs  rbx, 257698037810     mov qword ptr [rsp + 8], rbx     lea rsi, [rsp + 8]     mov edi, 200     call    playground::print_pointer<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0442\u0443\u0442, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u0435. <code>257698037810<\/code> \u044d\u0442\u043e \u0430\u043a\u043a\u0443\u0440\u0430\u0442 <code>(60_i64 &lt;&lt; 32) + 50<\/code>, \u0442.\u0435. \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f <code>const CONST_POINT: Point = new_point(50, 60)<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u043f\u043e\u0441\u0442\u043e\u0439\u0442\u0435, \u043c\u044b \u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0438 <code>static<\/code> \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c <code>Copy<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">#[derive(Clone, Copy)] struct Point {     x: i32,     y: i32, }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0447\u0442\u043e <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=c8f37ed4f31c8c108e2e8be871b0ca96\" rel=\"nofollow noopener noreferrer\">\u0432\u044b\u0445\u043e\u0434\u0438\u0442<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    ...     let p1 = STATIC_POINT;     print_pointer(200, &amp;p1);     ...     movabs  rax, 85899345930     mov qword ptr [rsp], rax     mov rsi, rsp     mov edi, 200     call playground::print_pointer<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u043e\u0436\u0435 inlining, <code>(20_i64 &lt;&lt; 32) + 10 = 85899345930<\/code> \u043a\u0430\u043a \u0440\u0430\u0437 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 <code>static STATIC_POINT: Point = new_point(10, 20)<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u0440\u043e\u0447\u0435, \u0440\u0430\u0437\u043d\u0438\u0446\u044b, \u0432 \u043f\u043b\u0430\u043d\u0435 &#171;\u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043d\u0438\u044f&#187;, \u043c\u0435\u0436\u0434\u0443 static \u0438 const \u043d\u0430 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0435 \u0432\u0438\u0434\u043d\u043e (\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0441 \u043f\u043e\u043f\u0440\u0430\u0432\u043a\u043e\u0439 \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e <code>Copy<\/code> is-a-must).<\/p>\n<p>  <\/p>\n<h2 id=\"static-i-constizmenyaemost-mutability\">static \u0438 const:: \u0418\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0441\u0442\u044c (Mutability)<\/h2>\n<p>  <\/p>\n<p>\u041a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=11c1a0355fa91cd4e5e68bad195a9f7d\" rel=\"nofollow noopener noreferrer\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>  <\/p>\n<p>\u041d\u0438 \u043e\u0434\u043d\u043e \u0438\u0437 &#171;\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439&#187; \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043e \u0432 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435, \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    \/\/ STATIC_POINT = new_point(1, 2); \/\/ Err: cannot assign to immutable static item `STATIC_POINT`     \/\/ MUT_STATIC_POINT = new_point(1, 2); \/\/ Err: use of mutable static is unsafe and requires unsafe function or block     \/\/ CONST_POINT = new_point(1, 2); \/\/ Err: invalid left-hand side of assignment<\/code><\/pre>\n<p>  <\/p>\n<p><code>MUT_STATIC_POINT<\/code> \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432 unsafe:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    unsafe {         MUT_STATIC_POINT.x += 2;         MUT_STATIC_POINT.y += 4;     }     unsafe {       let p_point = &amp;MUT_STATIC_POINT as *const _ as *mut Point;       (*p_point).x += 1;     }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li><code>static mut<\/code> \u044f\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043a\u0430\u043a\u043e\u0432\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043b\u043e, \u0430 \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0432\u0435\u0449\u0430\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e, \u043e\u0433\u043e\u0440\u0430\u0436\u0438\u0432\u0430\u044f \u0438 \u043f\u043e\u043c\u0435\u0447\u0430\u044f \u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0443\u0447\u0430\u0441\u0442\u043a\u0438 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>unsafe<\/code>.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041f\u043e\u043f\u044b\u0442\u043a\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c <code>STATIC_POINT<\/code> \u0438\u043b\u0438 <code>CONST_POINT<\/code> \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0443\u0436\u0430\u0441\u0430\u043c\u0438 Segmentation fault:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    \/\/ unsafe {     \/\/   let p_point = &amp;STATIC_POINT as *const _ as *mut Point;     \/\/   (*p_point).x += 1;     \/\/ }     \/\/ unsafe {     \/\/   let p_point = &amp;CONST_POINT as *const _ as *mut Point;     \/\/   (*p_point).x += 1;     \/\/ }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"static-i-constinicializaciya\">static \u0438 const:: \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<p>\u041d\u0443 \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u0431\u0449\u0438\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445\u2026 \u044d\u044d\u044d\u044d\u2026 &#171;\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445&#187; \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0438\u0445 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0435, \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0441 \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c <code>const<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">const fn new_point(x: i32, y: i32) -> Point {     return Point { x: x, y: y }; }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f &#171;\u0447\u0438\u0441\u0442\u044b\u043c\u0438&#187; (pure), \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438\u0445 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043c\u0435\u043d\u0435\u043d \u0435\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c<\/li>\n<li>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432 &#171;\u043a\u0443\u0447\u0443&#187; \u043b\u0435\u0437\u0442\u044c \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0435\u043b\u044c\u0437\u044f<\/li>\n<\/ul>\n<p>  <\/p>\n<h2 id=\"static-i-constdestruktor\">static \u0438 const:: \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/h2>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u043e\u0435 \u0441\u0442\u0430\u0440\u043e\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e, \u043c\u0435\u0442\u043e\u0434 <code>Drop::drop()<\/code> \u0434\u043b\u044f <code>static<\/code> \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d, \u043d\u043e \u043e\u043d \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<h2 id=\"static-i-constchto-ispolzovat\">static \u0438 const:: \u0427\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c?<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u0434\u0430\u0435\u0442 <a href=\"https:\/\/github.com\/rust-lang\/rfcs\/blob\/master\/text\/0246-const-vs-static.md\" rel=\"nofollow noopener noreferrer\">RFC-0246<\/a>:<\/p>\n<p>  <\/p>\n<ul>\n<li><strong>constants<\/strong> declare constant values. These represent a value, not a memory address. This is the most common thing one would reach for and would replace static as we know it today in almost all cases.<\/li>\n<li><strong>statics<\/strong> declare global variables. These represent a memory address. They would be rarely used: the primary use cases are global locks, global atomic counters, and interfacing with legacy C libraries.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e: \u0442\u0438\u043f unit, unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0442\u0438\u043f\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<p>  <\/p>\n<h1 id=\"tip-unit\">\u0422\u0438\u043f <code>unit<\/code><\/h1>\n<p>  <\/p>\n<blockquote><p>In the area of mathematical logic and computer science known as type theory, a <strong>unit<\/strong> type is a type that allows only one value (and thus can hold no information).<br \/>  \u2026<br \/>  In Haskell and Rust, the unit type is called () and its only value is also (), reflecting the 0-tuple interpretation.<br \/>  \u2026<br \/>  In Java, the unit type is called Void and its only value is null.<br \/>  \u2026<br \/>  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unit_type\" rel=\"nofollow noopener noreferrer\">Unit type<\/a><\/p><\/blockquote>\n<p>\u0418\u0442\u0430\u043a, \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0442\u0435\u0436 (tuple) <code>()<\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 \u0442\u0438\u043f, \u0438 \u043b\u0438\u0442\u0435\u0440\u0430\u043b (\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435). \u041c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn no_result() { }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u043b\u0438 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn no_result() {     return () }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u043b\u0438 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn no_result() -> () {     return () }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li><code>unit<\/code> \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 <code>void<\/code> \u0432 <code>C<\/code>, \u043d\u043e \u0442\u0430\u043c \u0443 <code>void<\/code> \u043d\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f<\/li>\n<li>\u0417\u0430\u043f\u0438\u0441\u044c <code>HashMap&lt;T, ()><\/code> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043d\u0435\u0441\u0443\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0438 (\u043a\u0430\u043a \u0431\u044b <code>HashSet<\/code>)<\/li>\n<\/ul>\n<p>  <\/p>\n<h1 id=\"unit-like-struktury\">Unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/h1>\n<p>  <\/p>\n<p>Unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043a\u0430\u043a \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438 \u0442\u0438\u043f, \u0438 \u043b\u0438\u0442\u0435\u0440\u0430\u043b:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct MyUnitStruct; static MY_UNIT_STRUCT1: MyUnitStruct = MyUnitStruct; static MY_UNIT_STRUCT2: MyUnitStruct = MyUnitStruct;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0435\u0449\u0435 <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/alloc.rs#L130\" rel=\"nofollow noopener noreferrer\">std::alloc::System<\/a> (\u043c\u044b \u043f\u043e\u0442\u0438\u0445\u043e\u043d\u044c\u043a\u0443 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u043a \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0443) \u0438 <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0eed0f8a68f712e9b14a688635139306\" rel=\"nofollow noopener noreferrer\">\u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u0438\u0445 \u0430\u0434\u0440\u0435\u0441\u0430<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    \/\/ println!(\"&amp;MyUnitStruct: {:p}\", &amp;MyUnitStruct);         \/\/ println!(\"&amp;std::alloc::System: {:p}\", &amp;std::alloc::System);      print_pointer(100, &amp;MyUnitStruct);     print_pointer(200, &amp;std::alloc::System);      print_pointer(300, &amp;MY_UNIT_STRUCT1);     print_pointer(400,  &amp;MY_UNIT_STRUCT2); <\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u041d\u0443 \u0438 \u0434\u0435\u043b\u0430, \u043e\u043d\u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u044b!<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0440\u0430\u0441\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>\/\/ println...<\/code>, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0430\u0434\u0440\u0435\u0441\u043e\u0432<\/li>\n<li>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>static<\/code> <\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0410\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440 \u043d\u0430\u043c\u0435\u043a\u0430\u0435\u0442, \u0447\u0442\u043e <code>MyUnitStruct<\/code> \u0438 <code>System<\/code> \u0442\u0440\u0430\u043a\u0442\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a <code>const<\/code>, \u0430 <code>MY_UNIT_STRUCT*<\/code> \u2014 \u043a\u0430\u043a <code>static<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    mov edi, 100     lea rsi, [rip + .L__unnamed_2]     call    playground::print_pointer     mov edi, 200     lea rsi, [rip + .L__unnamed_2]     call    playground::print_pointer     mov edi, 300     lea rsi, [rip + playground::MY_UNIT_STRUCT1]     call    playground::print_pointer     mov edi, 400     lea rsi, [rip + playground::MY_UNIT_STRUCT2]     call    playground::print_pointer<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0432\u0441\u0435, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043d\u043e \u043a\u0430\u043a\u043e\u0432\u043e \u0436\u0435 <a href=\"https:\/\/www.youtube.com\/watch?v=MaDKkV9tB6w&amp;t=292s\" rel=\"nofollow noopener noreferrer\">\u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435<\/a>? \u0415\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438, \u0438 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435.<\/p>\n<p>  <\/p>\n<h1 id=\"znacheniya-parametrov-tipa-po-umolchaniyu\">\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0442\u0438\u043f\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/h1>\n<p>  <\/p>\n<p>\u041c\u044b \u043f\u043e\u0447\u0442\u0438 \u0443 \u0446\u0435\u043b\u0438, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 <code>start()<\/code>, <code>stop()<\/code>, <code>handle_request()<\/code> \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=99aad98185ebf5438c532324a02cf9c1\" rel=\"nofollow noopener noreferrer\">\u0435\u0433\u043e \u0442\u0430\u043a<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Server{}  impl Server{     fn start(&amp;self) {         println!(\"Server started\");     } ...     fn run_server(s: &amp;Server){     s.start();     s.handle_request(\"req1\");     s.handle_request(\"req2\");     s.stop(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e <code>println!()<\/code> \u043d\u0430\u0434\u043e \u0431\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u0430\u043c\u0438 \u043d\u0435\u043a\u043e\u0435\u0433\u043e \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043b\u043e\u0433\u0433\u0435\u0440\u0430, \u0438 <code>Server<\/code> \u0443 \u043d\u0430\u0441 \u0442\u0435\u043f\u0435\u0440\u044c <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b86bf41e743a63ba1d00fdf09f5dbc58\" rel=\"nofollow noopener noreferrer\">\u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">trait Logger {     fn info(&amp;self, msg: &amp;str); }  struct Server&lt;L: Logger> {     logger: L, }  impl&lt;L: Logger> Server&lt;L> { ... fn run_server&lt;L: Logger>(s: &amp;Server&lt;L>) {     s.start();     s.handle_request(\"req1\");     s.handle_request(\"req2\");     s.stop(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0438 \u043a\u043b\u0430\u0441\u0441 <code>Server<\/code>, \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0438 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442, \u0442.\u0435. \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>run_server()<\/code>. \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043c\u043d\u043e\u0433\u043e, \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0438\u0445 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0442\u0438\u043f\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u0441\u043f\u0435\u0440\u0432\u0430 \u043d\u0430\u0434\u043e \u0433\u0434\u0435-\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e Logger. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u0436\u0435\u043b\u0430\u043d\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a &#171;\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c&#187; \u043b\u044e\u0431\u0443\u044e unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4ebcc0c1a33007769ab2f5dd702c83e0\" rel=\"nofollow noopener noreferrer\">\u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Logger<\/a> \u0434\u043b\u044f std::alloc::System:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">impl Logger for std::alloc::System {     fn info(&amp;self, msg: &amp;str) {         println!(\"INFO: {}\", msg);     } }  fn main() {     std::alloc::System.info(\"Hello\"); }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>&#171;\u041d\u0430\u0432\u0435\u0441\u0438\u0442\u044c&#187; \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043d\u0430 \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e, \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 &#171;\u0441\u0432\u043e\u0435&#187;<\/li>\n<li>\u041a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 \u2014 \u043e\u043d \u0443\u0436\u0435 \u0435\u0441\u0442\u044c (\u043a\u0430\u043a \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u043e\u0434\u0438\u043d \u043d\u0430 \u0432\u0441\u0435 unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b) \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 (<code>&amp;self<\/code>)<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c System \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d4e46ec67f631ed65280504c80ae6d75\" rel=\"nofollow noopener noreferrer\">\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 \u0442\u0438\u043f\u0430<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Server&lt;L: Logger = std::alloc::System> {     logger: L, }  impl&lt;L: Logger> Server&lt;L> {     fn start(&amp;self) {         self.logger.info(\"Server started\");         ... fn run_server(s: &amp;Server) {     s.start();     s.handle_request(\"req1\");     s.handle_request(\"req2\");     s.stop(); } fn main() {     let s = Server {         logger: std::alloc::System,     };     run_server(&amp;s); }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>voila \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>run_server()<\/code> \u0432\u0435\u0440\u043d\u0443\u043b\u0430\u0441\u044c \u043a \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443, \u0447\u0442\u043e \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c<\/li>\n<li><strong>PS<\/strong>: <a href=\"https:\/\/habr.com\/ru\/users\/darkeld3r\/\" class=\"user_link\">DarkEld3r<\/a> \u0434\u0430\u043b \u0446\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 &#171;\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e&#187; \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0773b8d8c4792a0187f0377822919309\" rel=\"nofollow noopener noreferrer\">\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/a> <code>struct Server&lt;L: Logger = MyLogger><\/code><\/li>\n<\/ul>\n<p>  <\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=sP_myq8HZ9c&amp;t=658s\" rel=\"nofollow noopener noreferrer\">\u041c\u043e\u0436\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u0435\u043d\u0438\u0435<\/a>, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u043a\u0430\u043a &#171;\u0412\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438&#187; (Dependency Injection), \u0442.\u0435. \u043a\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    let s = Server {         logger: std::alloc::System,     };<\/code><\/pre>\n<p>  <\/p>\n<p>\u2026 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432 \u0440\u043e\u043b\u0438 &#171;\u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0446\u0430&#187; \u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0442\u0438\u043f\u0430 <code>Server<\/code> \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c <code>Logger<\/code>. \u041d\u0435\u0442, \u0437\u0430 \u043a\u0443\u043b\u0438\u0441\u0430\u043c\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u0435 &#171;\u043a\u043b\u0430\u0441\u0441\u044b&#187; <code>Server<\/code> \u043f\u043e\u0434 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>Logger<\/code>.<\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u0432\u0430\u0436\u0435\u043d, \u0434\u043b\u044f \u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5f5baae0a8e7c7cbbbf235e2c1ac249a\" rel=\"nofollow noopener noreferrer\">\u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u043b\u043e\u0433\u0433\u0435\u0440<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct MyLogger {}  impl Logger for MyLogger {     fn info(&amp;self, msg: &amp;str) {         println!(\"MyLogger-INFO: {}\", msg);     } } ... fn main() {     let s1 = Server {         logger: std::alloc::System,     };     run_server(&amp;s1);      let s2 = Server {         logger: MyLogger{}     };     run_server(&amp;s2);<\/code><\/pre>\n<p>  <\/p>\n<p>\u2026 \u0438 \u0437\u0430\u0433\u043b\u044f\u043d\u0435\u043c \u0437\u0430 \u043a\u0443\u043b\u0438\u0441\u044b:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">playground::Server&lt;L>::start:     push    rax     mov qword ptr [rsp], rdi     lea rsi, [rip + .L__unnamed_4]     mov edx, 14     call    &lt;playground::MyLogger as playground::Logger>::info     pop rax     ret  playground::Server&lt;L>::start:     push    rax     mov qword ptr [rsp], rdi     lea rsi, [rip + .L__unnamed_4]     mov edx, 14     call    &lt;std::alloc::System as playground::Logger>::info     pop rax     ret<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u041d\u0430\u043b\u0438\u0446\u043e \u0434\u0432\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 <del>\u043a\u043b\u0430\u0441\u0441\u0430<\/del> \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u044b <code>Server<\/code>, \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u043b\u043e\u0433\u0433\u0435\u0440\u0430<\/li>\n<li>\u041d\u0438\u043a\u0430\u043a\u043e\u0433\u043e DI, \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#171;\u043a\u043b\u0430\u0441\u0441\u0430&#187; <code>Server<\/code> \u0442\u043e\u0447\u043d\u043e &#171;\u0437\u043d\u0430\u0435\u0442&#187;, \u0441 \u043a\u0430\u043a\u0438\u043c \u043b\u043e\u0433\u0433\u0435\u0440\u043e\u043c \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/li>\n<li>\u0422\u0430\u043a\u0430\u044f &#171;\u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430&#187; \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0432\u044b\u0437\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 vtable, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b &#171;\u0440\u0430\u0437\u0434\u0443\u0432\u0430\u0435\u0442\u0441\u044f&#187; \u0432 \u0440\u0430\u0437\u043c\u0435\u0440\u0435<\/li>\n<\/ul>\n<p>  <\/p>\n<h1 id=\"zamena-globalnogo-allokatora\">\u0417\u0430\u043c\u0435\u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430<\/h1>\n<p>  <\/p>\n<p>\u0417\u0430\u043c\u0435\u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/alloc.rs#L10\" rel=\"nofollow noopener noreferrer\">\u0437\u0434\u0435\u0441\u044c<\/a>, \u0442\u0430\u043a <a href=\"https:\/\/play.rust-lang.org\/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4d75cfd0d61dadb4aa3651436010ac83\" rel=\"nofollow noopener noreferrer\">\u0437\u0430\u043c\u0435\u043d\u0438\u043c<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Counter; ... unsafe impl GlobalAlloc for Counter {     unsafe fn alloc(&amp;self, layout: Layout) -> *mut u8 {         let ret = System.alloc(layout);         ... }  #[global_allocator] static A: Counter = Counter;<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u0412 \u043e\u0431\u0449\u0435\u043c, \u043d\u0430\u0434\u043e \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>#[global_allocator]<\/code> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c trait GlobalAlloc<\/li>\n<li>\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u043c\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>GlobalAlloc<\/code> \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u043a <code>std::alloc::System<\/code> \u0438 \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u0432\u0435\u0434\u0435\u0442 \u0441\u0447\u0435\u0442\u0447\u0438\u043a<\/li>\n<li>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 <code>#[global_allocator]<\/code> \u043f\u0435\u0440\u0435\u0434 <code>struct Counter<\/code> \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043e\u0448\u0438\u0431\u043a\u0435 <code>allocators must be statics<\/code>, \u0442.\u0435. \u0442\u0430\u043a\u0438 <code>Counter<\/code> \u044d\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430. \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435<\/li>\n<\/ul>\n<p>  <\/p>\n<p><code>System.alloc()<\/code>, \u0433\u0434\u0435 \u044d\u0442\u043e \u0438 \u0447\u0442\u043e \u044d\u0442\u043e? \u041f\u0440\u043e \u0441\u0430\u043c <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/alloc.rs#L130\" rel=\"nofollow noopener noreferrer\">std::alloc::System<\/a> \u043c\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u043c, \u044d\u0442\u043e <code>pub struct System;<\/code> \u0430 \u043e\u0442\u043a\u0443\u0434\u0430 \u0432\u0437\u044f\u043b\u043e\u0441\u044c <code>System.alloc()<\/code>? \u0412\u043e\u043f\u0440\u043e\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439, \u043a\u0430\u043a \u0443\u0436\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043d\u0430\u0432\u0435\u0441\u0438\u0442\u044c <code>alloc()<\/code> \u043d\u0430 unit-like \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>System<\/code> \u043c\u043e\u0436\u043d\u043e \u0433\u0434\u0435 \u0443\u0433\u043e\u0434\u043d\u043e (\u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438).<\/p>\n<p>  <\/p>\n<p>\u0427\u0435\u0440\u0435\u0437 <code>std::alloc::System<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/core\/src\/alloc\/global.rs#L123\" rel=\"nofollow noopener noreferrer\">std::alloc::GlobalAlloc<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">pub unsafe trait GlobalAlloc {     ...     unsafe fn alloc(&amp;self, layout: Layout) -> *mut u8;     ...     unsafe fn dealloc(&amp;self, ptr: *mut u8, layout: Layout);     ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b, &#171;\u0443\u0441\u043b\u043e\u0432\u043d\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f&#187;, \u0432\u0438\u0434\u0438\u043c\u043e, <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/sys\/mod.rs#L31\" rel=\"nofollow noopener noreferrer\">\u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0434\u0435\u0441\u044c<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">cfg_if::cfg_if! {     if #[cfg(unix)] {         mod unix;         pub use self::unix::*;     } else if #[cfg(windows)] {         mod windows;         pub use self::windows::*;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/sys\/windows\/alloc.rs#L191\" rel=\"nofollow noopener noreferrer\">\u0434\u043b\u044f windows<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">unsafe impl GlobalAlloc for System {     #[inline]     unsafe fn alloc(&amp;self, layout: Layout) -> *mut u8 {         \/\/ SAFETY: Pointers returned by `allocate` satisfy the guarantees of `System`         let zeroed = false;         unsafe { allocate(layout, zeroed) }     }     ...<\/code><\/pre>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/sys\/windows\/alloc.rs#L134\" rel=\"nofollow noopener noreferrer\">allocate()<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">extern \"system\" {     ...     \/\/ See https:\/\/docs.microsoft.com\/windows\/win32\/api\/heapapi\/nf-heapapi-heapalloc     fn HeapAlloc(hHeap: c::HANDLE, dwFlags: c::DWORD, dwBytes: c::SIZE_T) -> c::LPVOID;     ... } unsafe fn allocate(layout: Layout, zeroed: bool) -> *mut u8 {     ...     if layout.align() &lt;= MIN_ALIGN {         \/\/ SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.         \/\/ The returned pointer points to the start of an allocated block.         unsafe { HeapAlloc(heap, flags, layout.size()) as *mut u8 }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u041d\u0443 \u0432\u0441\u0435, \u0434\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c \u0434\u043e \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 <code>std::alloc::alloc()<\/code> \u0438\u0437 <a href=\"https:\/\/habr.com\/ru\/post\/573422\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0435\u0440\u0438\u0438<\/a>.<\/p>\n<p>  <\/p>\n<h1 id=\"stdallocalloc\">std::alloc::alloc()<\/h1>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/alloc\/src\/alloc.rs#L85\" rel=\"nofollow noopener noreferrer\">std::alloc::alloc()<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">\/\/\/ This function is expected to be deprecated in favor of the `alloc` method \/\/\/ of the [`Global`] type when it and the [`Allocator`] trait become stable. ... pub unsafe fn alloc(layout: Layout) -> *mut u8 {     unsafe { __rust_alloc(layout.size(), layout.align()) } }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>&#171;&#8230;is expected to be deprecated in favor of Global and Allocator&#187; \u2014 \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u043c \u044d\u0442\u043e<\/li>\n<\/ul>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/alloc\/src\/alloc.rs#L30\" rel=\"nofollow noopener noreferrer\">__rust_alloc()<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    \/\/ These are the magic symbols to call the global allocator.  rustc generates     \/\/ them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute     \/\/ (the code expanding that attribute macro generates those functions), or to call     \/\/ the default implementations in libstd (`__rdl_alloc` etc. in `library\/std\/src\/alloc.rs`)     \/\/ otherwise. ...     fn __rust_alloc(size: usize, align: usize) -> *mut u8;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421 <code>#[global_allocator]<\/code> \u0432\u0441\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0438\u0434\u0435\u043c \u0432 <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/eaf6f463599df1f18da94a6965e216ea15795417\/library\/std\/src\/alloc.rs#L350\" rel=\"nofollow noopener noreferrer\">__rdl_alloc()<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">    pub unsafe extern \"C\" fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {         \/\/ SAFETY: see the guarantees expected by `Layout::from_size_align` and         \/\/ `GlobalAlloc::alloc`.         unsafe {             let layout = Layout::from_size_align_unchecked(size, align);             System.alloc(layout)         }     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0435, \u0434\u043e\u043f\u043b\u044b\u043b\u0438 \u0434\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u043e\u0433\u043e <code>System.alloc()<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0415\u0449\u0435 \u0433\u043b\u044f\u043d\u0435\u043c <code>RawVec<\/code>, \u0438 \u043d\u0430 \u044d\u0442\u043e\u043c \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c.<\/p>\n<p>  <\/p>\n<h1 id=\"allokator-v-rawvec\">\u0410\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440 \u0432 RawVec<\/h1>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/996ff2e0a0f911f52bb1de6bdd0cfd5704de1fc9\/library\/alloc\/src\/raw_vec.rs#L53\" rel=\"nofollow noopener noreferrer\">RawVec<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">pub struct RawVec&lt;T, A: Allocator = Global> { ...<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u041d\u0435 \u0437\u0440\u044f \u0443\u0447\u0438\u043b\u0438 \u043c\u0430\u0442\u0447\u0430\u0441\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441 \u0447\u0443\u0432\u0441\u0442\u0432\u043e\u043c \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438\u0441\u043a\u0430\u0442\u044c <code>trait Allocator<\/code>, <code>struct Global;<\/code> \u0438 <code>impl Allocator for Global<\/code><\/li>\n<li><strong>Nota Bene<\/strong>: \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0438\u043c \u0438 \u0437\u0430\u043c\u0435\u043d\u044f\u0442 <code>std::alloc::alloc()<\/code><\/li>\n<\/ul>\n<p>  <\/p>\n<p><code>trait<\/code> <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/effea9a2a0d501db5722d507690a1a66236933bf\/library\/core\/src\/alloc\/mod.rs#L100\" rel=\"nofollow noopener noreferrer\">std::alloc::Allocator<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">pub unsafe trait Allocator { ...     fn allocate(&amp;self, layout: Layout) -> Result&lt;NonNull&lt;[u8]>, AllocError>;     ...     fn allocate_zeroed(&amp;self, layout: Layout) -> Result&lt;NonNull&lt;[u8]>, AllocError> {     ...<\/code><\/pre>\n<p>  <\/p>\n<p><code>struct<\/code> <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/a49e38e672c60da788360e088f00ad12353e3913\/library\/alloc\/src\/alloc.rs#L50\" rel=\"nofollow noopener noreferrer\">std::alloc::Global<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">\/\/\/ This type implements the [`Allocator`] trait by forwarding calls \/\/\/ to the allocator registered with the `#[global_allocator]` attribute ... pub struct Global;<\/code><\/pre>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/a49e38e672c60da788360e088f00ad12353e3913\/library\/alloc\/src\/alloc.rs#L223\" rel=\"nofollow noopener noreferrer\">impl Allocator for Global<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">unsafe impl Allocator for Global {     fn allocate(&amp;self, layout: Layout) -> Result&lt;NonNull&lt;[u8]>, AllocError> {         self.alloc_impl(layout, false)     }     ...<\/code><\/pre>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/a49e38e672c60da788360e088f00ad12353e3913\/library\/alloc\/src\/alloc.rs#L161\" rel=\"nofollow noopener noreferrer\">Global.alloc_impl()<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">impl Global { ...     fn alloc_impl(&amp;self, layout: Layout, zeroed: bool) -> Result&lt;NonNull&lt;[u8]>, AllocError> {         ...         let raw_ptr = if zeroed { alloc_zeroed(layout) } else { alloc(layout) };<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u0439\u0434\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0432\u0435\u0442\u043a\u0443 \u0441 <a href=\"https:\/\/github.com\/rust-lang\/rust\/blob\/a49e38e672c60da788360e088f00ad12353e3913\/library\/alloc\/src\/alloc.rs#L85\" rel=\"nofollow noopener noreferrer\">alloc(layout)<\/a>:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">pub unsafe fn alloc(layout: Layout) -> *mut u8 {     unsafe { __rust_alloc(layout.size(), layout.align()) } }<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u0410 <code>__rust_alloc()<\/code> \u0443\u0436\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043b\u0438, \u0443\u0444<\/li>\n<\/ul>\n<p>  <\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=k73BvJhiSfg&amp;t=51s\" rel=\"nofollow noopener noreferrer\">\u041d\u0435 \u043f\u0440\u043e\u0449\u0430\u044e\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<p><strong>\u0414\u0430\u043b\u0435\u0435<\/strong>: <a href=\"https:\/\/habr.com\/ru\/post\/588917\/\">\u0417\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/574522\/\"> https:\/\/habr.com\/ru\/articles\/574522\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043a\u0443\u0447\u0435\u0439, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u0432\u043e\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440. \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a, \u043d\u043e \u043d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c \u2014 \u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e, \u0441 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u043c \u043f\u0440\u043e\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0435\u043c \u0432 \u0441\u0443\u0442\u044c \u044f\u0432\u043b\u0435\u043d\u0438\u0439, \u0442\u0430\u043a \u0447\u0442\u043e \u043b\u0435\u0433\u043a\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 (\u043a\u0430\u043a \u0438 \u043e\u0431\u0435\u0449\u0430\u043b).<\/p>\n<p>  <\/p>\n<ul>\n<li><strong>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c<\/strong>: <a href=\"https:\/\/habr.com\/ru\/post\/573422\/\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043a\u0443\u0447\u0435\u0439 \u0432 Rust<\/a><\/li>\n<li><strong>\u041d\u0430\u0447\u0430\u043b\u043e \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/strong>: <a href=\"https:\/\/habr.com\/ru\/post\/566668\/\">\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435<\/a><\/li>\n<\/ul>\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-412400","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/412400","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=412400"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/412400\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}