{"id":159763,"date":"2020-09-20T22:50:24","date_gmt":"2020-09-20T14:50:24","guid":{"rendered":"http:\/\/4563.org\/?p=159763"},"modified":"2020-09-20T22:50:24","modified_gmt":"2020-09-20T14:50:24","slug":"stditeriteratorsize_hint-%e6%96%b9%e6%b3%95%e7%a9%b6%e7%ab%9f%e5%ae%9e%e7%8e%b0%e4%ba%86%e4%b8%aa%e4%bb%80%e4%b9%88%e5%8a%9f%e8%83%bd%e5%a4%b4%e5%a5%bd%e6%99%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=159763","title":{"rendered":"std::iter::iterator::size_hint \u65b9\u6cd5\u7a76\u7adf\u5b9e\u73b0\u4e86\u4e2a\u4ec0\u4e48\u529f\u80fd\u5934\u597d\u6655"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  std::iter::iterator::size_hint \u65b9\u6cd5\u7a76\u7adf\u5b9e\u73b0\u4e86\u4e2a\u4ec0\u4e48\u529f\u80fd\u5934\u597d\u6655               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 0gre2019 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u9996\u5148\u5b98\u65b9\u6587\u6863\u63cf\u8ff0\u4e3a\uff0c\u8fd4\u56de\u5f53\u524d\u8fed\u4ee3\u5668\u672a\u8fed\u4ee3\u5143\u7d20\u6570\u91cf\u7684\u4e0a\u4e0b\u754c\u3002<\/p>\n<p>\u9996\u5148\u8fd9\u4e2a\u5b9a\u4e49\u5c31\u5f88\u8ff7\u60d1\uff0c\u4ec0\u4e48\u53eb\u672a\u8fed\u4ee3\u5143\u7d20\u6570\u91cf\u7684\u4e0a\u4e0b\u754c\u3002\u3002\u672a\u8fed\u4ee3\u5143\u7d20\u6570\u91cf\u4e0d\u5e94\u8be5\u662f\u786e\u5b9a\u7684\u4e00\u4e2a\u503c\u5417 &lt;\u5373\u4f7f\u662f\u4e00\u4e2a\u7c7b\u4f3c\u4e8e (let x = (0..); \u7684\u65e0\u9650\u8fed\u4ee3\u5668\uff0c\u4e5f\u5e94\u8be5\u662f\u4e00\u4e2a\u786e\u5b9a\u7684\u503c\u2018infinite\u2019\u6216\u8005 None \u5427\uff1f&gt;<\/p>\n<p>\u56de\u5230\u6587\u6863\uff0c\u5177\u4f53\u5b9e\u73b0\u662f\u8fd4\u56de\u4e00\u4e2a tuple (usize, Option&lt;usize&gt;) \uff0c\u5de6\u8fb9\u8868\u793a\u4e0b\u754c\uff0c\u53f3\u8fb9\u8868\u793a\u4e0a\u754c\u3002<\/p>\n<p>\u7136\u540e\u518d\u770b\u4e0b\u5b98\u65b9\u7ed9\u7684\u4f8b\u5b50\uff1a<\/p>\n<ul>\n<li>\n<p>\u4e00\u4e2a\u57fa\u7840\u6837\u4f8b\uff1a let a = [1, 2, 3]; let iter = a.iter();<\/p>\n<p>assert_eq!((3, Some(3)), iter.size_hint());<\/p>\n<\/li>\n<\/ul>\n<p>\u4ece\u8fd9\u6bb5\u4ee3\u7801\u6765\u770b\uff0c\u7531\u4e8e iter \u7684 next \u672a\u88ab\u8c03\u7528\uff0c\u6240\u4ee5 iter \u7684\u672a\u8fed\u4ee3\u5143\u7d20\u6570\u91cf\u662f 3<\/p>\n<p>\u7136\u540e\u770b\u4e00\u4e0b size_hint \u7684\u8fd4\u56de\u503c\uff0c(3, Some(3))\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u662f\u4e00\u79cd\u6765\u81ea\u7f16\u8bd1\u5668\u7684\u2018\u63a8\u6d4b\u2019\u5417\uff1f\u6211\u7684\u7406\u89e3\u662f\uff0c\u7f16\u8bd1\u5668\u63a8\u6d4b\u51fa\u2018\u672a\u8fed\u4ee3\u5143\u7d20\u6570\u91cf\u7684\u6700\u5c0f\u503c\u2019\u4e3a 3\uff0c\u56e0\u4e3a\u6211\u4eec\u5df2\u7ecf\u786e\u5b9a\u4e86 iter \u751a\u81f3\u5e76\u672a\u5f00\u59cb\u5de5\u4f5c\uff1b\u7136\u540e\u7f16\u8bd1\u5668\u63a8\u6d4b\u51fa\u2018\u672a\u8fed\u4ee3\u5143\u7d20\u6570\u91cf\u7684\u6700\u5927\u503c\u2019\u4e5f\u4e3a 3\uff0c\u8fd9\u6211\u4e5f\u80fd\u7406\u89e3\u3002<\/p>\n<ul>\n<li>\n<p>\u4e00\u4e2a\u590d\u6742\u70b9\u7684\u6837\u4f8b\uff1a<\/p>\n<p>\/\/ \u8fc7\u6ee4\u51fa 0..10 \u4e2d\u7684\u5076\u6570.\u5219 iter \u7684\u5185\u5bb9\u5e94\u8be5\u662f(0, 2, 4, 6, 8)<\/p>\n<p>let iter = (0..10).filter(|x| x % 2 == 0);<\/p>\n<p>\/\/ \u51ed\u4ec0\u4e48\uff1f\uff1f\u8fd9\u4e2a 0 \u548c 10 \u4ee3\u8868\u4ec0\u4e48\uff1f\u6309\u6211\u7684\u7406\u89e3\u8fd8\u6709 5 \u4e2a\u5143\u7d20\u672a\u8fed\u4ee3\uff0c\u600e\u4e48\u4f1a\u6709\u4e2a 10 \u5462\u3002\u3002<\/p>\n<p>assert_eq!((0, Some(10)), iter.size_hint());<\/p>\n<p>\/\/ \u7528 chain \u9002\u914d\u5668\u5728\u4e4b\u540e\u63a5\u4e0a\u4e94\u4e2a\u503c\uff0c\u90a3\u4e48\u5e94\u8be5\u4e3a(0 2 4 6 8 15 16 17 18 19)<\/p>\n<p>let iter = (0..10).filter(|x| x % 2 == 0).chain(15..20);<\/p>\n<p>\/\/ \u4e3a\u4ec0\u4e48\u4e0a\u4e0b\u754c\u540c\u65f6\u52a0 5 \uff1f\uff1f\u8fd9\u91cc 5 \u548c 15 \u53c8\u4ee3\u8868\u4ec0\u4e48\u5462\uff1f\uff1f<\/p>\n<p>assert_eq!((5, Some(15)), iter.size_hint());<\/p>\n<\/li>\n<\/ul>\n<p>\u5b9e\u5728\u592a\u56f0\u60d1\u4e86\uff0c\u770b\u4e86\u4e00\u4e2a\u591a\u5c0f\u65f6\u6123\u662f\u6ca1\u770b\u61c2\u3002\u5982\u679c\u8fd9\u4e2a\u65b9\u6cd5\u771f\u7684\u662f\u4e00\u79cd\u2018\u63a8\u6d4b\u2019\u529f\u80fd\uff0c\u5b83\u662f\u51ed\u4ec0\u4e48\u6765\u63a8\u6d4b\u7684\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>std::iter::iterat&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/159763"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=159763"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/159763\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159763"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}