Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_rust
提交
0dd7b59f
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看板
提交
0dd7b59f
编写于
8月 23, 2022
作者:
J
jackymao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add functional_programming
上级
ef03e634
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
176 addition
and
1 deletion
+176
-1
data/1.rust初阶/2.rust基本概念/12.函数式编程/config.json
data/1.rust初阶/2.rust基本概念/12.函数式编程/config.json
+4
-1
data/1.rust初阶/2.rust基本概念/12.函数式编程/functional_programming.json
.../1.rust初阶/2.rust基本概念/12.函数式编程/functional_programming.json
+8
-0
data/1.rust初阶/2.rust基本概念/12.函数式编程/functional_programming.md
data/1.rust初阶/2.rust基本概念/12.函数式编程/functional_programming.md
+164
-0
未找到文件。
data/1.rust初阶/2.rust基本概念/12.函数式编程/config.json
浏览文件 @
0dd7b59f
...
...
@@ -2,7 +2,9 @@
"node_id"
:
"rust-4844e545058847c8b3198330fcedd12c"
,
"keywords"
:
[],
"children"
:
[],
"export"
:
[],
"export"
:
[
"functional_programming.json"
],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
}
\ No newline at end of file
data/1.rust初阶/2.rust基本概念/12.函数式编程/functional_programming.json
0 → 100644
浏览文件 @
0dd7b59f
{
"type"
:
"code_options"
,
"author"
:
"jackymao_com"
,
"source"
:
"functional_programming.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
""
}
\ No newline at end of file
data/1.rust初阶/2.rust基本概念/12.函数式编程/functional_programming.md
0 → 100644
浏览文件 @
0dd7b59f
# 函数式编程
虽然 rust 语言主要是指令式编程,但也借鉴了很多函数式编程的精华,譬如 rust 中的迭代器、闭包、高阶函数等。
Rust 迭代器函数式编程主要用到的有:
-
map
-
for_each
-
filter
-
filter_map
-
fold
-
reduce
-
zip
-
collect
用法如下:
```
rust
#![allow(unused_variables)]
#![allow(dead_code)]
use
std
::
collections
::
HashMap
;
fn
main
()
{
let
v
:
Vec
<
_
>
=
(
0
..
20
)
.collect
();
// println!("{:?}", v);
let
values
=
vec!
[
1
,
2
,
3
,
4
,
5
]
.into_iter
();
let
_
sum
=
values
.clone
()
.reduce
(|
acc
,
x
|
acc
+
x
);
// println!("{:?} {:?}", &values, _sum);
let
m
=
(
1
..
5
)
.fold
(
2
,
|
mul
,
x
|
mul
*
x
);
let
n
=
(
1
..
101
)
.fold
(
0
,
|
a
,
b
|
a
+
b
);
// println!("{:?} {:?}", m, n);
// fold is like reduce but can return an accumulator of a different type than the items of the iterator
let
values
=
vec!
[
"Hello"
,
"World"
,
"!"
]
.into_iter
();
let
_
sentence
=
values
.clone
()
.fold
(
String
::
new
(),
|
acc
,
x
|
acc
+
x
);
// println!("{:?} {:?}", values, _sentence);
let
v
=
vec!
[
"Hello"
,
"World"
,
"!"
]
.into_iter
();
let
w
:
Vec
<
String
>
=
v
.clone
()
.map
(
String
::
from
)
.collect
();
// println!("{:?} {:?}", v, w);
let
even
:
Vec
<
_
>
=
(
0
..
10
)
.filter
(|
x
|
x
%
2
==
0
)
.collect
();
// println!("{:?}", even);
let
v
=
vec!
[
-
1
,
2
,
-
3
,
4
,
5
]
.into_iter
();
let
_
positive_numbers
:
Vec
<
i32
>
=
v
.clone
()
.inspect
(|
x
|
println!
(
"Before filter: {}"
,
x
))
.filter
(|
x
:
&
i32
|
x
.is_positive
())
.inspect
(|
x
|
println!
(
"After filter: {}"
,
x
))
.collect
();
println!
(
"{:?} {:?}"
,
v
,
_
positive_numbers
);
let
v
:
Vec
<
i32
>
=
[
1
,
2
,
3
]
.into_iter
()
.map
(|
x
|
x
+
1
)
.rev
()
.collect
();
assert_eq!
(
v
,
[
4
,
3
,
2
]);
let
names
=
vec!
[
"Alice"
,
"Bob"
,
"Charlie"
];
let
scores
=
vec!
[
"90"
,
"80"
,
"95"
];
let
score_map
:
HashMap
<
_
,
_
>
=
names
.iter
()
.zip
(
scores
.iter
())
.collect
();
println!
(
"{:?} {:?} {:?}"
,
names
,
scores
,
score_map
);
fn
filter_map_test
()
{
let
v
=
vec!
[
"Hello"
,
"World"
,
"!"
]
.into_iter
();
let
w
:
Vec
<
String
>
=
v
.clone
()
.filter_map
(|
x
|
{
if
x
.len
()
>
2
{
Some
(
String
::
from
(
x
))
}
else
{
None
}
})
.collect
();
println!
(
"{:?} {:?}"
,
v
,
w
);
assert_eq!
(
w
,
vec!
[
"Hello"
.to_string
(),
"World"
.to_string
()]);
}
filter_map_test
();
}
```
使用的时候如果编译出现错误,就要注意到有的函数是会消耗迭代器的元素,有的会返回一个迭代器,有的是惰性的。
下面的例子能正确编译的是:
## 答案
```
rust
fn
main
()
{
let
arr
=
vec!
[
0
;
10
];
for
i
in
arr
.iter
()
{
println!
(
"{:?}"
,
i
)
}
println!
(
"{:?}"
,
arr
);
}
```
## 选项
###
```
rust
fn
main
()
{
let
arr
=
vec!
[
0
;
10
];
for
i
in
&
arr
.iter
()
{
println!
(
"{:?}"
,
i
)
}
println!
(
"{:?}"
,
arr
);
}
```
###
```
rust
fn
main
()
{
let
arr
=
vec!
[
0
;
10
];
for
i
in
arr
{
println!
(
"{:?}"
,
i
)
}
println!
(
"{:?}"
,
arr
);
}
```
###
```
rust
fn
main
()
{
let
arr
=
vec!
[
0
;
10
];
for
i
in
arr
.into_iter
()
{
println!
(
"{:?}"
,
i
)
}
println!
(
"{:?}"
,
arr
);
}
```
<!--
ref:
https://kerkour.com/rust-functional-programming
https://doc.rust-lang.org/std/iter/struct.Map.html
https://www.cnblogs.com/chenguifeng/p/12243999.html
https://course.rs/advance/functional-programing/iterator.html#消费者与适配器
-->
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录