rust

初学Rust笔记

て烟熏妆下的殇ゞ 提交于 2020-02-17 11:52:25
今天是学习rust的第三天,首先总结昨天的学习内容,温故才能知新~ 学习材料:Rust官网教程《The Rust Programming Language》 学习内容概览 Getting Started:hello world基础代码、Hello Cargo 初试cargo命令。 Programming a Guessing Game:通过一个猜数字的小例子初识Rust中的基础语言。 Common Programming Concepts:包括变量及其可变性、数据类型、函数、注释和控制流。 Understanding Ownership:首先介绍了ownership的定义,随后讲解了reference和slice。 (本部分为主要笔记内容) Using Structs to Structure Related Data:本章内容为结构体。 Hello Cargo没有太大的难度,基本跟随书上的介绍一步一步做就可以。学习了 cargo build 、 cargo run 、 cargo test 等命令,另外还介绍了依赖库的添加:在 cargo.toml 中的 [dependencies] 下添加依赖(此过程中如果遇到下载慢的情况可以参照上一篇笔记的解决方法);以及在依赖包的更新问题: cargo.lock 中会记录各个依赖的版本,防止出现由于版本更新而导致的bug。 第2

How do I read multiple integers from a single line of stdin?

喜夏-厌秋 提交于 2020-02-16 06:19:23
问题 To learn Rust, I'm looking at things like the HackerRank 30 days challenge, Project Euler, and other programming contests. My first obstacle is to read multiple integers from a single line of stdin. In C++ I can conveniently say: cin >> n >> m; How do I do this idiomatically in Rust? 回答1: The best way, as far as I know, is just to split the input line and then map those to integers, like this: use std::io; let mut line = String::new(); io::stdin().read_line(&mut line).expect("Failed to read

Initializing a Rust variable passed to async code such as tokio and hyper

早过忘川 提交于 2020-02-16 05:29:22
问题 I have a value that cannot be computed at compile time. It needs to be computed before any of the app code runs, and then it will only be read throughout the lifetime of the app. It also needs to be passed around to executors such as tokio and hyper handlers. How can I create such a value safely, idiomatically and without unneeded performance losses? If I create it in main and pass it to hyper , it does not live long enough. If I create it with lazy_static! , it only gets computed when it's

Initializing a Rust variable passed to async code such as tokio and hyper

故事扮演 提交于 2020-02-16 05:29:06
问题 I have a value that cannot be computed at compile time. It needs to be computed before any of the app code runs, and then it will only be read throughout the lifetime of the app. It also needs to be passed around to executors such as tokio and hyper handlers. How can I create such a value safely, idiomatically and without unneeded performance losses? If I create it in main and pass it to hyper , it does not live long enough. If I create it with lazy_static! , it only gets computed when it's

Can I force the use of my dependencies' Cargo.lock when resolving package versions?

让人想犯罪 __ 提交于 2020-02-16 00:26:59
问题 The Cargo FAQ states that Cargo.lock is not used for libraries, instead using dependency version ranges found in Cargo.toml , to reduce lib duplication among shared dependencies. However, I think there are instances where using a known successful build of a lib dependency is preferable. Namely, when a dependency no longer builds due to updates of its own dependencies. Is it possible to configure Cargo to favour a library's Cargo.lock , over Cargo.toml , if it's available? Preferably on a by

Can I force the use of my dependencies' Cargo.lock when resolving package versions?

柔情痞子 提交于 2020-02-16 00:26:13
问题 The Cargo FAQ states that Cargo.lock is not used for libraries, instead using dependency version ranges found in Cargo.toml , to reduce lib duplication among shared dependencies. However, I think there are instances where using a known successful build of a lib dependency is preferable. Namely, when a dependency no longer builds due to updates of its own dependencies. Is it possible to configure Cargo to favour a library's Cargo.lock , over Cargo.toml , if it's available? Preferably on a by

“cannot move out borrowed content” when assigning a variable from a struct field

强颜欢笑 提交于 2020-02-15 08:28:12
问题 I'm learning Rust and I'm fighting against the borrow checker. I have a basic Point structure. I have a scale function that modifies all the coordinates of the point. I would like to call this method from another method named convert : struct AngleUnit; struct Point { x: f32, y: f32, z: f32, unit: AngleUnit, } fn factor(_from: AngleUnit, _to: AngleUnit) -> f32 { 1.0 } impl Point { pub fn new(x: f32, y: f32, z: f32, unit: AngleUnit) -> Point { Point { x, y, z, unit } } fn scale(&mut self,

Stack behavior when returning a pointer to local variable

若如初见. 提交于 2020-02-14 12:59:04
问题 I have a simple example where the behaviour of Rust does not match my mental image, so I am wondering what am I missing: fn make_local_int_ptr() -> *const i32 { let a = 3; &a } fn main() { let my_ptr = make_local_int_ptr(); println!("{}", unsafe { *my_ptr } ); } Result: 3 This is not what I would expect. Using the notation given in The Stack and the Heap, I would expect the stack frame to look like this: Address | Name | Value ----------------------- 0 | a | 3 inside make_local_int_ptr() ,

RUST B站代码(不全面)

独自空忆成欢 提交于 2020-02-13 02:13:05
Rust编程视频教程(基础) 视频中所有例子源码地址 重点 https://github.com/anonymousGiga/learn_rust 二 RUST环境安装 curl -sSf https://sh.rustup.rs | sh source ~/.cargo/env rustc --version 新建工程 [root@kolla ~]# mkdir -p ~/source/rust/learn_rust [root@kolla ~]# cd source/rust/learn_rust/ [root@kolla learn_rust]# cargo new hello #创建 [root@kolla hello]# pwd /root/source/rust/learn_rust/hello [root@kolla hello]# cargo run #运行 [root@kolla hello]# cargo build #编译 [root@kolla hello]# cargo check #检查语法 三变量 [root@kolla learn_rust]# cargo new learn_var [root@kolla learn_rust]# cd learn_var/ [root@kolla learn_var]# cat src/main.rs const

Why is the mutability of a variable not reflected in its type signature in Rust?

荒凉一梦 提交于 2020-02-11 18:52:12
问题 As I understand, mutability is not reflected in variables type signature. For example, these two references have the same type signature &i32 : let ref_foo : &i32 = &foo; let mut ref_bar : &i32 = &bar; Why is this the case? It seems like a pretty major oversight. I mean, even C/C++ does this more explictly with having two const to indicate that we have a const pointer to const data: const int * const ptr_foo = &foo; const int * ptr_bar = &bar; Is there a better way of thinking about this? 回答1