{"action":"created","issue":{"url":"https://api.github.com/repos/yetone/blog/issues/2","repository_url":"https://api.github.com/repos/yetone/blog","labels_url":"https://api.github.com/repos/yetone/blog/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/yetone/blog/issues/2/comments","events_url":"https://api.github.com/repos/yetone/blog/issues/2/events","html_url":"https://github.com/yetone/blog/issues/2","id":267601724,"node_id":"MDU6SXNzdWUyNjc2MDE3MjQ=","number":2,"title":"[WIP] Rust 学习📓","user":{"login":"yetone","id":1206493,"node_id":"MDQ6VXNlcjEyMDY0OTM=","avatar_url":"https://avatars2.githubusercontent.com/u/1206493?v=4","gravatar_id":"","url":"https://api.github.com/users/yetone","html_url":"https://github.com/yetone","followers_url":"https://api.github.com/users/yetone/followers","following_url":"https://api.github.com/users/yetone/following{/other_user}","gists_url":"https://api.github.com/users/yetone/gists{/gist_id}","starred_url":"https://api.github.com/users/yetone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yetone/subscriptions","organizations_url":"https://api.github.com/users/yetone/orgs","repos_url":"https://api.github.com/users/yetone/repos","events_url":"https://api.github.com/users/yetone/events{/privacy}","received_events_url":"https://api.github.com/users/yetone/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2017-10-23T09:36:29Z","updated_at":"2018-09-01T14:35:49Z","closed_at":null,"author_association":"OWNER","body":"### 解引用强制多态会自动的将指针或智能指针的引用转换为指针内容的引用。\r\n\r\n```rust\r\nuse std::ops::Deref;\r\n\r\nstruct A(i32);\r\n\r\nimpl Deref for A {\r\n    type Target = i32;\r\n\r\n    fn deref(&self) -> &i32 {\r\n        &self.0\r\n    }\r\n}\r\n\r\nfn add_one(v: &i32) -> i32 {\r\n    v + 1\r\n}\r\n\r\nfn main() {\r\n    let a = A(1);\r\n    // impl Deref for A {type Target = B;}\r\n    // &A == &B\r\n    assert_eq!(add_one(&a), 2);\r\n}\r\n```\r\n解引用时 rust 会偷偷地调用 deref 方法：\r\n```rust\r\n*a == *(a.deref())\r\n```\r\n\r\n强制解引用多态是针对**函数参数**的，是 rust 为了方便传递**引用类型的参数**而提供的语法糖，为了把不匹配的引用类型转换成与函数签名中的参数相同的类型而做的语法糖：\r\n\r\n```rust\r\nfn hello(name: &str) {\r\n    println!(\"hello {}\", name);\r\n}\r\n\r\n// hello(&a) == hello(a.deref())\r\n```\r\n\r\n### scan 和 fold 的区别：\r\n\r\nscan 是个适配器（返回 Iterator），fold 是个消费器（返回实现了 FromIter 的类型）。scan closure 的第一个参数是个可变引用 &mut，每次迭代需要改变第一个参数，且 closure 的返回值需要是个 Optional<Self::Item>\r\n\r\n```rust\r\nfn main() {\r\n    let a = [1,2,3,4];\r\n    a.iter().scan(0, |a, c| {\r\n        println!(\"scan: {} {}\", a, c);\r\n        *a = *a + c;\r\n        Some(*a)\r\n    }).collect::<Vec<_>>();\r\n    a.iter().fold(0, |a, c| {\r\n        println!(\"fold: {} {}\", a, c);\r\n        a + c\r\n    });\r\n}\r\n```\r\n输出：\r\n```shell\r\nscan: 0 1\r\nscan: 1 2\r\nscan: 3 3\r\nscan: 6 4\r\nfold: 0 1\r\nfold: 1 2\r\nfold: 3 3\r\nfold: 6 4\r\n```\r\n\r\n### slice a &str\r\n\r\n```rust\r\nlet s = \"abcdefghi\" // &str;\r\ns[1..3]; // str\r\n&s[1..3]; // &str\r\ns[1..3].to_string(); // String\r\nlet s = String::from(\"abcdef\"); // String\r\ns[1..3]; // str\r\n```"},"comment":{"url":"https://api.github.com/repos/yetone/blog/issues/comments/417863754","html_url":"https://github.com/yetone/blog/issues/2#issuecomment-417863754","issue_url":"https://api.github.com/repos/yetone/blog/issues/2","id":417863754,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNzg2Mzc1NA==","user":{"login":"yetone","id":1206493,"node_id":"MDQ6VXNlcjEyMDY0OTM=","avatar_url":"https://avatars2.githubusercontent.com/u/1206493?v=4","gravatar_id":"","url":"https://api.github.com/users/yetone","html_url":"https://github.com/yetone","followers_url":"https://api.github.com/users/yetone/followers","following_url":"https://api.github.com/users/yetone/following{/other_user}","gists_url":"https://api.github.com/users/yetone/gists{/gist_id}","starred_url":"https://api.github.com/users/yetone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yetone/subscriptions","organizations_url":"https://api.github.com/users/yetone/orgs","repos_url":"https://api.github.com/users/yetone/repos","events_url":"https://api.github.com/users/yetone/events{/privacy}","received_events_url":"https://api.github.com/users/yetone/received_events","type":"User","site_admin":false},"created_at":"2018-09-01T14:35:49Z","updated_at":"2018-09-01T14:35:49Z","author_association":"OWNER","body":"# trait\r\n\r\n## supertrait\r\n\r\n看起来像继承，其实不是继承啊，傻子\r\n\r\n```rust\r\ntrait A {}\r\ntrait B: A {}\r\n```\r\n\r\n意味着，你要实现 B，必须得先实现 A：\r\n\r\n```rust\r\nstruct S;\r\nimpl A for S; // 如果漏掉了这一行就跪了\r\nimpl B for S;\r\n```"}}
{"actor":{"display_login":"yetone"}}