From 7b642af1410938d36356bbd978465b38b723f031 Mon Sep 17 00:00:00 2001 From: feilong Date: Wed, 24 Nov 2021 20:39:33 +0800 Subject: [PATCH] add lifetime exercise --- .../helloworld.md" | 4 +- .../config.json" | 6 ++- .../lifetime.json" | 6 +++ .../lifetime.md" | 51 +++++++++++++++++++ data/tree.json | 18 +++---- 5 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 "data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.json" create mode 100644 "data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" diff --git "a/data/1.rust\345\210\235\351\230\266/1.\351\242\204\345\244\207\347\237\245\350\257\206/1.rust\347\256\200\344\273\213/helloworld.md" "b/data/1.rust\345\210\235\351\230\266/1.\351\242\204\345\244\207\347\237\245\350\257\206/1.rust\347\256\200\344\273\213/helloworld.md" index 6344506..bf22b6a 100644 --- "a/data/1.rust\345\210\235\351\230\266/1.\351\242\204\345\244\207\347\237\245\350\257\206/1.rust\347\256\200\344\273\213/helloworld.md" +++ "b/data/1.rust\345\210\235\351\230\266/1.\351\242\204\345\244\207\347\237\245\350\257\206/1.rust\347\256\200\344\273\213/helloworld.md" @@ -18,8 +18,8 @@ fn main() { ```rust fn main() { - let str = "Hello,World!"; - println!("{}", str); + let str1 = "Hello,World!"; + println!("{}", str1); } ``` diff --git "a/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/config.json" "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/config.json" index 451af66..65ee15a 100644 --- "a/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/config.json" +++ "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/config.json" @@ -1,4 +1,8 @@ { "node_id": "rust-d38898d251df42d49f5f367f077ede55", - "keywords": [] + "keywords": [], + "children":[], + "export": [ + "lifetime.json" + ] } \ No newline at end of file diff --git "a/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.json" "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.json" new file mode 100644 index 0000000..fff32ca --- /dev/null +++ "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "huanhuilong", + "source": "lifetime.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" new file mode 100644 index 0000000..92d69a6 --- /dev/null +++ "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" @@ -0,0 +1,51 @@ +# Rust 函数的生命周期 + +选出以下Rust代码中,生命周期错误的选项 + +## 答案 + +```rust +// compile error! +// where am I come from? +fn process_objs(x:&Obejct, y:&Object):&Object{ + if(x.is_ok()){ + &x + }else{ + &y + } +} +``` + +## 选项 + +### 如果这个借用本来就是从外部传入的,那当然可以返回,函数结束后这个对象还是有效的 + +```rust +// I am borrowed from caller +// return borrow to the caller is safe +fn process_obj(obj:&Object):&Object{ + &obj +} +``` + +### 显式添加生命周期标记 + +```rust +// I am come from 'a lifetime, NOT 'b +fn process_objs<'a,'b>(x: &'a Obejct, y:&'b Object):&'a Object{ + &x +} +``` + +### 显式添加生命周期标记,解决歧义 + +```rust +// I am come from 'a lifetime, x,y,and result are all 'a lifetime +fn process_objs<'a>(x: &'a Obejct, y:&'a Object):&'a Object{ + if(x.is_ok()){ + &x + }else{ + &y + } +} +``` diff --git a/data/tree.json b/data/tree.json index efd4584..c761245 100644 --- a/data/tree.json +++ b/data/tree.json @@ -4,7 +4,7 @@ "keywords": [], "children": [ { - "Rust初阶": { + "rust初阶": { "node_id": "rust-e2292238ccbf403d85fb20dd7b4c85ad", "keywords": [], "children": [ @@ -14,7 +14,7 @@ "keywords": [], "children": [ { - "Rust简介": { + "rust简介": { "node_id": "rust-7c394f164d8f44519521f03db08a1321", "keywords": [], "children": [ @@ -68,7 +68,7 @@ } }, { - "Rust安装": { + "rust安装": { "node_id": "rust-502b3cef4dbc41fdad069522470f3662", "keywords": [], "children": [ @@ -96,7 +96,7 @@ } }, { - "认识Rust工具链": { + "认识rust工具链": { "node_id": "rust-0d9e5f9449b94aa692a363f21a66c8af", "keywords": [], "children": [] @@ -120,7 +120,7 @@ } }, { - "Rust基本概念": { + "rust基本概念": { "node_id": "rust-2cea732119294abab6f2887a364b242a", "keywords": [], "children": [ @@ -272,7 +272,7 @@ } }, { - "Rust中阶": { + "rust中阶": { "node_id": "rust-7377966439734497b91f3d775f9b0e0d", "keywords": [], "children": [ @@ -387,7 +387,7 @@ } }, { - "Rust高阶": { + "rust高阶": { "node_id": "rust-981f4f37a8e547abb135f54592ac52a5", "keywords": [], "children": [ @@ -496,14 +496,14 @@ } }, { - "用Rust实现X算法": { + "用rust实现X算法": { "node_id": "rust-d935276e360d41deb87caa06272c1ade", "keywords": [], "children": [] } }, { - "用Rust解X题": { + "用rust解X题": { "node_id": "rust-f8981b363064411c86d033f391ec133c", "keywords": [], "children": [] -- GitLab