{"id":364027,"date":"2024-05-21T02:08:38","date_gmt":"2024-05-21T02:08:38","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=364027"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=364027","title":{"rendered":"<span>\u0414\u0430 \u043a\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u044d\u0442\u043e\u0442 \u0432\u0430\u0448 impl Trait<\/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-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u0430 Rust 1.75.0, \u043d\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u043c async trait-\u0430\u043c\u0438 \u0438 return-position\u00a0impl Trait\u00a0in trait, \u043d\u0430\u0434\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <strong>impl Trait<\/strong> \u0438 \u0441 \u0447\u0435\u043c \u0435\u0433\u043e \u0435\u0434\u044f\u0442. <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 <s>\u0431\u0438\u0442\u0431\u043e\u043a\u0441\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u043e\u0432\u044b\u0445 \u0430\u043a\u0440\u043e\u043d\u0438\u043c\u043e\u0432<\/s> \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0437\u0430 \u043d\u0430\u0431\u043e\u0440\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 RPIT, RPITIT \u0438 \u0442.\u0434. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0432 Rust \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435.<\/p>\n<blockquote>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 <a href=\"https:\/\/youtu.be\/CWiz_RtA1Hw?si=FqEmTWlgLnUEcLbk\" rel=\"noopener noreferrer nofollow\">\u0432\u0438\u0434\u0435\u043e<\/a> \u043e\u0442 Jon Gjengset<\/p>\n<\/blockquote>\n<h2>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<ul>\n<li>\n<p><a href=\"#1\" rel=\"noopener noreferrer nofollow\">fn() \u2192 impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\" rel=\"noopener noreferrer nofollow\">fn(impl Trait)<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\" rel=\"noopener noreferrer nofollow\">trait { type = impl Trait }<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\" rel=\"noopener noreferrer nofollow\">type = impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\" rel=\"noopener noreferrer nofollow\">trait { fn() \u2192 impl Trait }<\/a><\/p>\n<\/li>\n<\/ul>\n<h2>fn() \u2192 impl Trait<\/h2>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Return Position Impl Trait (RPIT)<\/p>\n<p><strong>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p>\u0415\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"rust\">fn only_true&lt;I>(iter: I) -> \/* ??? *\/ where I: Iterator&lt;Item = bool> { iter.map(|x| foo(x)).filter(|&amp;x| x) }<\/code><\/pre>\n<p>\u041a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f? \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f <code>std::iter::Filter&lt;std::iter::Map&lt;I, ???>, ???><\/code>. \u0427\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432? \u041b\u0430\u0434\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f, \u0447\u0442\u043e\u0431\u044b \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 <code>Iterator&lt;Item = bool><\/code>. \u0412\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>Box&lt;dyn Iterator&lt;Item=bool>><\/code>. \u041d\u043e \u0442\u0443\u0442 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 + \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p>\u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 <em>\u044d\u043a\u0437\u0438\u0441\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b <\/em>(Existential types). \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">fn only_true&lt;I>(iter: I) -> impl Iterator&lt;Item = bool> \/* Opaque type *\/ where I: Iterator&lt;Item = bool>  { iter.filter(|&amp;x| x) \/* Hidden type *\/ }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f 2 \u043d\u043e\u0432\u044b\u0445 \u0442\u0435\u0440\u043c\u0438\u043d\u0430:<\/p>\n<p><em>Hidden Type<\/em> &#8212; \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439\/\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p><em>Opaque type<\/em> &#8212; \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Hidden Type.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043e\u0442 impl Traits:<\/p>\n<ul>\n<li>\n<p>\u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u0435 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0442\u0438\u043f\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0442 \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 impl Traits<\/strong><\/p>\n<ol>\n<li>\n<p>\u0412\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0438\u043f\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 (\u043f\u0440\u0430\u0432\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u043c):<\/p>\n<pre><code class=\"rust\">fn only_true&lt;I>(iter: I) ->    impl Future&lt;Output=impl Iterator&lt;Item = bool>> \/* Opaque type *\/ where I: Iterator&lt;Item = bool>  { async move { iter.filter(|&amp;x| x) \/* Hidden type *\/ } }<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p>\u0410\u0432\u0442\u043e-\u0442\u0440\u0435\u0439\u0442\u044b<\/p>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e-\u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c Hidden Type \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 &#171;\u0443\u0442\u0435\u0447\u043a\u0430&#187; (leakage):<\/p>\n<pre><code class=\"rust\">fn bar() -> impl Sized {     () }  fn foo() -> impl Sized + Send + Unpin {     bar() }<\/code><\/pre>\n<ol start=\"3\">\n<li>\n<p>\u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 Generic<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"rust\">fn f1&lt;R: Trait>() -> R {}  fn f2() -> impl Trait {}<\/code><\/pre>\n<p>\u0412 <code>f1<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0430\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u0438\u043f.<\/p>\n<p>\u0412 <code>f2<\/code> \u0442\u0435\u043b\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u0438\u043f.<\/p>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a Return Position Impl Trait (RPIT), \u0441\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438. \u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0435\u043b\u044c\u0437\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d \u0436\u0438\u0437\u043d\u0438 \u0432 RPIT. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">\/\/ \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 fn foo(t: &amp;()) -> impl Sized {     t }  \/\/ Ok fn foo&lt;'a>(t: &amp;'a ()) -> impl Sized + 'a {     t }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"rust\">\/\/ \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 fn bar&lt;T>(t: T) -> impl Sized {     () }  fn foo() -> impl Sized + 'static {     let s = String::new();          bar(&amp;s) }<\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0434\u043b\u044f 2021 \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0442\u0440\u044e\u043a:<\/p>\n<pre><code class=\"rust\">trait Captures&lt;U> {} impl&lt;T: ?Sized, U> Captures&lt;U> for T {}  \/\/ \u0414\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043b\u0430\u0439\u0444\u0442\u0430\u0439\u043c\u0430 fn foo&lt;'a>(t: &amp;'a ()) -> impl Sized + Captures&lt;&amp;'a ()> { t }  \/\/ \u0414\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 fn foo&lt;'a, 'b>(x: &amp;'a (), y: &amp;'b ()) -> impl Sized + Captures&lt;(&amp;'a (), &amp;'b ())> {     (x, y) }<\/code><\/pre>\n<p>\u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, <a href=\"https:\/\/github.com\/rust-lang\/rfcs\/pull\/3498\" rel=\"noopener noreferrer nofollow\">\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u043e\u0444\u0438\u043a\u0441\u0438\u043b\u0438<\/a> \u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d \u0436\u0438\u0437\u043d\u0438 \u0432 2024 \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438. <\/p>\n<p>\u041f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438 \u0432 impl Traits \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/rust-lang.github.io\/rfcs\/3498-lifetime-capture-rules-2024.html\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<h2>fn(impl Trait)<\/h2>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Argument Position Impl Trait (APIT)<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443-\u0441\u0430\u0445\u0430\u0440\u043e\u043c \u0434\u043b\u044f <code>&lt;T: Trait>(t: T)<\/code>:<\/p>\n<pre><code class=\"rust\">fn f1&lt;D>(display: D) where D: std::fmt::Display { \/* \u2026 *\/ }  fn f2(display: impl std::fmt::Display) { \/* \u2026 *\/ }<\/code><\/pre>\n<p>\u041e\u0442\u043b\u0438\u0447\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435, \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0442\u0438\u043f\u044b:<\/p>\n<pre><code class=\"rust\">f1::&lt;u32>(1); \/\/ Ok f2(1) \/\/ Ok f2::&lt;u32>(1); \/\/ \u041e\u0448\u0438\u0431\u043a\u0430<\/code><\/pre>\n<h2>trait { type = impl Trait }<\/h2>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Assoc. Type Position Impl Trait (ATPIT) <a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/63063\" rel=\"noopener noreferrer nofollow\">(\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432 nightly)<\/a><\/p>\n<p>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f \u0442\u0430\u043a\u0430\u044f \u0436\u0435 \u043a\u0430\u043a \u0438 \u0443 RPIT. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">struct Odd; impl IntoIterator for Odd {     type IntoIter = impl Iterator&lt;Item = u32>;     fn into_iter(self) -> Self::IntoIter {         (0u32..).filter(|x| x % 2 != 0)     }  }<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u043b\u044f \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d \u0436\u0438\u0437\u043d\u0438 \u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0442\u0438\u043f\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0451 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438:<\/p>\n<pre><code class=\"rust\">impl&lt;'a, T> Trait1 for Type {     type Assoc&lt;'b, U> = impl Trait2; \/\/ Ok }<\/code><\/pre>\n<h2>type = impl Trait<\/h2>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Type Alias Impl Trait (TAIT) <a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/41517\" rel=\"noopener noreferrer nofollow\">(\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432 nightly)<\/a><\/p>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c impl Trait-\u0430 \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445, \u043a\u0440\u043e\u043c\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">type Ready&lt;T> = impl std::future::Future&lt;Output = T>;  fn ready&lt;T>(t: T) -> Ready&lt;T> {     async move { t } }<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. <\/p>\n<h2>trait { fn() \u2192 impl Trait }<\/h2>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Return position impl Trait in Trait (RPITIT)<\/p>\n<p>\u0422\u043e, \u0447\u0442\u043e <a href=\"https:\/\/github.com\/rust-lang\/rust\/pull\/115822\/\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f<\/a> \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 1.75.0. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ATPIT \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 impl Trait, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0444\u0438\u0447\u0430. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0439 \u0444\u0438\u0447\u0438 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0442\u0440\u0435\u0439\u0442\u0430\u0445, \u0442\u0430\u043a \u043a\u0430\u043a <code>async fn () -> ret<\/code> \u0440\u0430\u0432\u043d\u043e\u0441\u0438\u043b\u044c\u043d\u0430 <code>fn () -> impl Future&lt;Output = ret><\/code>. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">impl MyAsyncTrait for MyStruct {     fn foo(&amp;mut self) -> impl Future&lt;Output = u32> {         async { 1 }     }  }<\/code><\/pre>\n<p>\u041d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b.<\/p>\n<pre><code class=\"rust\">trait It {     fn iter(&amp;self) -> impl Iterator&lt;Item = u32>; }  fn twice&lt;I: It>(i: I) where ???: Clone {     let it = i.iter();     it.clone().chain(it); }<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/109417\" rel=\"noopener noreferrer nofollow\">Return Type Notation<\/a> (\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435). \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"rust\">fn twice&lt;I: It&lt;iter(): Clone>>(i: I) {     let it = i.iter();     it.clone().chain(it); }<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 impl Trait \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0432\u0435\u0449\u044c \u0434\u043b\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u043e\u0432\u0435\u0440\u0445\u0435\u0434\u0430 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445. \u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 RPITIT \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u043e\u043b\u0433\u043e, \u043d\u043e \u043e\u043d\u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0435 ATPIT \u0438 TAIT. <\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<ul>\n<li>\n<p><a href=\"https:\/\/rustc-dev-guide.rust-lang.org\/opaque-types-type-alias-impl-trait.html\" rel=\"noopener noreferrer nofollow\">Opaque types<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=CWiz_RtA1Hw\" rel=\"noopener noreferrer nofollow\">Impl Trait aka Look ma\u2019, no generics! by Jon Gjengset<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/rust-lang.github.io\/rfcs\/3498-lifetime-capture-rules-2024.html\" rel=\"noopener noreferrer nofollow\">Lifetime capture rules 2024<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/63063\" rel=\"noopener noreferrer nofollow\">Assoc. Type Position Impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/41517\" rel=\"noopener noreferrer nofollow\">Type Alias Impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/pull\/115822\/\" rel=\"noopener noreferrer nofollow\">Async fn in Traits and Trait { fn() \u2192 impl Trait }<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/109417\" rel=\"noopener noreferrer nofollow\">Return Type Notation<\/a><\/p>\n<\/li>\n<\/ul>\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\/783266\/\"> https:\/\/habr.com\/ru\/articles\/783266\/<\/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-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u0430 Rust 1.75.0, \u043d\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u043c async trait-\u0430\u043c\u0438 \u0438 return-position\u00a0impl Trait\u00a0in trait, \u043d\u0430\u0434\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <strong>impl Trait<\/strong> \u0438 \u0441 \u0447\u0435\u043c \u0435\u0433\u043e \u0435\u0434\u044f\u0442. <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 <s>\u0431\u0438\u0442\u0431\u043e\u043a\u0441\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u043e\u0432\u044b\u0445 \u0430\u043a\u0440\u043e\u043d\u0438\u043c\u043e\u0432<\/s> \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0437\u0430 \u043d\u0430\u0431\u043e\u0440\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 RPIT, RPITIT \u0438 \u0442.\u0434. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0432 Rust \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435.<\/p>\n<blockquote>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 <a href=\"https:\/\/youtu.be\/CWiz_RtA1Hw?si=FqEmTWlgLnUEcLbk\" rel=\"noopener noreferrer nofollow\">\u0432\u0438\u0434\u0435\u043e<\/a> \u043e\u0442 Jon Gjengset<\/p>\n<\/blockquote>\n<h2>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<ul>\n<li>\n<p><a href=\"#1\" rel=\"noopener noreferrer nofollow\">fn() \u2192 impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\" rel=\"noopener noreferrer nofollow\">fn(impl Trait)<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\" rel=\"noopener noreferrer nofollow\">trait { type = impl Trait }<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\" rel=\"noopener noreferrer nofollow\">type = impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\" rel=\"noopener noreferrer nofollow\">trait { fn() \u2192 impl Trait }<\/a><\/p>\n<\/li>\n<\/ul>\n<h2>fn() \u2192 impl Trait<\/h2>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Return Position Impl Trait (RPIT)<\/p>\n<p><strong>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p>\u0415\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"rust\">fn only_true&lt;I>(iter: I) -> \/* ??? *\/ where I: Iterator&lt;Item = bool> { iter.map(|x| foo(x)).filter(|&amp;x| x) }<\/code><\/pre>\n<p>\u041a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f? \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f <code>std::iter::Filter&lt;std::iter::Map&lt;I, ???>, ???><\/code>. \u0427\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432? \u041b\u0430\u0434\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f, \u0447\u0442\u043e\u0431\u044b \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 <code>Iterator&lt;Item = bool><\/code>. \u0412\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>Box&lt;dyn Iterator&lt;Item=bool>><\/code>. \u041d\u043e \u0442\u0443\u0442 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 + \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p>\u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 <em>\u044d\u043a\u0437\u0438\u0441\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b <\/em>(Existential types). \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">fn only_true&lt;I>(iter: I) -> impl Iterator&lt;Item = bool> \/* Opaque type *\/ where I: Iterator&lt;Item = bool>  { iter.filter(|&amp;x| x) \/* Hidden type *\/ }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f 2 \u043d\u043e\u0432\u044b\u0445 \u0442\u0435\u0440\u043c\u0438\u043d\u0430:<\/p>\n<p><em>Hidden Type<\/em> &#8212; \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439\/\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p><em>Opaque type<\/em> &#8212; \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Hidden Type.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043e\u0442 impl Traits:<\/p>\n<ul>\n<li>\n<p>\u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u0435 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0442\u0438\u043f\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0442 \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 impl Traits<\/strong><\/p>\n<ol>\n<li>\n<p>\u0412\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0438\u043f\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 (\u043f\u0440\u0430\u0432\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u043c):<\/p>\n<pre><code class=\"rust\">fn only_true&lt;I>(iter: I) ->    impl Future&lt;Output=impl Iterator&lt;Item = bool>> \/* Opaque type *\/ where I: Iterator&lt;Item = bool>  { async move { iter.filter(|&amp;x| x) \/* Hidden type *\/ } }<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p>\u0410\u0432\u0442\u043e-\u0442\u0440\u0435\u0439\u0442\u044b<\/p>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e-\u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c Hidden Type \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 &#171;\u0443\u0442\u0435\u0447\u043a\u0430&#187; (leakage):<\/p>\n<pre><code class=\"rust\">fn bar() -> impl Sized {     () }  fn foo() -> impl Sized + Send + Unpin {     bar() }<\/code><\/pre>\n<ol start=\"3\">\n<li>\n<p>\u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 Generic<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"rust\">fn f1&lt;R: Trait>() -> R {}  fn f2() -> impl Trait {}<\/code><\/pre>\n<p>\u0412 <code>f1<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0430\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u0438\u043f.<\/p>\n<p>\u0412 <code>f2<\/code> \u0442\u0435\u043b\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u0438\u043f.<\/p>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a Return Position Impl Trait (RPIT), \u0441\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438. \u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0435\u043b\u044c\u0437\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d \u0436\u0438\u0437\u043d\u0438 \u0432 RPIT. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">\/\/ \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 fn foo(t: &amp;()) -> impl Sized {     t }  \/\/ Ok fn foo&lt;'a>(t: &amp;'a ()) -> impl Sized + 'a {     t }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"rust\">\/\/ \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 fn bar&lt;T>(t: T) -> impl Sized {     () }  fn foo() -> impl Sized + 'static {     let s = String::new();          bar(&amp;s) }<\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0434\u043b\u044f 2021 \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0442\u0440\u044e\u043a:<\/p>\n<pre><code class=\"rust\">trait Captures&lt;U> {} impl&lt;T: ?Sized, U> Captures&lt;U> for T {}  \/\/ \u0414\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043b\u0430\u0439\u0444\u0442\u0430\u0439\u043c\u0430 fn foo&lt;'a>(t: &amp;'a ()) -> impl Sized + Captures&lt;&amp;'a ()> { t }  \/\/ \u0414\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 fn foo&lt;'a, 'b>(x: &amp;'a (), y: &amp;'b ()) -> impl Sized + Captures&lt;(&amp;'a (), &amp;'b ())> {     (x, y) }<\/code><\/pre>\n<p>\u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, <a href=\"https:\/\/github.com\/rust-lang\/rfcs\/pull\/3498\" rel=\"noopener noreferrer nofollow\">\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043f\u043e\u0444\u0438\u043a\u0441\u0438\u043b\u0438<\/a> \u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d \u0436\u0438\u0437\u043d\u0438 \u0432 2024 \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438. <\/p>\n<p>\u041f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438 \u0432 impl Traits \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/rust-lang.github.io\/rfcs\/3498-lifetime-capture-rules-2024.html\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<h2>fn(impl Trait)<\/h2>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Argument Position Impl Trait (APIT)<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443-\u0441\u0430\u0445\u0430\u0440\u043e\u043c \u0434\u043b\u044f <code>&lt;T: Trait>(t: T)<\/code>:<\/p>\n<pre><code class=\"rust\">fn f1&lt;D>(display: D) where D: std::fmt::Display { \/* \u2026 *\/ }  fn f2(display: impl std::fmt::Display) { \/* \u2026 *\/ }<\/code><\/pre>\n<p>\u041e\u0442\u043b\u0438\u0447\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435, \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0442\u0438\u043f\u044b:<\/p>\n<pre><code class=\"rust\">f1::&lt;u32>(1); \/\/ Ok f2(1) \/\/ Ok f2::&lt;u32>(1); \/\/ \u041e\u0448\u0438\u0431\u043a\u0430<\/code><\/pre>\n<h2>trait { type = impl Trait }<\/h2>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Assoc. Type Position Impl Trait (ATPIT) <a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/63063\" rel=\"noopener noreferrer nofollow\">(\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432 nightly)<\/a><\/p>\n<p>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f \u0442\u0430\u043a\u0430\u044f \u0436\u0435 \u043a\u0430\u043a \u0438 \u0443 RPIT. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">struct Odd; impl IntoIterator for Odd {     type IntoIter = impl Iterator&lt;Item = u32>;     fn into_iter(self) -> Self::IntoIter {         (0u32..).filter(|x| x % 2 != 0)     }  }<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u043b\u044f \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d \u0436\u0438\u0437\u043d\u0438 \u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a \u0442\u0438\u043f\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0451 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438:<\/p>\n<pre><code class=\"rust\">impl&lt;'a, T> Trait1 for Type {     type Assoc&lt;'b, U> = impl Trait2; \/\/ Ok }<\/code><\/pre>\n<h2>type = impl Trait<\/h2>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Type Alias Impl Trait (TAIT) <a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/41517\" rel=\"noopener noreferrer nofollow\">(\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432 nightly)<\/a><\/p>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c impl Trait-\u0430 \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445, \u043a\u0440\u043e\u043c\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">type Ready&lt;T> = impl std::future::Future&lt;Output = T>;  fn ready&lt;T>(t: T) -> Ready&lt;T> {     async move { t } }<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. <\/p>\n<h2>trait { fn() \u2192 impl Trait }<\/h2>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<p><strong>Feature<\/strong>: Return position impl Trait in Trait (RPITIT)<\/p>\n<p>\u0422\u043e, \u0447\u0442\u043e <a href=\"https:\/\/github.com\/rust-lang\/rust\/pull\/115822\/\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f<\/a> \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 1.75.0. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ATPIT \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 impl Trait, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0444\u0438\u0447\u0430. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0439 \u0444\u0438\u0447\u0438 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0442\u0440\u0435\u0439\u0442\u0430\u0445, \u0442\u0430\u043a \u043a\u0430\u043a <code>async fn () -> ret<\/code> \u0440\u0430\u0432\u043d\u043e\u0441\u0438\u043b\u044c\u043d\u0430 <code>fn () -> impl Future&lt;Output = ret><\/code>. \u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">impl MyAsyncTrait for MyStruct {     fn foo(&amp;mut self) -> impl Future&lt;Output = u32> {         async { 1 }     }  }<\/code><\/pre>\n<p>\u041d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b.<\/p>\n<pre><code class=\"rust\">trait It {     fn iter(&amp;self) -> impl Iterator&lt;Item = u32>; }  fn twice&lt;I: It>(i: I) where ???: Clone {     let it = i.iter();     it.clone().chain(it); }<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/109417\" rel=\"noopener noreferrer nofollow\">Return Type Notation<\/a> (\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435). \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"rust\">fn twice&lt;I: It&lt;iter(): Clone>>(i: I) {     let it = i.iter();     it.clone().chain(it); }<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u0430 \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 impl Trait \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0432\u0435\u0449\u044c \u0434\u043b\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u043e\u0432\u0435\u0440\u0445\u0435\u0434\u0430 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445. \u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 RPITIT \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u043e\u043b\u0433\u043e, \u043d\u043e \u043e\u043d\u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0435 ATPIT \u0438 TAIT. <\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<ul>\n<li>\n<p><a href=\"https:\/\/rustc-dev-guide.rust-lang.org\/opaque-types-type-alias-impl-trait.html\" rel=\"noopener noreferrer nofollow\">Opaque types<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=CWiz_RtA1Hw\" rel=\"noopener noreferrer nofollow\">Impl Trait aka Look ma\u2019, no generics! by Jon Gjengset<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/rust-lang.github.io\/rfcs\/3498-lifetime-capture-rules-2024.html\" rel=\"noopener noreferrer nofollow\">Lifetime capture rules 2024<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/63063\" rel=\"noopener noreferrer nofollow\">Assoc. Type Position Impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/41517\" rel=\"noopener noreferrer nofollow\">Type Alias Impl Trait<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/pull\/115822\/\" rel=\"noopener noreferrer nofollow\">Async fn in Traits and Trait { fn() \u2192 impl Trait }<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/rust-lang\/rust\/issues\/109417\" rel=\"noopener noreferrer nofollow\">Return Type Notation<\/a><\/p>\n<\/li>\n<\/ul>\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\/783266\/\"> https:\/\/habr.com\/ru\/articles\/783266\/<\/a><br \/><\/br><\/br><\/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-364027","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/364027","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=364027"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/364027\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=364027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=364027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=364027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}