rust

【运维经】第9章——rust(cargo)加速

不羁的心 提交于 2020-02-02 11:40:53
目录 rust(cargo)加速 因为本人主营业务是C++,那么就要更了解C++的敌人——RUST(听说她要取代C++)。但不得不说,RUST的工具链还是非常值得称赞的,后续会开几期来讲讲RUST工具链,今天还是谈谈加速。 找到cargo的配置,~/.cargo/config ( base ) frank@deepin:~$ cd .cargo/ ( base ) frank@deepin:~/.cargo$ ll 总用量 20 drwxr-xr-x 2 frank frank 4096 1月 22 14:33 bin -rw-r--r-- 1 frank frank 168 1月 1 21:58 config -rw-r--r-- 1 frank frank 37 12月 31 11:56 env drwxr-xr-x 3 frank frank 4096 1月 1 21:58 git drwxr-xr-x 5 frank frank 4096 1月 22 13:52 registry 修改配置,编辑config,添加下面内容。 [source.crates-io] registry = "[https://github.com/rust-lang/crates.io-index](https://github.com/rust-lang/crates.io-index)"

How can I create an efficient iterator of chars from stdin with Rust?

做~自己de王妃 提交于 2020-02-02 08:11:25
问题 Now that the Read::chars iterator has been officially deprecated, what is the the proper way to obtain an iterator over the chars coming from a Reader like stdin without reading the entire stream into memory? 回答1: The corresponding issue for deprecation nicely sums up the problems with Read::chars and offers suggestions: Code that does not care about processing data incrementally can use Read::read_to_string instead. Code that does care presumably also wants to control its buffering strategy

How can I create an efficient iterator of chars from stdin with Rust?

。_饼干妹妹 提交于 2020-02-02 08:09:13
问题 Now that the Read::chars iterator has been officially deprecated, what is the the proper way to obtain an iterator over the chars coming from a Reader like stdin without reading the entire stream into memory? 回答1: The corresponding issue for deprecation nicely sums up the problems with Read::chars and offers suggestions: Code that does not care about processing data incrementally can use Read::read_to_string instead. Code that does care presumably also wants to control its buffering strategy

How can I do a mutable borrow in a for loop?

此生再无相见时 提交于 2020-02-02 05:39:39
问题 I tried: let mut vec = [1,2,3]; for mut x in &vec { *x=3; } for mut &x in &vec { x=3; } for mut *x in &vec { x=3; } for mut x in mut &vec { *x=3; } for mut x in &(mut vec) { *x=3; } None of these work; how should I do it? 回答1: You may want to re-read The Rust Programming Language section on mutability: You can also create a reference to it, using &x , but if you want to use the reference to change it, you will need a mutable reference: let mut x = 5; let y = &mut x; fn main() { let mut array

Type inference and borrowing vs ownership transfer

我只是一个虾纸丫 提交于 2020-02-02 02:14:06
问题 I am learning Rust and I've run into some confusing behaviour. The following code compiles fine and works as expected ( edit : added code other than test function, previously omitted): struct Container<'a> { contents : &'a mut i32, } fn main() { let mut one = Container { contents: &mut 5 }; test(&mut one); println!("Contents: {}",one.contents); } fn test<'a>(mut x : &'a mut Container) { *x.contents += 1; let y = x; *y.contents += 1; x = y; println!("{:?}",*x.contents) } Now in the statement

How do I declare a static mutable variable without assignment?

 ̄綄美尐妖づ 提交于 2020-02-02 00:24:48
问题 I tried the following struct mbuf { cacheline: *mut [u64], // great amount of rows follows below // .......... } static mut arr: [mbuf; 32]; // Q1 my main aim // something as before but using Vec; // Q2 also main aim fn main() { // let static mut arr: [mbuf; 32]; // Q3 also doesn't work // static mut arr: [mbuf; 32]; // Q3 also doesn't work } and got error src/main.rs:74:29: 74:30 error: expected one of `+` or `=`, found `;` src/main.rs:74 static mut arr: [mbuf; 32]; ^ Q1,Q2,Q3 - Is it

How can I copy a vector to another location and reuse the existing allocated memory?

末鹿安然 提交于 2020-02-01 04:44:06
问题 In C++, to copy the contents of a vector to another vector we use the assignment operator dest = src . However, in Rust src would be moved into dest and no longer usable. I know the simplest answer is to do dest = src.clone() (for the sake of this question we'll assume T in Vec<T> is Clone ). However - if I'm understanding correctly - this creates a brand new third vector with the copied contents of src and moves it into dest , throwing away dest 's dynamically allocated array. If this is

What is the correct way to write `Vec<u16>` content to a file?

。_饼干妹妹 提交于 2020-02-01 03:13:27
问题 I'm having trouble writing Vec<u16> content to a file: use std::fs::File; use std::io::{Write, BufWriter}; use std::mem; #[derive(Debug, Copy, Clone, PartialEq)] pub enum ImageFormat { GrayScale, Rgb32, } #[derive(Debug, Copy, Clone, PartialEq)] pub struct ImageHeader { pub width: usize, pub height: usize, pub format: ImageFormat, } pub struct Image { pub header: ImageHeader, pub data: Vec<u16>, } fn write_to_file(path: &str, img: &Image) -> std::io::Result<()> { let f = try!(File::create

How do I convert a string to a list of chars?

混江龙づ霸主 提交于 2020-02-01 00:38:12
问题 Since a string supports iteration but not indexing, I would like to convert a string into a list of chars. I have "abc" and I want ['a', 'b', 'c'] . It can be any type as long as I can index into it. A Vec<char> or a [char; 3] would be fine, other ideas would also be interesting! Faster would be better since I am dealing with very long strings. A version that is more efficient when assuming that the string is ASCII would also be cool. 回答1: Use the chars method on String or str : fn main() {

Rust macro开发新手入门

北慕城南 提交于 2020-01-31 17:45:50
Rust语言最强大的一个特点就是可以创建和利用宏/Macro。不过创建Rust宏看起来挺复杂,常常令刚接触Rust的开发者心生畏惧。这片文章的目的就是帮助你理解Rust Macro的基本运作原理,学习如何创建自己的Rust宏。 相关链接: 在线学编程 1、什么是Rust的宏/Macro? 如果你尝试过Rust,应该已经用过Rust的宏了: println! 。这个宏可以在终端输出一行文本,并且支持变量的插值。 简单地说,Rust宏让你可以发明自己的语法,编写出可以自行展开的代码,也就是我们通常所说的 元编程 ,你甚至可以用Rust宏来创作自己的DSL。 Rust宏的基本运作机制就是:首先匹配宏规则中定义的模式,然后将匹配结果绑定到变量,最后展开变量替换后的代码。 不理解也没有关系,让我们继续看。 2、如果创建Rust宏/Macro? 可以使用Rust预置的 macro_rules! 宏来创建一个新的Rust宏。 下图展示了如何创建一个空白的Rust宏: hey! ,这个宏什么功能也没有,我们现在只关注它的结构: () => {} 看起来很神秘,因为它不是标准的rust语法,是macro_rules!这个宏自己发明的,用来表示一条宏规则, => 左边是匹配模式,右边是等待展开的代码: 左边的小括号部分是Rust宏的匹配器/Matcher,用来匹配模式并捕捉变量