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.json" "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.json" index 8bb392f99f3c9a42e20281f66781cffb45750e95..47793f3157fb895b985c316bc48a48c7589fdeb6 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.json" +++ "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.json" @@ -1,6 +1,7 @@ { - "type": "code_options", - "author": "幻灰龙", - "source": "helloworld.md", - "notebook_enable": true + "type": "code_options", + "author": "huanhuilong", + "source": "helloworld.md", + "notebook_enable": true, + "exercise_id": "6ee394e414ff4ca88dbd9e4bf0cee33d" } \ No newline at end of file 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/historyofrust.json" "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/historyofrust.json" index 86f1ffe3e4a32b31cb2e137417041b42fe5aca48..a1c1a435820c6c48960eaacc152a0bbc188f25fa 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/historyofrust.json" +++ "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/historyofrust.json" @@ -1,6 +1,7 @@ { - "type": "code_options", - "author": "maozhonggui", - "source": "historyofrust.md", - "notebook_enable": true + "type": "code_options", + "author": "maozhonggui", + "source": "historyofrust.md", + "notebook_enable": true, + "exercise_id": "7e6ebda86d1e44b7974f719f08c24c7e" } \ 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/1.\345\207\275\346\225\260\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/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e4005f9e95f838d34d6be929c0c1604dae42cff4 --- /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/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "rust-7f73462bc1d54b52891a4c2fc84f2a19", + "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/1.\345\207\275\346\225\260\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/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.json" new file mode 100644 index 0000000000000000000000000000000000000000..e9c35b027c5cc905e982cf41bb010566965ef1e4 --- /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/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "huanhuilong", + "source": "lifetime.md", + "notebook_enable": false, + "exercise_id": "526a3ad38c9a4f38af213e5d243c0e9e" +} \ 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/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" similarity index 96% rename from "data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" rename to "data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" index 92d69a6647f594af8133a0049b69c249f8966d3b..f62b1934d8a3446af2d663151a643b929769a6af 100644 --- "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/1.\345\207\275\346\225\260\347\224\237\345\221\275\345\221\250\346\234\237/lifetime.md" @@ -1,4 +1,4 @@ -# Rust 函数的生命周期 +# Rust 函数的生命周期(I) 选出以下Rust代码中,生命周期错误的选项 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/2.\347\273\223\346\236\204\344\275\223\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/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ad2ad4476a62d5010cf5723104d96c5ac9c1f710 --- /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/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "rust-f7b9ef1329c745959fb1ca56061b4ae5", + "keywords": [], + "children": [], + "export": [ + "lifetime_struct.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/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/lifetime_struct.json" "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/lifetime_struct.json" new file mode 100644 index 0000000000000000000000000000000000000000..f1a0b683f548a4422362e389845a82a0976b4204 --- /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/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/lifetime_struct.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "huanhuilong", + "source": "lifetime_struct.md", + "notebook_enable": false, + "exercise_id": "64a2ddc560a54f588e62e5730883df38" +} \ 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/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/lifetime_struct.md" "b/data/2.rust\344\270\255\351\230\266/2.\347\224\237\345\221\275\345\221\250\346\234\237/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/lifetime_struct.md" new file mode 100644 index 0000000000000000000000000000000000000000..d475cb37a68bc55bcc62e9fcad9f99626adb876e --- /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/2.\347\273\223\346\236\204\344\275\223\347\224\237\345\221\275\345\221\250\346\234\237/lifetime_struct.md" @@ -0,0 +1,69 @@ +# 结构体里的生命周期(I) + +以下对Rust结构体生命周期使用中,正确的代码是哪一项? + +## 答案 + +```rust +struct Piece<'a>{ + slice:&'a [u8] // 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参 +} + + +// Piece的定义里面,'a 表示vec的生命周期, +// 下面的例子调用,vec的生命周期至少应该大于等于piece的生命周期 +// 简单说vec存活的作用域应该大于等于piece的存活作用域 +fn test(){ + let vec = Vec::::new(); + let piece = Piece{slice: vec.as_slice()}; +} +``` + +## 选项 + +### 结构体内含有的引用变量,函数返回后,生命周期就结束了 + +```rust +struct Piece<'a>{ + slice:&'a [u8] // 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参 +} + +fn test_2()->Piece{ + let vec = Vec::::new(); + let piece = Piece{slice: vec.as_slice()}; + piece // compile error: ^^^^^ returns a value referencing data owned by the current function +} +``` + +### expected named lifetime parameter + +```rust +struct Piece{ + slice:& [u8] // 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参 +} + + +// Piece的定义里面,'a 表示vec的生命周期, +// 下面的例子调用,vec的生命周期至少应该大于等于piece的生命周期 +// 简单说vec存活的作用域应该大于等于piece的存活作用域 +fn test(){ + let vec = Vec::::new(); + let piece = Piece{slice: vec.as_slice()}; +} +``` + +### 结构体内持有的变量应该在构建的时候存活 + +```rust +struct Piece<'a>{ + slice:&'a [u8] // 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参 +} + +fn test(){ + let vec + { + vec = Vec::::new(); + } + let piece = Piece{slice: vec.as_slice()}; +} +``` 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 65ee15aa9b3bbfef3c79a92facfb2947dd7a32db..451af661088580d5f5b2ac3b483b9331c156bda9 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,8 +1,4 @@ { "node_id": "rust-d38898d251df42d49f5f367f077ede55", - "keywords": [], - "children":[], - "export": [ - "lifetime.json" - ] + "keywords": [] } \ 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" deleted file mode 100644 index fff32caa464011bc2bcfe9d7abaf931a4f106ccd..0000000000000000000000000000000000000000 --- "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" +++ /dev/null @@ -1,6 +0,0 @@ -{ - "type": "code_options", - "author": "huanhuilong", - "source": "lifetime.md", - "notebook_enable": false -} \ No newline at end of file diff --git a/data/tree.json b/data/tree.json index c76124577c96210754574145aacd48dfcf51030c..3554c6096f6073b083ba871929198221ec8433b5 100644 --- a/data/tree.json +++ b/data/tree.json @@ -330,7 +330,22 @@ "生命周期": { "node_id": "rust-d38898d251df42d49f5f367f077ede55", "keywords": [], - "children": [] + "children": [ + { + "函数生命周期": { + "node_id": "rust-7f73462bc1d54b52891a4c2fc84f2a19", + "keywords": [], + "children": [] + } + }, + { + "结构体生命周期": { + "node_id": "rust-f7b9ef1329c745959fb1ca56061b4ae5", + "keywords": [], + "children": [] + } + } + ] } }, { diff --git a/src/tree.py b/src/tree.py index 6a9e39fc518542f858f1f80635b35164b046e6ba..834db61992782bd64f77d5b873b593e637e9a7a3 100644 --- a/src/tree.py +++ b/src/tree.py @@ -71,10 +71,6 @@ def check_export(base, cfg): return flag -def gen_node_id(): - return "oceanbase-" + uuid.uuid4().hex - - class TreeWalker: def __init__(self, root, tree_name, title=None, log=None): self.name = tree_name @@ -107,7 +103,7 @@ class TreeWalker: for index, section_node in enumerate(chapter_node["children"]): section_title = list(section_node.keys())[0] full_path = os.path.join( - chapter_path, f"{index}.{section_title}") + chapter_path, f"{index+1}.{section_title}") if os.path.isdir(full_path): self.ensure_exercises(full_path) @@ -123,7 +119,8 @@ class TreeWalker: level_path = os.path.join(self.root, level) num, config = self.load_level_node(level_path) levels.append((num, config)) - levels.sort(key=lambda item: item[0]) + + levels = self.resort_children(self.root, levels) root_node["children"] = [item[1] for item in levels] return root_node @@ -149,7 +146,7 @@ class TreeWalker: num, chapter = self.load_chapter_node(full_name) chapters.append((num, chapter)) - chapters.sort(key=lambda item: item[0]) + chapters = self.resort_children(base, chapters) level_node["children"] = [item[1] for item in chapters] return level_node @@ -161,10 +158,21 @@ class TreeWalker: num, section = self.load_section_node(full_name) sections.append((num, section)) - sections.sort(key=lambda item: item[0]) + sections = self.resort_children(base, sections) chapter_node["children"] = [item[1] for item in sections] return chapter_node + def resort_children(self, base, children): + children.sort(key=lambda item: item[0]) + for index, [number, element] in enumerate(children): + title = list(element.keys())[0] + origin = os.path.join(base, f"{number}.{title}") + posted = os.path.join(base, f"{index+1}.{title}") + if origin != posted: + self.logger.info(f"rename [{origin}] to [{posted}]") + os.rename(origin, posted) + return children + def ensure_chapters(self): for subdir in os.listdir(self.root): self.ensure_level_config(subdir) @@ -285,4 +293,4 @@ class TreeWalker: exercise = load_json(full_name) if "exercise_id" not in exercise: exercise["exercise_id"] = uuid.uuid4().hex - dump_json(full_name, exercise) + dump_json(full_name, exercise, True, True)