rust

What is the purpose of `&` before the loop variable?

点点圈 提交于 2020-02-11 08:40:21
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

What is the purpose of `&` before the loop variable?

谁都会走 提交于 2020-02-11 08:40:20
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

What is the purpose of `&` before the loop variable?

眉间皱痕 提交于 2020-02-11 08:39:08
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

The best way to iterate over sections of 2D arrays in Rust

与世无争的帅哥 提交于 2020-02-07 17:21:40
问题 I'm learning Rust by porting a small program that was written in C. It works with comparable performance to the C version. Still, I can't help but feeling my code could be better, hence this post. The following code solves a matrix equation using Gaussian elimination, which requires iterating over portions of an array. pub struct MatrixEquation { vec: Vec<f64>, mat: Vec<Vec<f64>>, } impl MatrixEquation { fn solve(&mut self) { let size = self.vec.len(); // Transform matrix to upper triangular

The best way to iterate over sections of 2D arrays in Rust

て烟熏妆下的殇ゞ 提交于 2020-02-07 17:19:39
问题 I'm learning Rust by porting a small program that was written in C. It works with comparable performance to the C version. Still, I can't help but feeling my code could be better, hence this post. The following code solves a matrix equation using Gaussian elimination, which requires iterating over portions of an array. pub struct MatrixEquation { vec: Vec<f64>, mat: Vec<Vec<f64>>, } impl MatrixEquation { fn solve(&mut self) { let size = self.vec.len(); // Transform matrix to upper triangular

用rust写一个玩具解释器3

我们两清 提交于 2020-02-06 12:49:42
项目目录 项目地址 画质有点渣 其实不应该创建太多的包,因为里面其实只有一个有效文件(mod.rs和test.rs算是辅助) . 所以说目录可以更简单一些. token 在上文中提到了把token看为字符串, 而其大致分为两种. 我们预先定义的 比如 +,-,*,/,if,return,fn和let等 留给用户的 比如: 变量名,整数 //token/token.rs //不想下载第三方包, 所以这样子是一个可接受的选择 use std : : collections : : HashMap ; pub fn lookup_ident ( ident : & String ) -> TokenType { let mmap : HashMap < String , TokenType > = { let mut map = HashMap : : new ( ) ; map . insert ( "fn" . to_string ( ) , FUNCTION ) ; map . insert ( "let" . to_string ( ) , LET ) ; map . insert ( "true" . to_string ( ) , TRUE ) ; map . insert ( "false" . to_string ( ) , FALSE ) ; map . insert

How to convert a Future into a Stream?

蹲街弑〆低调 提交于 2020-02-06 08:02:10
问题 I'm trying to use async_std to receive UDP datagrams from the network. There is a UdpSocket that implements async recv_from, this method returns a future but I need a async_std::stream::Stream that gives a stream of UDP datagrams because it is a better abstraction. I've found tokio::net::UdpFramed that does exactly what I need but it is not available in current versions of tokio. Generally speaking the question is how do I convert Future s from a given async function into a Stream ? 回答1: For

How to define lifetimes on a Fn trait bound returning references?

巧了我就是萌 提交于 2020-02-05 04:34:29
问题 I'm trying to create a struct with a field, generic over F where F implements something like: Fn(&mut Compiler, &[Token]) -> &Token . The only issue is, I'm not sure how I define lifetimes on the Fn trait which satisfy the constraint that the returned &Token references data in the &[Token] slice supplied as an argument. Everything I've tried has thrown cryptic errors thus far. Here is an MVCE which demonstrates the code (without any lifetimes): struct Compiler; #[derive(Debug)] struct Token

Rust学习笔记002-编程概念

喜欢而已 提交于 2020-02-05 02:31:19
标识符 Rust 中的名称被称为 “标识符”(“identifier”),它们可以是任意非空的 ASCII 字符串,不过有如下限制: 要么是: 第一个字符是字母。 其它字符是字母数字或者 _ 。 或者是: 第一个字符是 _ 。 标识符需多于一个字符。单独的 _ 不是标识符。 其它字符是字母数字或者 _ 。 原始标识符 有时出于某种原因你可能需要将关键字作为名称。你可以使用 “原始标识符”(“raw identifier”)。原始标识符以 r# 开头: let r#fn = "你好"; // 调用名为 'match' 的函数 r#match(); 当然,一般使用不到原始标识符,必要时候可以这么用。 来源: CSDN 作者: Maratrix 链接: https://blog.csdn.net/u013958997/article/details/104173151