rust

Writing to a file or stdout in Rust

强颜欢笑 提交于 2020-08-01 16:36:30
问题 I'm learning Rust, and I'm somewhat stumped. I'm trying to give the user the option of writing output to stdout or to a supplied filename. I started with the example code that's given for using extra::getopts located here. From there, in the do_work function, I'm trying to do this: use std::io::stdio::stdout; use std::io::buffered::BufferedWriter; fn do_work( input: &str, out: Option<~str> ) { println!( "Input: {}", input ); println!( "Output: {}", match out { Some(x) => x, None => ~"Using

Why doesn't ops::Range<T> implement Copy, even if T is Copy?

强颜欢笑 提交于 2020-08-01 09:53:51
问题 Recently, I wanted to write a type holding parameters for a 3D projection: use std::ops::Range; #[derive(Clone, Copy)] struct CamProj { /// Near and far plane proj_range: Range<f32>, /// Field of view fov: cgmath::Rad<f32>, // `Rad` derives `Copy` /// Width divided by height aspect_ratio: f32, } However, I got this error: error[E0204]: the trait `Copy` may not be implemented for this type --> <anon>:3:21 | 3 | #[derive(Clone, Copy)] | ^^^^ ... 6 | proj_range: Range<f32>, | -------------------

How does Rust guarantee memory safety and prevent segfaults?

我怕爱的太早我们不能终老 提交于 2020-08-01 06:11:42
问题 I was looking for a language to learn, and I saw that Rust is getting quite popular. Two things impressed me about Rust, memory safety and preventing segfaults. How does Rust achieve this? What differences between Rust and Java for example enable Rust's safety features? 回答1: How Rust achieves memory safety is, at its core, actually quite simple. It hinges mainly on two principles: ownership and borrowing. Ownership The compiler uses an affine type system to track the ownership of each value:

Out of source builds (external build directory) with Cargo?

本秂侑毒 提交于 2020-08-01 06:03:45
问题 Having used CMake, I've become used to out-of-source builds, which are encouraged with CMake. How can out-of-source builds be done with Cargo? Using in-source-builds again feels like a step backwards: Development tools need to be configured to ignore paths. Sometimes multiple plugins and development tools - especially using VIM or Emacs! Some tools can't be configured to easily hide build files. While dotfiles are typically hidden, they will still show Cargo.lock and target/ , worse still,

Out of source builds (external build directory) with Cargo?

陌路散爱 提交于 2020-08-01 06:01:00
问题 Having used CMake, I've become used to out-of-source builds, which are encouraged with CMake. How can out-of-source builds be done with Cargo? Using in-source-builds again feels like a step backwards: Development tools need to be configured to ignore paths. Sometimes multiple plugins and development tools - especially using VIM or Emacs! Some tools can't be configured to easily hide build files. While dotfiles are typically hidden, they will still show Cargo.lock and target/ , worse still,

How to use a custom serde deserializer for chrono timestamps?

六眼飞鱼酱① 提交于 2020-07-30 05:58:07
问题 I'm trying to parse JSON into a struct which has a chrono::DateTime field. The JSON has the timestamps saved in a custom format that I wrote a deserializer for. How do I connect the two and get it working using #[serde(deserialize_with)] ? I'm using NaiveDateTime for simpler code extern crate serde; extern crate serde_json; use serde::Deserialize; extern crate chrono; use chrono::NaiveDateTime; fn from_timestamp(time: &String) -> NaiveDateTime { NaiveDateTime::parse_from_str(time, "%Y-%m-%dT

Early-breaking from Rust's match

好久不见. 提交于 2020-07-29 12:20:14
问题 I want to switch through many possible cases for x and there's one case (here x == 0 ) where I want to check the result of some additional code to determine what to do next. One possibility is to return early from the match. I'd use break to do this early-returning in C, but this isn't allowed in Rust. return returns from the parent function (in this case main() ) and not from the match only (i.e. the println! at the end isn't run!). I could just negate the sub-condition (here y == 0 ) and

Early-breaking from Rust's match

一世执手 提交于 2020-07-29 12:19:06
问题 I want to switch through many possible cases for x and there's one case (here x == 0 ) where I want to check the result of some additional code to determine what to do next. One possibility is to return early from the match. I'd use break to do this early-returning in C, but this isn't allowed in Rust. return returns from the parent function (in this case main() ) and not from the match only (i.e. the println! at the end isn't run!). I could just negate the sub-condition (here y == 0 ) and

How can I modify a slice that is a function parameter?

泄露秘密 提交于 2020-07-29 12:16:28
问题 Parameters can be passed to functions and modified: fn set_42(int: &mut i32) { *int += 42; } fn main() { let mut int = 0; set_42(&mut int); println!("{:?}", int); } Output: 42 Changing the code to use a slice fails with a whole bunch of errors: fn pop_front(slice: &mut [i32]) { *slice = &{slice}[1..]; } fn main() { let mut slice = &[0, 1, 2, 3][..]; pop_front(&mut slice); println!("{:?}", slice); } Output: error[E0308]: mismatched types --> src/main.rs:2:14 | 2 | *slice = &{ slice }[1..]; | ^

科技爱好者周刊(第 111 期):智能电视的误区

ぐ巨炮叔叔 提交于 2020-07-29 05:05:19
这里记录每周值得分享的科技内容,周五发布。 本杂志开源(GitHub: ruanyf/weekly ),欢迎提交 issue,投稿或推荐你的项目。 周刊讨论区的帖子 《谁在招人?》 ,提供大量就业信息,欢迎访问或发布工作/实习岗位。 封面图 板齿犀(Elasmotherium)是一类已经灭绝的大型犀牛,高2米,长5米,前额上有长达2米的角,约1600万年前灭绝。( 出处 ) 本周话题:智能电视的误区 国内的智能电视机,现在都自带系统,一打开就是各种菜单,有直播、影视、游戏、应用等等。 我不赞成这样的设计,也不愿意购买这样的电视机。 这是因为电视机的使用年限很长,可能达到10年。内置的操作系统不可能跟着更新这么久,事实上很多电视机自从出厂,就再也不更新系统了。以后,你一打开电视,就会看到过时的系统。 而且,为了压低成本,智能电视使用的 CPU、内存和储存,都是很差的配置,不可能有好的性能和体验。只要一两年,系统就会变得很卡,动不动就提醒你没有剩余空间了。 正确的做法应该是,电视机和系统是分开的两个硬件,不集成在一起。电视机最好就是一个单纯的显示设备,操作系统由机顶盒来承担。 但是,这在国内恐怕很难做到。因为国内电视机的操作系统,很大的目的是播放开机广告,他们舍不得这一块的利益。 同理,自带操作系统的智能家电,我都建议不要购买,比如可以上网的互联网冰箱。 数据分析全套讲解