rust

How do I convert a Vector of bytes (u8) to a string

时光总嘲笑我的痴心妄想 提交于 2020-01-08 16:25:06
问题 I am trying to write simple TCP/IP client in Rust and I need to print out the buffer I got from the server. How do I convert the u8 vector to a String for printing? 回答1: To convert a slice of bytes to a string slice (assuming a UTF-8 encoding): use std::str; // // pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> // // Assuming buf: &[u8] // fn main() { let buf = &[0x41u8, 0x41u8, 0x42u8]; let s = match str::from_utf8(buf) { Ok(v) => v, Err(e) => panic!("Invalid UTF-8 sequence: {}", e), }

What is a crate attribute and where do I add it?

非 Y 不嫁゛ 提交于 2020-01-08 14:18:09
问题 In order to get a feel for how Rust works, I decided to look at a little terminal-based text editor called Iota. I cloned the repository and ran cargo build only to be told: error: *if let* syntax is experimental help: add #![feature(if_let)] to the crate attributes to enable Where am I supposed to add #![feature(if_let)] to the crate attributes? 回答1: A crate attribute is an attribute ( #[...] ) that applies to the enclosing context ( #![...] ). This attribute must be added to the top of your

Rust 1.0.0-alpha 即将释出

走远了吗. 提交于 2020-01-07 19:16:37
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Rust 1.0.0-alpha 即将释出 发行说明和参考资料: Version 1.0.0-alpha (January 2015) ---------------------------------- * ~2300 changes, numerous bugfixes * Highlights * The language itself is considered feature complete for 1.0, though there is a significant amount of cleanup and bugfixes remaining. * Nearly 50% of the public API surface of the standard library has been declared 'stable'. Those interfaces will not change. * Most crates that are not `std` have been moved out of the Rust distribution into the Cargo ecosystem so they can evolve separately and don't need to be

Rust 1.0.0-alpha 释出

不打扰是莪最后的温柔 提交于 2020-01-07 18:28:47
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 今天Rust 1.0.0-alpha 按计划如期释出。 有几个地方或以帮助大家感受这一门语言 1、官方提供的《 A 30-minute Introduction to Rust 》。可以在30分钟内对Rust语言有基本的了解。 2、官方出版的电子书 《 The Rust Programming Language 》。 官方首次发布该电子书,也是官方发布的第一本编程书,相比之前为新手准备的Guide,该书更有层次,更全面,还有很多编程实践在里面,可以更好地 帮助新手系统的了解语言的全貌。 【推荐】 3、由Steve Klabnik维护的《 Rust by Example 》。它为Rust的每功能点提供一个示例程序,并且由浅入深、分门别类的组织在一起,学完一个示例可以按下一步,继续学习第二个示例,为新手提供了一个良好的实践向导。此外它还提供了一个可以做实验的沙盒,用户不用安装Rust也可以写代码,做实验。 【推荐】 详细请看:http://doc.rust-lang.org/1.0.0-alpha/ 来源: oschina 链接: https://my.oschina.net/u/10896/blog/365726

Fighting with / not understanding Rust borrow checker

旧街凉风 提交于 2020-01-07 06:28:08
问题 I started writing Rust code a few days ago, and just now had my first encounter with the borrow checker. #[derive(Clone, Eq, Debug, PartialEq)] pub struct Vm<'a> { instructions: Rc<InstructionSequence>, pc: usize, stack: Vec<Value<'a>>, frames: Vec<Frame<'a>>, } impl<'a> Vm<'a> { pub fn run(&'a mut self) { loop { let instruction = self.instructions.get(self.pc).unwrap(); match instruction { &Instruction::Push(ref value) => { let top_activation = &mut self.frames.last_mut().unwrap().activation

How can I make a window override-redirect with glutin?

浪子不回头ぞ 提交于 2020-01-07 04:28:11
问题 I'm creating a program that uses glutin, and I want to provide a command-line flag to make the window override-redirect so it can be used as a desktop wallpaper for certain window managers that don't support the desktop window type. I've done a lot of research and managed to cobble together a block of code that I thought would work, using the provided xlib display and window from glutin. Here is my existing code: unsafe { use glutin::os::unix::WindowExt; let x_connection = std::sync::Arc::

What does “expected value, found trait” mean?

