rust

Scope of addresses: Does not live long enough

淺唱寂寞╮ 提交于 2020-01-04 21:34:51
问题 I was surprised by the result of these two apparently similar programs. fn main() { let y: &int = &31i; println!("My number is {}.",*y) } //Output My number is 31. However, this code gives me an error. fn main() { let y: ∫ y = &31i; println!("My number is {}.",*y) } // Output on Rust Playpen 3:12 error: borrowed value does not live long enough 5:2 note: reference must be valid for the block at 1:10... 3:13 note: ...but borrowed value is only valid for the statement at 3:4 Apparently, &31i

Scope of addresses: Does not live long enough

我是研究僧i 提交于 2020-01-04 21:34:11
问题 I was surprised by the result of these two apparently similar programs. fn main() { let y: &int = &31i; println!("My number is {}.",*y) } //Output My number is 31. However, this code gives me an error. fn main() { let y: ∫ y = &31i; println!("My number is {}.",*y) } // Output on Rust Playpen 3:12 error: borrowed value does not live long enough 5:2 note: reference must be valid for the block at 1:10... 3:13 note: ...but borrowed value is only valid for the statement at 3:4 Apparently, &31i

Scope of addresses: Does not live long enough

荒凉一梦 提交于 2020-01-04 21:34:06
问题 I was surprised by the result of these two apparently similar programs. fn main() { let y: &int = &31i; println!("My number is {}.",*y) } //Output My number is 31. However, this code gives me an error. fn main() { let y: ∫ y = &31i; println!("My number is {}.",*y) } // Output on Rust Playpen 3:12 error: borrowed value does not live long enough 5:2 note: reference must be valid for the block at 1:10... 3:13 note: ...but borrowed value is only valid for the statement at 3:4 Apparently, &31i

Expected behavior of `for` loop in rust [duplicate]

戏子无情 提交于 2020-01-04 16:56:18
问题 This question already has answers here : How do I include the end value in a range? (2 answers) Closed last year . I'm new to rust and I've question about the way the for loop. fn main() { for x in 1 .. 10 { println!("x == {}", x); } } The output of the program is x == 1 x == 2 x == 3 x == 4 x == 5 x == 6 x == 7 x == 8 x == 9 I was expecting for loop to execute and display x == 10 but it stopped at 9. Is this expected behavior 回答1: This is expected! The 0 .. 10 is an "exclusive range" meaning

Why do subsequent Rust variables increment the stack pointer instead of decrementing it?

这一生的挚爱 提交于 2020-01-04 15:53:49
问题 I find it odd how when you create statically-allocated variables in Rust that it seems as the stack pointer increases. I know this is not the case since the stack pointer decreases as memory is allocated. If I were to do the same thing in C, I would see the stack pointer decrease as I created more variables. Why is it this way? Does the Rust compiler allocate these from bottom to top instead on top to bottom? fn main() { let i1 = 1; let i2 = 1; let i3 = 1; println!("i1 : {:?}", &i1 as *const

Passing a closure that modifies its environment to a function in Rust

≯℡__Kan透↙ 提交于 2020-01-04 07:36:41
问题 I have a closure that captures and modifies its environment. I want to pass this closure to a function that accepts closures: fn main() { let mut integer = 5; let mut closure_variable = || -> i32 { integer += 1; integer }; execute_closure(&mut closure_variable); } fn execute_closure(closure_argument: &mut Fn() -> i32) { let result = closure_argument(); println!("Result of closure: {}", result); } Because the closure modifies its environment, this fails: error[E0525]: expected a closure that

Parsing camel case strings with nom

孤者浪人 提交于 2020-01-04 06:43:11
问题 I want to parse a string like "ParseThis" or "parseThis" into a vector of strings like ["Parse", "This"] or ["parse", "this"] using the nom crate. All attempts I've tried do not return the expected result. It's possible that I don't understand yet how to use all the functions in nom. I tried: named!(camel_case<(&str)>, map_res!( take_till!(is_not_uppercase), std::str::from_utf8)); named!(p_camel_case<&[u8], Vec<&str>>, many0!(camel_case)); But p_camel_case just returns a Error(Many0) for

Inferring types and using type annotations when parsing a string to a number

三世轮回 提交于 2020-01-04 06:23:08
问题 I'm trying to write a function that will parse float from given string. It should return error in case of wrong or negative value. fn read_value(strvalue: &str) -> Result<f32, Error> { match FromStr::from_str(strvalue) { None => Err(Error::InvalidValue), Some(value) => if value >= 0.0 {Ok(value)} else {Err(Error::InvalidValue)} } } This code gives: src/main.rs:50:27: 50:32 error: the type of this value must be known in this context src/main.rs:50 Some(value) => if value >= 0.0 {Ok(value)}

Will the non-lexical lifetime borrow checker release locks prematurely?

痞子三分冷 提交于 2020-01-04 06:21:08
问题 I've read What are non-lexical lifetimes?. With the non-lexical borrow checker, the following code compiles: fn main() { let mut scores = vec![1, 2, 3]; let score = &scores[0]; // borrows `scores`, but never used // its lifetime can end here scores.push(4); // borrows `scores` mutably, and succeeds } It seems reasonable in the case above, but when it comes to a mutex lock, we don't want it to be released prematurely. In the following code, I would like to lock a shared structure first and

Is it possible to create a macro to implement builder pattern methods?

前提是你 提交于 2020-01-04 06:09:55
问题 I have a builder pattern implemented for my struct: pub struct Struct { pub grand_finals_modifier: bool, } impl Struct { pub fn new() -> Struct { Struct { grand_finals_modifier: false, } } pub fn grand_finals_modifier<'a>(&'a mut self, name: bool) -> &'a mut Struct { self.grand_finals_modifier = grand_finals_modifier; self } } Is it possible in Rust to make a macro for methods like this to generalize and avoid a lot of duplicating code? Something that we can use as the following: impl Struct