rust

转: Rust中的Pin详解 【Rust语言中文社区】

若如初见. 提交于 2020-05-08 09:57:45
Rust中的Pin详解 原创 automanyang Rust语言中文社区 昨天 https://mp.weixin.qq.com/s/PjctbPbyR5OeaqTHZdB5uQ 相关概念 Pin<P> 这是一个struct,作用就是将P所指向的T在内存中固定住,不能移动。说白一些,就是不能通过safe代码拿到&mut T。 Pin 定义如下: pub struct Pin<P> { pointer: P, } Unpin 这是一个trait,定义在std::marker中,如果一个T: Unpin,就说明T在pin后可以安全的移动,实际就是可以拿到&mut T。 pub auto trait Unpin {} !Unpin 对Unpin取反,!Unpin的双重否定就是pin。如果一个类型中包含了PhantomPinned,那么这个类型就是!Unpin。 pub struct PhantomPinned; #[stable(feature = "pin", since = "1.33.0")] impl !Unpin for PhantomPinned {} Pin 的实现 我们这里只关注safe方法,重点是new方法: impl<P: Deref<Target: Unpin>> Pin<P> { pub fn new(pointer: P) -> Pin<P> { unsafe

“cannot find macro” error in the macro's own doc test

a 夏天 提交于 2020-05-08 06:42:50
问题 I am trying to add documentation tests to a Rust macro that I'm exporting. Something like this: /// Usage: /// /// ``` /// let x = addone!(100); /// ``` #[macro_export] macro_rules! addone { ($x:expr) => ($x + 1) } If I run cargo test on this, I get failures: ---- src/lib.rs - addone (line 3) stdout ---- error: cannot find macro `addone!` in this scope --> src/lib.rs:2:9 | 2 | let x = addone!(100); | ^^^^^^ I can't think of a legal way of adding macro_use inside the doc test, so no luck there

“cannot find macro” error in the macro's own doc test

好久不见. 提交于 2020-05-08 06:38:09
问题 I am trying to add documentation tests to a Rust macro that I'm exporting. Something like this: /// Usage: /// /// ``` /// let x = addone!(100); /// ``` #[macro_export] macro_rules! addone { ($x:expr) => ($x + 1) } If I run cargo test on this, I get failures: ---- src/lib.rs - addone (line 3) stdout ---- error: cannot find macro `addone!` in this scope --> src/lib.rs:2:9 | 2 | let x = addone!(100); | ^^^^^^ I can't think of a legal way of adding macro_use inside the doc test, so no luck there

Is there a way to have a Rust closure that moves only some variables into it?

给你一囗甜甜゛ 提交于 2020-05-06 17:46:53
问题 I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this value, they do slightly different work. Each of these threads should be able to read the general settings struct. use crossbeam; // 0.7.3 struct Settings { // ... many fields } const MAX_FEASIBLE_SCORE: u8 = 10; fn example(settings: Settings) {

Is there a way to have a Rust closure that moves only some variables into it?

依然范特西╮ 提交于 2020-05-06 17:45:17
问题 I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this value, they do slightly different work. Each of these threads should be able to read the general settings struct. use crossbeam; // 0.7.3 struct Settings { // ... many fields } const MAX_FEASIBLE_SCORE: u8 = 10; fn example(settings: Settings) {

2020年编程语言的发展方向

与世无争的帅哥 提交于 2020-05-05 23:50:27
在进入新的十年之际,我们请编程专家(包括我们自己的O'Reilly的几位作者和讲师⁠)对他们对于某些老牌玩家和快速发展的语言所存储内容的想法进行了思考。 Python 今年Python的最大新闻是,创建者和“一生的仁慈独裁者” Guido van Rossum退休,将Python交给了Python指导委员会。到目前为止,这是权力的无痛转变,正如Python Crash Course的作者埃里克·马特斯(Eric Matthes)所认为的那样,这不足为奇,因为“圭多(Guido)长期以来保持着自己以及他在社区中的角色。” 2020年还将终止对Python 2.7的支持,这很可能导致其在坚持派中的头痛问题。同时,Python仍然是数据科学的首选语言。 对于Matthes而言,Python的一个令人激动的方面是“来自一个社区的各种有趣且关键的项目已经诞生了,长期以来,它们都是有意建立多样性的。” Python指导委员会成员和CPython的核心开发人员Carol Willing也庆祝了这些项目,例如Binder服务,该服务通过在Jupyter Notebook中创建可执行环境来促进可重复的研究,尤其是当它们超出其最初的目标时。她指出,“活页夹去年在许多Python会议上被广泛用于教学讲习班和教程。” Willing还向CircuitPython和Mu项目大声疾呼,问道:“谁不喜欢硬件

Rust 强制类型转换 使用as 将不变编程可变

旧城冷巷雨未停 提交于 2020-05-05 19:25:46
fn g(&self)->&mut Self{ unsafe { & mut * ( self as * const Self as * mut Self)} } as 只能转换基本数据类型 ,结构体类型 只能转换指针, 不能转换结构体引用,应该是结构体引用也被视为了复杂的结构体类型。 来源: oschina 链接: https://my.oschina.net/u/4271740/blog/4267527

Yew 框架 (二)子组件的创建和渲染

被刻印的时光 ゝ 提交于 2020-05-05 00:51:08
Yew 子组件的创建和渲染 前一篇 Yew框架(一) 应用初始化过程 我们了解一应用启动的过程,后续我将探索Yew中的一些功能是如何实现的,先来看看 子组件的创建渲染过程。 Yew 支持在组件的视图中嵌套组件,支持给子组件传递属性,下图是测试代码扩展前后的对比: 上面的代码主要是创建VChild对象,不是特别难,但要看懂它做了什么,为什么要这样做,还得先看 Properties 宏对组件的属性做了什么。 Properties 属性 这是我们自定义的Button组件的属性声明: 扩展后: 代码一下变多了。 首先是定义了一个 PropsWrapper,包含两个属性,但属性的类型都为Option类型,并且实现了Default特性,从后面的代码可以看到它就是PropsBuilder 用来存储过程信息的。 定义了两个结构体,都实现PropsBuilderStep特性,定义一个PropsBuilder结构体,有一个泛型参数,参数实现PropsBuilderStep。这是一个状态机在Rust的实现模式,用泛型参数来表示构建器的状态,通过构建器的方法来进行状态迁移,不同的状态可以调用不同的方法。 因为我们只有两个属性,所以 PropsBuilderStep 只有两个状态。PropsBuilderStep_missing_required_prop_text 是初始状态,该状态来可以调用

Win7 VSCode 离线安装Rust语言及环境配置

穿精又带淫゛_ 提交于 2020-05-04 06:45:35
前置依赖 装过Visual Studio或Visual Studio Build Tool 2015 下载Rust离线安装包 https://forge.rust-lang.org/other-installation-methods.html 写本文时使用的版本为1.37 https://static.rust-lang.org/dist/rust-1.37.0-x86_64-pc-windows-msvc.msi 安装时选Advanced 换下路径,如 E:\Rust stable MSVC 1.37\ 接下来把RLS选项勾上 装好后到Gayhub下载配置模板节省头发。 https://github.com/RoteErde/RustVSCodeTemplate 打开VSCode,菜单 File > Open Folder 打开下载的模板目录 然后VSCode扩展搜Gayhub中提到的3个扩展 Rust (rls) C/C++ Native Debug 装好后打开src/main.rs,修改为 fn main() { let mut a = 0 ; a = 3 ; a = 5 ; a = 10 ; println !( " Hello, world! " ); } 设置断点选项,菜单 File > Preferences > Setting中搜索break,勾选Allow