mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-22 22:23:28 +08:00
Initial commit.
This commit is contained in:
8
func_pointer/Cargo.toml
Normal file
8
func_pointer/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "func_pointer"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
30
func_pointer/src/main.rs
Normal file
30
func_pointer/src/main.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
fn add_one(x: i32) -> i32 {
|
||||
x + 1
|
||||
}
|
||||
|
||||
fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
|
||||
f(arg) + f(arg)
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let answer = do_twice(add_one, 5);
|
||||
|
||||
println! ("答案为:{}", answer);
|
||||
|
||||
let list_of_numbers = vec! [1, 2, 3];
|
||||
let list_of_strings: Vec<String> =
|
||||
list_of_numbers.iter().map(ToString::to_string).collect();
|
||||
|
||||
println! ("结果为:{:?}", list_of_strings);
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Status {
|
||||
Value(u32),
|
||||
Stop,
|
||||
}
|
||||
|
||||
let mut list_of_statuses: Vec<Status> = (0u32..20).map(Status::Value).collect();
|
||||
list_of_statuses.append(&mut vec! [Status::Stop]);
|
||||
println! ("list_of_statuses: {:?}", list_of_statuses);
|
||||
}
|
||||
Reference in New Issue
Block a user