Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_rust
提交
ba11e301
S
skill_tree_rust
项目概览
CSDN 技术社区
/
skill_tree_rust
通知
32
Star
7
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ba11e301
编写于
11月 24, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add lifetime exercise2
上级
7b642af1
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
151 addition
and
31 deletion
+151
-31
data/1.rust初阶/1.预备知识/1.rust简介/helloworld.json
data/1.rust初阶/1.预备知识/1.rust简介/helloworld.json
+6
-4
data/1.rust初阶/1.预备知识/1.rust简介/historyofrust.json
data/1.rust初阶/1.预备知识/1.rust简介/historyofrust.json
+6
-4
data/2.rust中阶/2.生命周期/1.函数生命周期/config.json
data/2.rust中阶/2.生命周期/1.函数生命周期/config.json
+9
-0
data/2.rust中阶/2.生命周期/1.函数生命周期/lifetime.json
data/2.rust中阶/2.生命周期/1.函数生命周期/lifetime.json
+8
-0
data/2.rust中阶/2.生命周期/1.函数生命周期/lifetime.md
data/2.rust中阶/2.生命周期/1.函数生命周期/lifetime.md
+1
-1
data/2.rust中阶/2.生命周期/2.结构体生命周期/config.json
data/2.rust中阶/2.生命周期/2.结构体生命周期/config.json
+9
-0
data/2.rust中阶/2.生命周期/2.结构体生命周期/lifetime_struct.json
data/2.rust中阶/2.生命周期/2.结构体生命周期/lifetime_struct.json
+8
-0
data/2.rust中阶/2.生命周期/2.结构体生命周期/lifetime_struct.md
data/2.rust中阶/2.生命周期/2.结构体生命周期/lifetime_struct.md
+69
-0
data/2.rust中阶/2.生命周期/config.json
data/2.rust中阶/2.生命周期/config.json
+2
-5
data/2.rust中阶/2.生命周期/lifetime.json
data/2.rust中阶/2.生命周期/lifetime.json
+0
-7
data/tree.json
data/tree.json
+16
-1
src/tree.py
src/tree.py
+17
-9
未找到文件。
data/1.rust初阶/1.预备知识/1.rust简介/helloworld.json
浏览文件 @
ba11e301
{
"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
data/1.rust初阶/1.预备知识/1.rust简介/historyofrust.json
浏览文件 @
ba11e301
{
"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
data/2.rust中阶/2.生命周期/1.函数生命周期/config.json
0 → 100644
浏览文件 @
ba11e301
{
"node_id"
:
"rust-7f73462bc1d54b52891a4c2fc84f2a19"
,
"keywords"
:
[],
"children"
:
[],
"export"
:
[
"lifetime.json"
]
}
\ No newline at end of file
data/2.rust中阶/2.生命周期/1.函数生命周期/lifetime.json
0 → 100644
浏览文件 @
ba11e301
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"lifetime.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"526a3ad38c9a4f38af213e5d243c0e9e"
}
\ No newline at end of file
data/2.rust中阶/2.生命周期/lifetime.md
→
data/2.rust中阶/2.生命周期/
1.函数生命周期/
lifetime.md
浏览文件 @
ba11e301
# Rust 函数的生命周期
# Rust 函数的生命周期
(I)
选出以下Rust代码中,生命周期错误的选项
...
...
data/2.rust中阶/2.生命周期/2.结构体生命周期/config.json
0 → 100644
浏览文件 @
ba11e301
{
"node_id"
:
"rust-f7b9ef1329c745959fb1ca56061b4ae5"
,
"keywords"
:
[],
"children"
:
[],
"export"
:
[
"lifetime_struct.json"
]
}
\ No newline at end of file
data/2.rust中阶/2.生命周期/2.结构体生命周期/lifetime_struct.json
0 → 100644
浏览文件 @
ba11e301
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"lifetime_struct.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"64a2ddc560a54f588e62e5730883df38"
}
\ No newline at end of file
data/2.rust中阶/2.生命周期/2.结构体生命周期/lifetime_struct.md
0 → 100644
浏览文件 @
ba11e301
# 结构体里的生命周期(I)
以下对Rust结构体生命周期使用中,正确的代码是哪一项?
## 答案
```
rust
struct
Piece
<
'a
>
{
slice
:
&
'a
[
u8
]
// 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参
}
// Piece的定义里面,'a 表示vec的生命周期,
// 下面的例子调用,vec的生命周期至少应该大于等于piece的生命周期
// 简单说vec存活的作用域应该大于等于piece的存活作用域
fn
test
(){
let
vec
=
Vec
::
<
u8
>
::
new
();
let
piece
=
Piece
{
slice
:
vec
.as_slice
()};
}
```
## 选项
### 结构体内含有的引用变量,函数返回后,生命周期就结束了
```
rust
struct
Piece
<
'a
>
{
slice
:
&
'a
[
u8
]
// 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参
}
fn
test_2
()
->
Piece
{
let
vec
=
Vec
::
<
u8
>
::
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
::
<
u8
>
::
new
();
let
piece
=
Piece
{
slice
:
vec
.as_slice
()};
}
```
### 结构体内持有的变量应该在构建的时候存活
```
rust
struct
Piece
<
'a
>
{
slice
:
&
'a
[
u8
]
// 表明slice是来自外部对象的一个借用,'a只是一个生命周期形参
}
fn
test
(){
let
vec
{
vec
=
Vec
::
<
u8
>
::
new
();
}
let
piece
=
Piece
{
slice
:
vec
.as_slice
()};
}
```
data/2.rust中阶/2.生命周期/config.json
浏览文件 @
ba11e301
{
"node_id"
:
"rust-d38898d251df42d49f5f367f077ede55"
,
"keywords"
:
[],
"children"
:[],
"export"
:
[
"lifetime.json"
]
"keywords"
:
[]
}
\ No newline at end of file
data/2.rust中阶/2.生命周期/lifetime.json
已删除
100644 → 0
浏览文件 @
7b642af1
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"lifetime.md"
,
"notebook_enable"
:
false
}
\ No newline at end of file
data/tree.json
浏览文件 @
ba11e301
...
...
@@ -330,7 +330,22 @@
"生命周期"
:
{
"node_id"
:
"rust-d38898d251df42d49f5f367f077ede55"
,
"keywords"
:
[],
"children"
:
[]
"children"
:
[
{
"函数生命周期"
:
{
"node_id"
:
"rust-7f73462bc1d54b52891a4c2fc84f2a19"
,
"keywords"
:
[],
"children"
:
[]
}
},
{
"结构体生命周期"
:
{
"node_id"
:
"rust-f7b9ef1329c745959fb1ca56061b4ae5"
,
"keywords"
:
[],
"children"
:
[]
}
}
]
}
},
{
...
...
src/tree.py
浏览文件 @
ba11e301
...
...
@@ -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
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录