rust

Why can't I update a value through an immutable reference of a mutable reference of a mutable value?

别来无恙 提交于 2020-01-15 10:24:29
问题 I found the following hard to understand: fn main() { let mut x: i32 = 10; { let y: &mut i32 = &mut x; *y += 10; println!("y={}", *y); let z: &&mut i32 = &y; // z += 10; // error[E0368]: binary assignment operation `+=` cannot be applied to type `&mut i32` // *z += 10; // binary assignment operation `+=` cannot be applied to type `&mut i32` // **z += 10; //cannot assign to data in a `&` reference } println!("x={}", x); } When I include *z += 10 , the error message is: error[E0368]: binary

Rust Borrow checker only complains about borrowing as mutable multiple times when a function that returns a reference with the same lifetime assigned

做~自己de王妃 提交于 2020-01-15 09:20:10
问题 I'm having problem with some Rust code where I'm being allowed to borrow something as mutable more than once on certain conditions (first confusing part), but not others. I've written the following example to illustrate: (Playground) struct NoLifetime {} struct WithLifetime <'a> { pub field: &'a i32 } fn main() { let mut some_val = NoLifetime {}; borrow_mut_function(&mut some_val); borrow_mut_function(&mut some_val); // Borrowing as mutable for the second time. let num = 5; let mut life_val =

How do I access files in the src directory from files in my tests directory?

懵懂的女人 提交于 2020-01-15 08:59:21
问题 I have a project layout that looks like the following: src/ int_rle.rs lib.rs tests/ test_int_rle.rs The project compiles with cargo build , but I am unable to run the test with cargo test . I get the error error[E0432]: unresolved import `int_rle`. There is no `int_rle` in the crate root --> tests/test_int_rle.rs:1:5 | 1 | use int_rle; | ^^^^^^^ error[E0433]: failed to resolve. Use of undeclared type or module `int_rle` --> tests/test_int_rle.rs:7:9 | 7 | int_rle::IntRle { values: vec![1, 2,

Method call on clap::App moving ownership more than once

戏子无情 提交于 2020-01-15 07:48:28
问题 Even after reading the chapters about reference ownership and borrowing, I cannot understand some things in the following code, effectively stopping me from calling more than one method from clap::App ! extern crate clap; use clap::App; fn main() { let mut app = App::new("name me").args_from_usage("<input_file> 'Sets the input file to use'"); let matches = app.get_matches(); app.print_help(); println!( "Using input file: {}", matches.value_of("input_file").unwrap() ); } Compiling this code

Method call on clap::App moving ownership more than once

柔情痞子 提交于 2020-01-15 07:48:25
问题 Even after reading the chapters about reference ownership and borrowing, I cannot understand some things in the following code, effectively stopping me from calling more than one method from clap::App ! extern crate clap; use clap::App; fn main() { let mut app = App::new("name me").args_from_usage("<input_file> 'Sets the input file to use'"); let matches = app.get_matches(); app.print_help(); println!( "Using input file: {}", matches.value_of("input_file").unwrap() ); } Compiling this code

What's the best way to register a function to run during an unexpected exit of a Rust program?

不问归期 提交于 2020-01-15 07:28:07
问题 I'm creating a terminal text editor in Rust. The editor puts the terminal into raw mode, disabling character echoing and the like, and then restores the original terminal function upon exit. However, the editor has some bugs, and crashes unexpectedly every now and again due to issues like unsigned variable underflow. When this happens, the cleanup code which would restore the terminal to its original state never runs. The cleanup function I'd like to run is the following: fn restore_orig_mode

What is the paradigmatic way to create a Rust tree with a parent pointer? [duplicate]

不羁岁月 提交于 2020-01-15 06:31:06
问题 This question already has answers here : How do I express mutually recursive data structures in safe Rust? (3 answers) How to model complex recursive data structures (graphs)? (1 answer) Closed 2 years ago . I need to define a binary search tree where each node has access to the parent: enum Tree<'a> { Leaf, Node { left: Box<Tree<'a>>, right: Box<Tree<'a>>, parent: &'a Tree<'a>, data: u64, } } impl <'a> Tree<'a> { pub fn new(data: u64, parent: &'a Tree) -> Tree<'a> { Tree::Node { left: Box:

How can I have a struct which is only used for its internal constant?

安稳与你 提交于 2020-01-15 05:47:11
问题 I need to encapsulate constants in the Rust type system. Ideally, RFC2000 would be ready, but in its absence, and since I only need a restricted set of constants, I can implement something close to what I need: trait U32Const { const VAL: u32; } struct U32Const10; impl U32Const for U32Const10 { const VAL: u32 = 10; } struct MyType<X: U32Const> { val: u32, template: X, } impl<X: U32Const> MyType<X> { fn do_something(&self) -> u32 { self.val * X::VAL } } fn main() { let a = MyType::<U32Const10>

Terminate a loop from a different thread with Rust

こ雲淡風輕ζ 提交于 2020-01-15 04:01:55
问题 I have a simple loop in a thread, and I'd like to stop the loop. Normally with Python, I would use a global variable to stop the loop, but I don't know for Rust. I heard about channels, but I don't know how to pass a channel Receiver into my function which starts a new thread. 回答1: Channels seem like overkill for a simple loop condition. You can use an atomic variable instead which can be shared across threads. 来源: https://stackoverflow.com/questions/41465573/terminate-a-loop-from-a-different

How to allocate structs on the heap without taking up space on the stack in stable Rust?

☆樱花仙子☆ 提交于 2020-01-15 03:17:05
问题 In this code... struct Test { a: i32, b: i64 } fn foo() -> Box<Test> { // Stack frame: let v = Test { a: 123, b: 456 }; // 12 bytes Box::new(v) // `usize` bytes (`*const T` in `Box`) } ... as far as I understand (ignoring possible optimizations), v gets allocated on the stack and then copied to the heap, before being returned in a Box . And this code... fn foo() -> Box<Test> { Box::new(Test { a: 123, b: 456 }) } ...shouldn't be any different, presumably, since there should be a temporary