▼魔方 西西 提交于 2020-01-07 04:25:15
问题 I am trying to build a scene manager that lets you push scenes onto a stack. When each scene is popped off the stack, it is run until stopped and then we repeat. An example is a menu in a game; which is one scene. When you close it, the game map behind it is another scene. pub trait Scene { fn start(&mut self) {} fn update(&mut self) {} fn stop(&mut self) {} fn is_active(&self) -> bool { return false; } } pub struct SceneManager { scenes: Vec<Box<Scene>>, } impl SceneManager { fn new<T>(scene

我是如何从Java转型为Go区块链工程师

徘徊边缘 提交于 2020-01-07 00:53:20
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我是如何从Java转型为Go区块链工程师 本文来自于一个比原链核心开发的陈述 前言 本人在加入比原链之前一直是做Java开发的,当初看到Go还有点犹豫,还怕过不了面试,结果是否掌握一门语言的考量确实没那么高,我顺利入职比原链,并在半个月内很快掌握Go并能够进行核心项目的开发。 Java语言在较大的成熟项目上具有优势,但是在区块链开发中确实会有很多短处,比如在协程处理上,还有Java语言本身不够灵活等等,当然选择使用Go主要是因为其在区块链的流行程度。 区块链的流行语言 在区块链公链的开发圈子里,我们找到了一些流行的编程语言,有C++、Golang、Python和最近新起的Rust等等。 稍微对比较有名的项目采用的编程语言做个统计,如下图: 老一代的公链,比如Bitcoin,Litcoin一般使用C/C++较多(我们看看那个时间,当时Go还没起来呢),新一代的公链比如以太坊,联盟链翘楚超级账本,开始较多使用Go语言,当然我们看到Rust的发展势头也很猛,近两年的很多公链比如波卡,Grin都开始采用Rust语言开发了。 Go语言的优势 部署简单 Go 编译生成的是一个静态可执行文件,除了 glibc 外没有其他外部依赖。这让部署变得异常方便:目标机器上只需要一个基础的系统和必要的管理、监控工具

Sending channel signal from a closure

安稳与你 提交于 2020-01-06 18:12:59
问题 I'm trying to send a signal from within a closure, using the following code. use std::thread; use std::sync::mpsc::channel; fn main() { let (tx, rx) = channel(); let t1 = thread::spawn(move || { watch(|x| tx.send(x)); }); let t2 = thread::spawn(move || { println!("{:?}", rx.recv().unwrap()); }); let _ = t1.join(); let _ = t2.join(); } fn watch<F>(callback: F) where F : Fn(String) { callback("hello world".to_string()); } However, it fails compiling raising the following error: src/test.rs:8:19

2019年,又是一个成长年!

我与影子孤独终老i 提交于 2020-01-06 16:56:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 大胡子 是我特别佩服的一位老师,第一次知道他是17年偶尔一次线上讲座,具体题目忘记了,内容大概是关于"技术人如何赚钱",很real,告诉一些晚辈如何赚钱,以及拥有一些赚钱思维,我觉得这个真的很酷。后来才了解到他的 疯人院 和星球,所以立马入会,所幸会费不是很高(这要是再高个几百的,当时的我是肯定不会去"割韭菜"社群)。不过,在这两年里,虽然不曾在社群发表自己的观点,但是通过群里各种大牛、前辈们的探讨,在加上自己的思考,也的确让自己再技术之外成长了很多,所以,在这里我向大家极力推荐大胡子老师,他的公众号是 姜胡说 . 年初了,总该总结总结过去一年的成长和收获,也该去梳理梳理新一年的征程如何走下去,这里有一篇胡子老师的文章,觉得很有意思,感兴趣的可以去仔细读读 早知道这个,我至少可以少奋斗3年 ,虽然有点标题档了,但不妨碍这依然是一篇值得看和值得思考的文章. 以下摘录几个观点,用于鞭策自己: 1.Stay hungry,Stay foolish Stay hungry,Stay foolish! 是乔布斯老爷子在斯坦福大学大学演讲时结尾的一句话,翻译过来即为"求知若饥,虚心若愚"。 记得那个时候我还在上大学,乔老爷子的自传以及演讲都那么激动人心,我还曾经将 Stay hungry,Stay foolish!