{"id":341301,"date":"2022-11-15T21:00:06","date_gmt":"2022-11-15T21:00:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=341301"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=341301","title":{"rendered":"<span>\u041f\u0440\u043e\u0441\u0442\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0439 \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>\u0423 \u0432\u0430\u0441 \u0431\u044b\u0432\u0430\u043b\u043e \u0442\u0430\u043a\u043e\u0435, \u0447\u0442\u043e \u0432\u044b \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0441 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u043c\u0438 \u0432 Rust? \u0423\u0436\u0435 \u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b <code>Fn<\/code>-\u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u043b\u0438, \u0438 <code>move<\/code> \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0432\u0435\u0437\u0434\u0435, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e, \u0430 borrow checker \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043d\u0435 \u0443\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f? \u0418 \u0442\u0443\u0442 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f! \u0421\u043b\u043e\u0436\u043d\u043e \u0438 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e. \u0414\u0443\u0440\u0430\u0446\u043a\u0438\u0439 \u043f\u0440\u0438\u0432\u0435\u0440\u0435\u0434\u0430 Rust.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438 \u043d\u0430 \u0447\u0442\u043e \u0432\u043b\u0438\u044f\u0435\u0442 <code>move<\/code>, \u0430 \u043d\u0430 \u0447\u0442\u043e \u2014 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u041d\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u044f \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u044e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u043e\u0432, \u043d\u043e \u0438 \u0443 \u0432\u043f\u043e\u043b\u043d\u0435 \u0437\u0440\u0435\u043b\u044b\u0445. \u0425\u0443\u0436\u0435 \u0442\u043e\u0433\u043e, \u0435\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u0442\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0418\u0442\u0430\u043a, \u043a\u043b\u044e\u0447 \u043a \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u2014 \u044d\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u043a\u0430\u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430. \u041f\u0440\u0438\u0447\u0435\u043c \u0437\u0430\u0445\u0432\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043f\u043e\u043b\u044f\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0430 \u0442\u0435\u043b\u043e \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0442\u0435\u043b\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430 (\u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0442\u0440\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445: <code>Fn::call<\/code>, <code>FnMut::call_mut<\/code>, <code>FnOnce::call_once<\/code>).<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn new_closure(a: i32) -> impl Fn(i32) -> i32 {     move |x| a * x }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 <code>Fn<\/code>, \u043e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>move<\/code> \u043f\u0435\u0440\u0435\u0434 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e <code>move<\/code> \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0434\u043b\u044f <code>FnOnce<\/code>-\u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0439. \u041e\u0448\u0438\u0431\u043a\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <strong><code>move<\/code> \u043e\u0442\u043d\u043e\u0441\u044f\u0442 \u043d\u0435 \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0443 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0432 \u043d\u0435\u043c, \u0430 \u043a \u0441\u043f\u043e\u0441\u043e\u0431\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0435\u043b\u0430 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f<\/strong>. \u0422\u043e \u0435\u0441\u0442\u044c, <code>move<\/code> \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0442\u043e, \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442 \u043b\u0438 \u0441\u0430\u043c\u043e \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 (\u043d\u0435 \u0435\u0433\u043e \u0442\u0435\u043b\u043e, \u0430 \u0435\u0433\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430!) \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0432\u044b\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a\u0438\u043c \u043f\u0441\u0435\u0432\u0434\u043e\u043a\u043e\u0434\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">\/\/ \u0414\u043b\u044f `move |x| a * x`  struct Closure {     a: i32 }  impl Fn&lt;i32> for Closure {     type Output = i32;      fn call(&amp;self, x: i32) -> F::Output {         self.a * x     } }<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"rust\">\/\/ \u0414\u043b\u044f `|x| a * x`  struct Closure&lt;'a> {     a: &amp;'a i32 }  impl Fn&lt;i32> for Closure&lt;'_> {     type Output = i32;      fn call(&amp;self, x: i32) -> F::Output {         self.a * x     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0442\u0435\u043b\u043e \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438 <code>FnOnce<\/code>-\u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u043d\u0435 \u0432\u043b\u0430\u0434\u0435\u0442\u044c \u0441\u0432\u043e\u0438\u043c \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435\u043c, \u0430 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn map(x: usize, fun: impl FnOnce(usize) -> usize) -> usize {     fun(x) }  let msg = String::from(\"hello\");  let product = |x| msg.len() * x; \/\/ \u0417\u0430\u0438\u043c\u0441\u0442\u0432\u0443\u0435\u0442 `msg`  let b = map(7, product); println!(\"{msg} {b}\");<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">hello 35<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u043e \u0442\u0430\u043a\u043e\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn new_closure(msg: String) -> impl FnOnce(usize) -> usize {     |x| msg.len() * x }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">error[E0373]: closure may outlive the current function, but it borrows `msg`, which is owned by the current function  --> src\/main.rs:2:5   | 2 |     |x| msg.len() * x   |     ^^^ --- `msg` is borrowed here   |     |   |     may outlive borrowed value `msg`   | note: closure is returned here  --> src\/main.rs:2:5   | 2 |     |x| msg.len() * x   |     ^^^^^^^^^^^^^^^^^ help: to force the closure to take ownership of `msg` (and any other referenced variables), use the `move` keyword   | 2 |     move |x| msg.len() * x   |     ++++<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Closure&lt;'a> {     msg: &amp;'a String }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u043c\u0435\u0435\u0442 \u043b\u0430\u0439\u0444\u0442\u0430\u0439\u043c \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0436\u0438\u0437\u043d\u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>msg<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u0442\u0435\u043b\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0417\u043d\u0430\u0447\u0438\u0442 \u0441\u0430\u043c\u043e \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0436\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043e \u0438\u0437 \u043d\u0435\u0435. \u041d\u0443\u0436\u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0432\u0438\u0434\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Closure {     msg: String }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u043e\u0432\u0430 <code>move<\/code>, \u043a\u0430\u043a \u0441\u043e\u0432\u0435\u0442\u0443\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440, \u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0438\u043d\u0430\u0447\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn new_closure(msg: String) -> impl FnOnce(usize) -> usize {     |x| msg.into_bytes().len() * x }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432 \u0442\u0435\u043b\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0432\u044b\u0437\u043e\u0432 <code>into_bytes<\/code> \u0437\u0430\u0432\u043b\u0430\u0434\u0435\u0432\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>msg<\/code>, \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0430\u043c \u0434\u043e\u0433\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0435\u0451 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435, \u0430 \u043d\u0435 \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f. \u0421 \u0442\u0440\u0435\u0439\u0442\u043e\u043c <code>Fn<\/code> \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn new_closure(msg: String) -> impl Fn(usize) -> usize {     |x| msg.into_bytes().len() * x }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0448\u0438\u0431\u043a\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">error[E0507]: cannot move out of `msg`, a captured variable in an `Fn` closure  --> src\/main.rs:2:9   | 1 | fn new_closure(msg: String) -> impl Fn(usize) -> usize {   |                --- captured outer variable 2 |     |x| msg.into_bytes().len() * x   |     --- ^^^ ------------ `msg` moved due to this method call   |     |   |   |     |   move occurs because `msg` has type `String`, which does not implement the `Copy` trait   |     captured by this `Fn` closure   | note: this function takes ownership of the receiver `self`, which moves `msg`<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043d\u0430\u0448\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">struct Closure {     msg: String }  impl Fn&lt;usize> for Closure {     type Output = usize;      fn call(&amp;self, x: usize) -> F::Output {         self.msg.into_bytes().len() * x     } }<\/code><\/pre>\n<p>  <\/p>\n<p><code>self<\/code> \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c <code>self.msg<\/code> \u0432\u043d\u0443\u0442\u0440\u044c <code>into_bytes<\/code>. \u0415\u0441\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0433\u043e \u043d\u0435 \u0441\u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn new_closure(msg: String) -> impl Fn(usize) -> usize {     |x| msg.clone().into_bytes().len() * x }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u043e\u043f\u044f\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0430! \u0422\u0435\u043f\u0435\u0440\u044c \u0443\u0436\u0435 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e <code>msg<\/code> \u0441\u0442\u0430\u043b \u0437\u0430\u0438\u043c\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0435\u043b\u0430 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f, \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>msg.clone()<\/code>, \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b \u0435\u0433\u043e \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043a\u0430\u043a \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u043e\u0435 \u043f\u043e\u043b\u0435. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>move<\/code> \u0440\u0443\u043a\u0430\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u043b\u0435 <code>msg<\/code> \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0432\u043b\u0430\u0434\u0435\u044e\u0449\u0438\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"rust\">fn new_closure(msg: String) -> impl Fn(usize) -> usize {     move |x| msg.clone().into_bytes().len() * x }<\/code><\/pre>\n<p>  <\/p>\n<blockquote><p>\u041d\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0443: \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435, \u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"rust\">let msg = String::from(\"hello\"); let c = 25; let product = { let msg = &amp;msg; move |x| msg.len() * x * c };<\/code><\/pre>\n<p>  <\/p><\/blockquote>\n<p>\u0418\u0442\u0430\u043a, \u0447\u0442\u043e \u0432 \u0438\u0442\u043e\u0433\u0435?<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0417\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0432 \u043f\u043e\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f, \u0430 \u0442\u0435\u043b\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0442\u0435\u043b\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 <code>Fn::call<\/code>, <code>FnMut::call_mut<\/code> \u0438\u043b\u0438 <code>FnOnce::call_once<\/code>.<\/li>\n<li>\u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <code>move<\/code> \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f, \u0430 \u043d\u0435 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0432 \u0442\u0435\u043b\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435.<\/li>\n<li>\u0427\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f \u0432 \u0441\u0430\u043c\u043e\u043c \u0442\u0435\u043b\u0435, \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0442\u0435\u043f\u0435\u0440\u044c \u0441 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043c\u043e\u0440\u043e\u043a\u0438. \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0436\u0435 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 <code>async<\/code>-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0431\u043b\u043e\u043a\u0438. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e, \u0440\u0430\u0437\u0440\u0443\u043b\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0432\u0430\u0441 \u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u0435\u043b\u043e\u043c \u0442\u0435\u0445\u043d\u0438\u043a\u0438.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/699596\/\"> https:\/\/habr.com\/ru\/post\/699596\/<\/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>\u0423 \u0432\u0430\u0441 \u0431\u044b\u0432\u0430\u043b\u043e \u0442\u0430\u043a\u043e\u0435, \u0447\u0442\u043e \u0432\u044b \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0441 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u043c\u0438 \u0432 Rust? \u0423\u0436\u0435 \u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b <code>Fn<\/code>-\u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u043b\u0438, \u0438 <code>move<\/code> \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0432\u0435\u0437\u0434\u0435, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e, \u0430 borrow checker \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043d\u0435 \u0443\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f? \u0418 \u0442\u0443\u0442 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f! \u0421\u043b\u043e\u0436\u043d\u043e \u0438 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e. \u0414\u0443\u0440\u0430\u0446\u043a\u0438\u0439 \u043f\u0440\u0438\u0432\u0435\u0440\u0435\u0434\u0430 Rust.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438 \u043d\u0430 \u0447\u0442\u043e \u0432\u043b\u0438\u044f\u0435\u0442 <code>move<\/code>, \u0430 \u043d\u0430 \u0447\u0442\u043e \u2014 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u041d\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u044f \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u044e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u043e\u0432, \u043d\u043e \u0438 \u0443 \u0432\u043f\u043e\u043b\u043d\u0435 \u0437\u0440\u0435\u043b\u044b\u0445. \u0425\u0443\u0436\u0435 \u0442\u043e\u0433\u043e, \u0435\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u0442\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/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-341301","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341301","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=341301"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341301\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}