上一页 1 ··· 58 59 60 61 62 63 64 65 66 ··· 164 下一页

2020年12月28日

摘要: use std::ops::Deref; struct MyBox<T>(T); impl<T> MyBox<T> { fn new(x: T) -> MyBox<T> { MyBox(x) } } //impl<T> Deref for MyBox<T> { // type Target = T; 阅读全文
posted @ 2020-12-28 09:52 tycoon3 阅读(435) 评论(0) 推荐(0)
摘要: https://kaisery.github.io/trpl-zh-cn/ch04-02-references-and-borrowing.html 示例 4-5 中的元组代码有这样一个问题:我们必须将 String 返回给调用函数,以便在调用 calculate_length 后仍能使用 Stri 阅读全文
posted @ 2020-12-28 09:28 tycoon3 阅读(382) 评论(0) 推荐(0)

2020年12月26日

摘要: struct Rec { width : u32, length : u32 } fn process(rec1: Rec) -> Rec { let mut rec2 = rec1; rec2.width = 10; rec2.length = 11; rec2 } fn main() { let 阅读全文
posted @ 2020-12-26 22:28 tycoon3 阅读(153) 评论(0) 推荐(0)
摘要: [root@bogon fnmut]# cat src/main.rs #[derive(Debug)] struct f_closure{ name: String, } impl f_closure{ fn fn_call( self) -> String{ self.name } } fn g 阅读全文
posted @ 2020-12-26 12:08 tycoon3 阅读(365) 评论(0) 推荐(0)
摘要: cat src/main.rs #[derive(Debug)] struct f_closure{ name: String, } impl f_closure{ fn fn_call( self) -> String{ self.name } } fn get_string<T>(name: S 阅读全文
posted @ 2020-12-26 12:02 tycoon3 阅读(964) 评论(0) 推荐(0)
摘要: cat src/main.rs #[derive(Debug)] struct f_closure{ name: String, } impl f_closure{ fn fn_call(& self) -> String{ self.name } } fn main() { let name = 阅读全文
posted @ 2020-12-26 11:29 tycoon3 阅读(1965) 评论(0) 推荐(0)

2020年12月25日

摘要: 在学习rust的过程中,了解了高阶函数的知识,说白了就是函数可以作为参数,也可以返回一个函数,正好最近看到了scip公开课里面讲高阶过程这一章,有个求平方根的过程,里面的lisp实现相当的简洁优美,基本是对数学公式的形式化书写。这里用rust实现一下,看看区别有多大。 主要实现了传入函数作为参数,并 阅读全文
posted @ 2020-12-25 20:38 tycoon3 阅读(512) 评论(0) 推荐(0)
摘要: Rust点滴: 闭包那点事儿 概述 我们常常需要回调函数的功能, 需要函数并不是在创建时执行, 而是以回调的方式, 在需要的时候延迟执行. 并且, 常常需要在函数中获取环境中的一些信息, 又不需要将其作为函数参数传入. 这种应用场景就需要闭包这一工具了. 闭包是持有外部环境变量的函数. 所谓外部环境 阅读全文
posted @ 2020-12-25 20:37 tycoon3 阅读(486) 评论(0) 推荐(0)
摘要: 迭代器与闭包 fn main() { let vec_1 = vec![1, 2, 3,4,5]; let vec_2 = vec!["a","b","c","d","e"]; //对vec的`iter()`举出`i32`,(通过用`x`匹配)把它解构成`i32`。 //对vec的`into_ite 阅读全文
posted @ 2020-12-25 20:31 tycoon3 阅读(223) 评论(0) 推荐(0)
摘要: 返回闭包错误例子: fn returns_closure() -> Fn(i32) -> i32 { |x| x + 1 }正确例子: fn returns_closure() -> Box<dyn Fn(i32) -> i32> { Box::new(|x| x + 1)} fn main() { 阅读全文
posted @ 2020-12-25 20:23 tycoon3 阅读(108) 评论(0) 推荐(0)
上一页 1 ··· 58 59 60 61 62 63 64 65 66 ··· 164 下一页

导航