rust

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

醉酒当歌 提交于 2020-12-08 16:15:54
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

旧城冷巷雨未停 提交于 2020-12-08 16:14:30
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below

Why do I need to collect into a vector when using `flat_map`?

时光怂恿深爱的人放手 提交于 2020-12-08 08:44:46
问题 I'm working on Project Euler 96 to teach myself Rust. I've written this code to read in the file and convert it into a vector of integers (Playground). let file = File::open(&args[1]).expect("Sudoku file not found"); let reader = BufReader::new(file); let x = reader .lines() .map(|x| x.unwrap()) .filter(|x| !x.starts_with("Grid")) .flat_map(|s| s.chars().collect::<Vec<_>>()) // <-- collect here! .map(|x| x.to_digit(10).unwrap()) .collect::<Vec<_>>(); This all works fine but I'm puzzled why I

What is an idiomatic way to create a zero-sized struct that can't be instantiated outside its crate?

我是研究僧i 提交于 2020-12-08 07:23:40
问题 I have something similar to this: mod private { // My crate pub struct A; impl A { pub fn new() -> Self { Self } // ... } } fn main() { // External code let obj = private::A::new(); let obj2 = private::A; } Currently, A doesn't need to store any internal state to do what I want it to (it's just being used as a placeholder in an enum), so I made it a zero-sized struct. However, that might change in the future, so I want to prevent code outside this crate from instantiating A without going

What is an idiomatic way to create a zero-sized struct that can't be instantiated outside its crate?

柔情痞子 提交于 2020-12-08 07:22:50
问题 I have something similar to this: mod private { // My crate pub struct A; impl A { pub fn new() -> Self { Self } // ... } } fn main() { // External code let obj = private::A::new(); let obj2 = private::A; } Currently, A doesn't need to store any internal state to do what I want it to (it's just being used as a placeholder in an enum), so I made it a zero-sized struct. However, that might change in the future, so I want to prevent code outside this crate from instantiating A without going

expected enum `std::result::Result`, found () [closed]

只谈情不闲聊 提交于 2020-12-08 06:56:09
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 10 months ago . Improve this question I'm new to Rust. I try to create a Point struct that implements Eq and Debug , so I did this: use std::fmt; pub struct Point { x: f32, y: f32, } impl Point { pub fn new(x: f32, y: f32) -> Point { Point{ x: x, y: y, } } } impl fmt::Debug for Point { fn fmt(

Rust, how to return reference to something in a struct that lasts as long as the struct?

给你一囗甜甜゛ 提交于 2020-12-07 03:41:07
问题 I am porting a compiler I wrote to Rust. In it, I have an enum Entity which represents things like functions and variables: pub enum Entity<'a> { Variable(VariableEntity), Function(FunctionEntity<'a>) // Room for more later. } I then have a struct Scope which is responsible for holding on to these entities in a hash map, where the key is the name given by the programmer to the entity. (For example, declaring a function named sin would put an Entity into the hash map at the key sin .) pub

Rust, how to return reference to something in a struct that lasts as long as the struct?

为君一笑 提交于 2020-12-07 03:40:51
问题 I am porting a compiler I wrote to Rust. In it, I have an enum Entity which represents things like functions and variables: pub enum Entity<'a> { Variable(VariableEntity), Function(FunctionEntity<'a>) // Room for more later. } I then have a struct Scope which is responsible for holding on to these entities in a hash map, where the key is the name given by the programmer to the entity. (For example, declaring a function named sin would put an Entity into the hash map at the key sin .) pub

How to use dynamic dispatch with a method which takes an iterator as a parameter?

荒凉一梦 提交于 2020-12-06 12:21:00
问题 I am writing a command line application in rust for processing audio from a sensor. I would like the user to be able to choose an algorithm or filter to apply from several options. I was hoping to use dynamic dispatch to switch out a struct which implements my filter trait at runtime. However, this is not allowed by the compiler, because one of the trait methods takes a generic parameter. How could I implement this same functionality, without causing any compiler troubles? I know that an easy

How to use dynamic dispatch with a method which takes an iterator as a parameter?

倖福魔咒の 提交于 2020-12-06 12:20:49
问题 I am writing a command line application in rust for processing audio from a sensor. I would like the user to be able to choose an algorithm or filter to apply from several options. I was hoping to use dynamic dispatch to switch out a struct which implements my filter trait at runtime. However, this is not allowed by the compiler, because one of the trait methods takes a generic parameter. How could I implement this same functionality, without causing any compiler troubles? I know that an easy