{"id":207335,"date":"2020-12-28T08:07:48","date_gmt":"2020-12-28T00:07:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=207335"},"modified":"2020-12-21T10:09:26","modified_gmt":"2020-12-21T02:09:26","slug":"learn-rust-generics","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/learn-rust-generics.html","title":{"rendered":"\u5b66\u4e60Rust\u6cdb\u578b\u4e0e\u7279\u6027"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nRust \u8bed\u8a00\u662f\u4e00\u79cd\u9ad8\u6548\u3001\u53ef\u9760\u7684\u901a\u7528\u9ad8\u7ea7\u8bed\u8a00\u3002\u5176\u9ad8\u6548\u4e0d\u4ec5\u9650\u4e8e\u5f00\u53d1\u6548\u7387\uff0c\u5b83\u7684\u6267\u884c\u6548\u7387\u4e5f\u662f\u4ee4\u4eba\u79f0\u8d5e\u7684\uff0c\u662f\u4e00\u79cd\u5c11\u6709\u7684\u517c\u987e\u5f00\u53d1\u6548\u7387\u548c\u6267\u884c\u6548\u7387\u7684\u8bed\u8a00\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u6cdb\u578b\u662f\u4e00\u4e2a\u7f16\u7a0b\u8bed\u8a00\u4e0d\u53ef\u6216\u7f3a\u7684\u673a\u5236\u3002
\nC++ \u8bed\u8a00\u4e2d\u7528\"\u6a21\u677f\"\u6765\u5b9e\u73b0\u6cdb\u578b\uff0c\u800c C \u8bed\u8a00\u4e2d\u6ca1\u6709\u6cdb\u578b\u7684\u673a\u5236\uff0c\u8fd9\u4e5f\u5bfc\u81f4 C \u8bed\u8a00\u96be\u4ee5\u6784\u5efa\u7c7b\u578b\u590d\u6742\u7684\u5de5\u7a0b\u3002
\n\u6cdb\u578b\u673a\u5236\u662f\u7f16\u7a0b\u8bed\u8a00\u7528\u4e8e\u8868\u8fbe\u7c7b\u578b\u62bd\u8c61\u7684\u673a\u5236\uff0c\u4e00\u822c\u7528\u4e8e\u529f\u80fd\u786e\u5b9a\u3001\u6570\u636e\u7c7b\u578b\u5f85\u5b9a\u7684\u7c7b\uff0c\u5982\u94fe\u8868\u3001\u6620\u5c04\u8868\u7b49\u3002<\/p>\n

\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u6cdb\u578b<\/strong><\/div>\n

\u8fd9\u662f\u4e00\u4e2a\u5bf9\u6574\u578b\u6570\u5b57\u9009\u62e9\u6392\u5e8f\u7684\u65b9\u6cd5\uff1a
\n\u5b9e\u4f8b<\/p>\n

\r\nfn max(array: &[i32]) -> i32 {\r\n    let mut max_index = 0;\r\n    let mut i = 1;\r\n    while i < array.len() {\r\n        if array[i] > array[max_index] {\r\n            max_index = i;\r\n        }\r\n        i += 1;\r\n    }\r\n    array[max_index]\r\n}\r\n\r\nfn main() {\r\n    let a = [2, 4, 6, 3, 1];\r\n    println!(\"max = {}\", max(&a));\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\r\nmax = 6<\/pre>\n

\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u53d6\u6700\u5927\u503c\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406 i32 \u6570\u5b57\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f46\u65e0\u6cd5\u7528\u4e8e f64 \u7c7b\u578b\u7684\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528\u6cdb\u578b\u6211\u4eec\u53ef\u4ee5\u4f7f\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5229\u7528\u5230\u5404\u4e2a\u7c7b\u578b\u4e2d\u53bb\u3002\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u6240\u6709\u7684\u6570\u636e\u7c7b\u578b\u90fd\u53ef\u4ee5\u6bd4\u5927\u5c0f\uff0c\u6240\u4ee5\u63a5\u4e0b\u6765\u4e00\u6bb5\u4ee3\u7801\u5e76\u4e0d\u662f\u7528\u6765\u8fd0\u884c\u7684\uff0c\u800c\u662f\u7528\u6765\u63cf\u8ff0\u4e00\u4e0b\u51fd\u6570\u6cdb\u578b\u7684\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfn max(array: &[T]) -> T {\r\n    let mut max_index = 0;\r\n    let mut i = 1;\r\n    while i < array.len() {\r\n        if array[i] > array[max_index] {\r\n            max_index = i;\r\n        }\r\n        i += 1;\r\n    }\r\n    array[max_index]\r\n}<\/t><\/pre>\n
\u7ed3\u6784\u4f53\u4e0e\u679a\u4e3e\u7c7b\u4e2d\u7684\u6cdb\u578b<\/strong><\/div>\n

\u5728\u4e4b\u524d\u6211\u4eec\u5b66\u4e60\u7684 Option \u548c Result \u679a\u4e3e\u7c7b\u5c31\u662f\u6cdb\u578b\u7684\u3002<\/p>\n

Rust \u4e2d\u7684\u7ed3\u6784\u4f53\u548c\u679a\u4e3e\u7c7b\u90fd\u53ef\u4ee5\u5b9e\u73b0\u6cdb\u578b\u673a\u5236\u3002<\/p>\n

\r\nstruct Point {\r\n    x: T,\r\n    y: T\r\n}<\/t><\/pre>\n

\u8fd9\u662f\u4e00\u4e2a\u70b9\u5750\u6807\u7ed3\u6784\u4f53\uff0cT \u8868\u793a\u63cf\u8ff0\u70b9\u5750\u6807\u7684\u6570\u5b57\u7c7b\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n

\r\nlet p1 = Point {x: 1, y: 2};\r\nlet p2 = Point {x: 1.0, y: 2.0};<\/pre>\n

\u4f7f\u7528\u65f6\u5e76\u6ca1\u6709\u58f0\u660e\u7c7b\u578b\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u81ea\u52a8\u7c7b\u578b\u673a\u5236\uff0c\u4f46\u4e0d\u5141\u8bb8\u51fa\u73b0\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u60c5\u51b5\u5982\u4e0b\uff1a<\/p>\n

\r\nlet p = Point {x: 1, y: 2.0};<\/pre>\n

x \u4e0e 1 \u7ed1\u5b9a\u65f6\u5c31\u5df2\u7ecf\u5c06 T \u8bbe\u5b9a\u4e3a i32\uff0c\u6240\u4ee5\u4e0d\u5141\u8bb8\u518d\u51fa\u73b0 f64 \u7684\u7c7b\u578b\u3002\u5982\u679c\u6211\u4eec\u60f3\u8ba9 x \u4e0e y \u7528\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u8868\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6cdb\u578b\u6807\u8bc6\u7b26\uff1a<\/p>\n

\r\nstruct Point {\r\n    x: T1,\r\n    y: T2\r\n}<\/pre>\n

\u5728\u679a\u4e3e\u7c7b\u4e2d\u8868\u793a\u6cdb\u578b\u7684\u65b9\u6cd5\u8bf8\u5982 Option \u548c Result\uff1a<\/p>\n

\r\nenum Option {\r\n    Some(T),\r\n    None,\r\n}\r\n\r\nenum Result {\r\n    Ok(T),\r\n    Err(E),\r\n}<\/t><\/pre>\n

\u7ed3\u6784\u4f53\u4e0e\u679a\u4e3e\u7c7b\u90fd\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\uff0c\u90a3\u4e48\u65b9\u6cd5\u4e5f\u5e94\u8be5\u5b9e\u73b0\u6cdb\u578b\u7684\u673a\u5236\uff0c\u5426\u5219\u6cdb\u578b\u7684\u7c7b\u5c06\u65e0\u6cd5\u88ab\u6709\u6548\u7684\u65b9\u6cd5\u64cd\u4f5c\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nstruct Point {\r\n    x: T,\r\n    y: T,\r\n}\r\n\r\nimpl<\/t> Point<\/t> {\r\n    fn x(&self) -> &T {\r\n        &self.x\r\n    }\r\n}\r\n\r\nfn main() {\r\n    let p = Point { x: 1, y: 2 };\r\n    println!(\"p.x = {}\", p.x());\r\n}<\/t><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\r\np.x = 1<\/pre>\n

\u6ce8\u610f\uff0cimpl \u5173\u952e\u5b57\u7684\u540e\u65b9\u5fc5\u987b\u6709 \uff0c\u56e0\u4e3a\u5b83\u540e\u9762\u7684 T \u662f\u4ee5\u4e4b\u4e3a\u699c\u6837\u7684\u3002\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e3a\u5176\u4e2d\u7684\u4e00\u79cd\u6cdb\u578b\u6dfb\u52a0\u65b9\u6cd5\uff1a<\/p>\n

\r\nimpl Point {\r\n    fn x(&self) -> f64 {\r\n        self.x\r\n    }\r\n}\r\n<\/f64><\/pre>\n

impl \u5757\u672c\u8eab\u7684\u6cdb\u578b\u5e76\u6ca1\u6709\u963b\u788d\u5176\u5185\u90e8\u65b9\u6cd5\u5177\u6709\u6cdb\u578b\u7684\u80fd\u529b\uff1a<\/p>\n

\r\nimpl Point {\r\n    fn mixup(self, other: Point) -> Point {\r\n        Point {\r\n            x: self.x,\r\n            y: other.y,\r\n        }\r\n    }\r\n}<\/pre>\n

\u65b9\u6cd5 mixup \u5c06\u4e00\u4e2a Point \u70b9\u7684 x \u4e0e Point \u70b9\u7684 y \u878d\u5408\u6210\u4e00\u4e2a\u7c7b\u578b\u4e3a Point \u7684\u65b0\u70b9\u3002<\/p>\n

\u7279\u6027<\/strong><\/div>\n

\u7279\u6027\uff08trait\uff09\u6982\u5ff5\u63a5\u8fd1\u4e8e Java \u4e2d\u7684\u63a5\u53e3\uff08Interface\uff09\uff0c\u4f46\u4e24\u8005\u4e0d\u5b8c\u5168\u76f8\u540c\u3002\u7279\u6027\u4e0e\u63a5\u53e3\u76f8\u540c\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u4eec\u90fd\u662f\u4e00\u79cd\u884c\u4e3a\u89c4\u8303\uff0c\u53ef\u4ee5\u7528\u4e8e\u6807\u8bc6\u54ea\u4e9b\u7c7b\u6709\u54ea\u4e9b\u65b9\u6cd5\u3002<\/p>\n

\u7279\u6027\u5728 Rust \u4e2d\u7528 trait \u8868\u793a\uff1a<\/p>\n

\r\ntrait Descriptive {\r\n    fn describe(&self) -> String;\r\n}<\/pre>\n

Descriptive \u89c4\u5b9a\u4e86\u5b9e\u73b0\u8005\u5fc5\u987b\u6709\u662f describe(&self) -> String \u65b9\u6cd5\u3002<\/p>\n

\u6211\u4eec\u7528\u5b83\u5b9e\u73b0\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nstruct Person {\r\n    name: String,\r\n    age: u8\r\n}\r\n\r\nimpl Descriptive for Person {\r\n    fn describe(&self) -> String {\r\n        format!(\"{} {}\", self.name, self.age)\r\n    }\r\n}<\/pre>\n

\u683c\u5f0f\u662f\uff1a<\/p>\n

\r\nimpl <\u7279\u6027\u540d> for <\u6240\u5b9e\u73b0\u7684\u7c7b\u578b\u540d><\/pre>\n

Rust \u540c\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u7279\u6027\uff0c\u6bcf\u4e2a impl \u5757\u53ea\u80fd\u5b9e\u73b0\u4e00\u4e2a\u3002<\/p>\n

\u9ed8\u8ba4\u7279\u6027<\/strong><\/div>\n

\u8fd9\u662f\u7279\u6027\u4e0e\u63a5\u53e3\u7684\u4e0d\u540c\u70b9\uff1a\u63a5\u53e3\u53ea\u80fd\u89c4\u8303\u65b9\u6cd5\u800c\u4e0d\u80fd\u5b9a\u4e49\u65b9\u6cd5\uff0c\u4f46\u7279\u6027\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u4f5c\u4e3a\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u56e0\u4e3a\u662f\"\u9ed8\u8ba4\"\uff0c\u6240\u4ee5\u5bf9\u8c61\u65e2\u53ef\u4ee5\u91cd\u65b0\u5b9a\u4e49\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u91cd\u65b0\u5b9a\u4e49\u65b9\u6cd5\u4f7f\u7528\u9ed8\u8ba4\u7684\u65b9\u6cd5\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\ntrait Descriptive {\r\n    fn describe(&self) -> String {\r\n        String::from(\"[Object]\")\r\n    }\r\n}\r\n\r\nstruct Person {\r\n    name: String,\r\n    age: u8\r\n}\r\n\r\nimpl Descriptive for Person {\r\n    fn describe(&self) -> String {\r\n        format!(\"{} {}\", self.name, self.age)\r\n    }\r\n}\r\n\r\nfn main() {\r\n    let cali = Person {\r\n        name: String::from(\"Cali\"),\r\n        age: 24\r\n    };\r\n    println!(\"{}\", cali.describe());\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\r\nCali 24<\/pre>\n

\u5982\u679c\u6211\u4eec\u5c06 impl Descriptive for Person \u5757\u4e2d\u7684\u5185\u5bb9\u53bb\u6389\uff0c\u90a3\u4e48\u8fd0\u884c\u7ed3\u679c\u5c31\u662f\uff1a<\/p>\n

\r\n[Object]<\/pre>\n
\u7279\u6027\u505a\u53c2\u6570<\/strong><\/div>\n

\u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u505a\u53c2\u6570\uff0c\u4f8b\u5982\u56de\u8c03\u51fd\u6570\u3001\u8bbe\u7f6e\u6309\u94ae\u4e8b\u4ef6\u7b49\u3002\u5728 Java \u4e2d\u51fd\u6570\u5fc5\u987b\u4ee5\u63a5\u53e3\u5b9e\u73b0\u7684\u7c7b\u5b9e\u4f8b\u6765\u4f20\u9012\uff0c\u5728 Rust \u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u7279\u6027\u53c2\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n

\r\nfn output(object: impl Descriptive) {\r\n    println!(\"{}\", object.describe());\r\n}<\/pre>\n

\u4efb\u4f55\u5b9e\u73b0\u4e86 Descriptive \u7279\u6027\u7684\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u6ca1\u5fc5\u8981\u4e86\u89e3\u4f20\u5165\u5bf9\u8c61\u6709\u6ca1\u6709\u5176\u4ed6\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u53ea\u9700\u8981\u4e86\u89e3\u5b83\u4e00\u5b9a\u6709 Descriptive \u7279\u6027\u89c4\u8303\u7684\u65b9\u6cd5\u5c31\u53ef\u4ee5\u4e86\u3002\u5f53\u7136\uff0c\u6b64\u51fd\u6570\u5185\u4e5f\u65e0\u6cd5\u4f7f\u7528\u5176\u4ed6\u7684\u5c5e\u6027\u4e0e\u65b9\u6cd5\u3002<\/p>\n

\u7279\u6027\u53c2\u6570\u8fd8\u53ef\u4ee5\u7528\u8fd9\u79cd\u7b49\u6548\u8bed\u6cd5\u5b9e\u73b0\uff1a<\/p>\n

\r\nfn output(object: T) {\r\n    println!(\"{}\", object.describe());\r\n}<\/t:><\/pre>\n

\u8fd9\u662f\u4e00\u79cd\u98ce\u683c\u7c7b\u4f3c\u6cdb\u578b\u7684\u8bed\u6cd5\u7cd6\uff0c\u8fd9\u79cd\u8bed\u6cd5\u7cd6\u5728\u6709\u591a\u4e2a\u53c2\u6570\u7c7b\u578b\u5747\u662f\u7279\u6027\u7684\u60c5\u51b5\u4e0b\u5341\u5206\u5b9e\u7528\uff1a<\/p>\n

\r\nfn output_two(arg1: T, arg2: T) {\r\n    println!(\"{}\", arg1.describe());\r\n    println!(\"{}\", arg2.describe());\r\n}<\/t:><\/pre>\n

\u7279\u6027\u4f5c\u7c7b\u578b\u8868\u793a\u65f6\u5982\u679c\u6d89\u53ca\u591a\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u7528 + \u7b26\u53f7\u8868\u793a\uff0c\u4f8b\u5982\uff1a<\/p>\n

\r\nfn notify(item: impl Summary + Display)\r\nfn notify(item: T)<\/t:><\/pre>\n

\u6ce8\u610f\uff1a\u4ec5\u7528\u4e8e\u8868\u793a\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u610f\u5473\u7740\u53ef\u4ee5\u5728 impl \u5757\u4e2d\u4f7f\u7528\u3002<\/p>\n

\u590d\u6742\u7684\u5b9e\u73b0\u5173\u7cfb\u53ef\u4ee5\u4f7f\u7528 where \u5173\u952e\u5b57\u7b80\u5316\uff0c\u4f8b\u5982\uff1a<\/p>\n

\r\nfn some_function(t: T, u: U)<\/t:><\/pre>\n

\u53ef\u4ee5\u7b80\u5316\u6210\uff1a<\/p>\n

\r\nfn some_function(t: T, u: U) -> i32\r\n    where T: Display + Clone,\r\n          U: Clone + Debug<\/pre>\n

\u5728\u4e86\u89e3\u8fd9\u4e2a\u8bed\u6cd5\u4e4b\u540e\uff0c\u6cdb\u578b\u7ae0\u8282\u4e2d\u7684\"\u53d6\u6700\u5927\u503c\"\u6848\u4f8b\u5c31\u53ef\u4ee5\u771f\u6b63\u5b9e\u73b0\u4e86\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\ntrait Comparable {\r\n    fn compare(&self, object: &Self) -> i8;\r\n}\r\n\r\nfn max(array: &[T]) -> &T {\r\n    let mut max_index = 0;\r\n    let mut i = 1;\r\n    while i < array.len() {\r\n        if array[i].compare(&array[max_index]) > 0 {\r\n            max_index = i;\r\n        }\r\n        i += 1;\r\n    }\r\n    &array[max_index]\r\n}\r\n\r\nimpl Comparable for f64 {\r\n    fn compare(&self, object: &f64) -> i8 {\r\n        if &self > &object { 1 }\r\n        else if &self == &object { 0 }\r\n        else { -1 }\r\n    }\r\n}\r\n\r\nfn main() {\r\n    let arr = [1.0, 3.0, 5.0, 4.0, 2.0];\r\n    println!(\"maximum of arr is {}\", max(&arr));\r\n}<\/t:><\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

\r\nmaximum of arr is 5<\/pre>\n

Tip: \u7531\u4e8e\u9700\u8981\u58f0\u660e compare \u51fd\u6570\u7684\u7b2c\u4e8c\u53c2\u6570\u5fc5\u987b\u4e0e\u5b9e\u73b0\u8be5\u7279\u6027\u7684\u7c7b\u578b\u76f8\u540c\uff0c\u6240\u4ee5 Self \uff08\u6ce8\u610f\u5927\u5c0f\u5199\uff09\u5173\u952e\u5b57\u5c31\u4ee3\u8868\u4e86\u5f53\u524d\u7c7b\u578b\uff08\u4e0d\u662f\u5b9e\u4f8b\uff09\u672c\u8eab\u3002<\/p>\n

\u7279\u6027\u505a\u8fd4\u56de\u503c<\/strong><\/div>\n

\u7279\u6027\u505a\u8fd4\u56de\u503c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfn person() -> impl Descriptive {\r\n    Person {\r\n        name: String::from(\"Cali\"),\r\n        age: 24\r\n    }\r\n}<\/pre>\n

\u4f46\u662f\u6709\u4e00\u70b9\uff0c\u7279\u6027\u505a\u8fd4\u56de\u503c\u53ea\u63a5\u53d7\u5b9e\u73b0\u4e86\u8be5\u7279\u6027\u7684\u5bf9\u8c61\u505a\u8fd4\u56de\u503c\u4e14\u5728\u540c\u4e00\u4e2a\u51fd\u6570\u4e2d\u6240\u6709\u53ef\u80fd\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5fc5\u987b\u5b8c\u5168\u4e00\u6837\u3002\u6bd4\u5982\u7ed3\u6784\u4f53 A \u4e0e\u7ed3\u6784\u4f53 B \u90fd\u5b9e\u73b0\u4e86\u7279\u6027 Trait\uff0c\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\u5c31\u662f\u9519\u8bef\u7684\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\nfn some_function(bool bl) -> impl Descriptive {\r\n    if bl {\r\n        return A {};\r\n    } else {\r\n        return B {};\r\n    }\r\n}<\/pre>\n
\u6709\u6761\u4ef6\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/div>\n

impl \u529f\u80fd\u5341\u5206\u5f3a\u5927\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u5b9e\u73b0\u7c7b\u7684\u65b9\u6cd5\u3002\u4f46\u5bf9\u4e8e\u6cdb\u578b\u7c7b\u6765\u8bf4\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u533a\u5206\u4e00\u4e0b\u5b83\u6240\u5c5e\u7684\u6cdb\u578b\u5df2\u7ecf\u5b9e\u73b0\u7684\u65b9\u6cd5\u6765\u51b3\u5b9a\u5b83\u63a5\u4e0b\u6765\u8be5\u5b9e\u73b0\u7684\u65b9\u6cd5\uff1a<\/p>\n

\r\nstruct A {}\r\n\r\nimpl A {\r\n    fn d(&self) {}\r\n}<\/t><\/t:><\/t><\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u58f0\u660e\u4e86 A<\/t> \u7c7b\u578b\u5fc5\u987b\u5728 T \u5df2\u7ecf\u5b9e\u73b0 B \u548c C \u7279\u6027\u7684\u524d\u63d0\u4e0b\u624d\u80fd\u6709\u6548\u5b9e\u73b0\u6b64 impl \u5757\u3002<\/t><\/p>\n","protected":false},"excerpt":{"rendered":"

\u6cdb\u578b\u662f\u4e00\u4e2a\u7f16\u7a0b\u8bed\u8a00\u4e0d\u53ef\u6216\u7f3a\u7684\u673a\u5236\u3002 C++ \u8bed\u8a00\u4e2d\u7528”\u6a21\u677f”\u6765\u5b9e\u73b0\u6cdb\u578b\uff0c\u800c C \u8bed\u8a00\u4e2d\u6ca1\u6709\u6cdb\u578b\u7684\u673a\u5236\uff0c\u8fd9\u4e5f\u5bfc\u81f4 […]<\/p>\n","protected":false},"author":1481,"featured_media":207331,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-207335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=207335"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207335\/revisions"}],"predecessor-version":[{"id":207338,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207335\/revisions\/207338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/207331"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=207335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=207335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=207335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}