lifetime

“borrowed value does not live long enough” when using the builder pattern

妖精的绣舞 提交于 2019-11-27 02:21:59
I have the following code: pub struct Canvas<'a> { width: isize, height: isize, color: Color, surface: Surface, texture: Texture, renderer: &'a Renderer, } impl<'a> Canvas<'a> { pub fn new(width: isize, height: isize, renderer: &'a Renderer) -> Canvas<'a> { let color = Color::RGB(0, 30, 0); let mut surface = core::create_surface(width, height); let texture = Canvas::gen_texture(&mut surface, width, height, color, renderer); Canvas { width: width, height: height, color: color, surface: surface, texture: texture, renderer: renderer, } } pub fn color(&mut self, color: Color) -> &mut Canvas<'a> {

Why does my variable not live long enough?

孤街浪徒 提交于 2019-11-27 02:17:55
I have a simple piece of code that is supposed to read a file into a vector by lines use std::io::{self, Read}; use std::fs::File; fn file_to_vec(filename: &str) -> Result<Vec<&str>, io::Error> { let mut file = try!(File::open(filename)); let mut string = String::new(); try!(file.read_to_string(&mut string)); string.replace("\r", ""); let data: Vec<&str> = string.split('\n').collect(); Ok(data) } fn main() {} I am getting the following error: error[E0597]: `string` does not live long enough --> src/main.rs:10:27 | 10 | let data: Vec<&str> = string.split('\n').collect(); | ^^^^^^ does not live

When returning the outcome of consuming a StdinLock, why was the borrow to stdin retained?

爷,独闯天下 提交于 2019-11-27 02:14:14
Given the following function: use std::io::{BufRead, stdin}; fn foo() -> usize { let stdin = stdin(); let stdinlock = stdin.lock(); stdinlock .lines() .count() } This fails to compile with the following error: error: `stdin` does not live long enough --> src/main.rs:12:1 | 7 | let stdinlock = stdin.lock(); | ----- borrow occurs here ... 11 | } | ^ `stdin` dropped here while still borrowed | = note: values in a scope are dropped in the opposite order they are created I find this surprising because the outcome of consuming the lock (via lines ) does not retain any references to the original

Moved variable still borrowing after calling `drop`?

前提是你 提交于 2019-11-26 23:16:21
fn main() { let mut x: Vec<&i32> = vec![]; let a = 1; x.push(&a); drop(x); // x.len(); // error[E0382]: use of moved value: `x` } // `a` dropped here while still borrowed The compiler knows drop() drops x (as evident from the error in the commented-out code) but still thinks the variable is borrowing from a ! This is unfair! Should this be considered as one of numerous dupes of rust-lang/rust#6393 (which is now tracked by rust-lang/rfcs#811 ?) But the discussion there seems to be centered on making &mut self and &self coexist in a single block. Lukas Kalbertodt I can't give you a definite

Why does the Rust compiler request I constrain a generic type parameter's lifetime (error E0309)?

被刻印的时光 ゝ 提交于 2019-11-26 23:12:13
问题 Why does the Rust compiler emit an error requesting me to constrain the lifetime of the generic parameter in the following structure? pub struct NewType<'a, T> { x: &'a T, } error[E0309]: the parameter type `T` may not live long enough --> src/main.rs:2:5 | 2 | x: &'a T, | ^^^^^^^^ | = help: consider adding an explicit lifetime bound `T: 'a`... note: ...so that the reference type `&'a T` does not outlive the data it points at --> src/main.rs:2:5 | 2 | x: &'a T, | ^^^^^^^^ I can fix it by

When is it useful to define multiple lifetimes in a struct?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:48:08
In Rust, when we want a struct to contain references, we typically define their lifetimes as such: struct Foo<'a> { x: &'a i32, y: &'a i32, } But it's also possible to define multiple lifetimes for different references in the same struct: struct Foo<'a, 'b> { x: &'a i32, y: &'b i32, } When is it ever useful to do this? Can someone provide some example code that doesn't compile when both lifetimes are 'a but does compile when the lifetimes are 'a and 'b (or vice versa)? After staying up way too late, I was able to come up with an example case where the lifetimes matter. Here is the code: static

How do I call a function that requires a 'static lifetime with a variable created in main?

☆樱花仙子☆ 提交于 2019-11-26 22:06:58
问题 I've got a struct defined that has a function which defines a static lifetime: impl MyStruct { pub fn doSomething(&'static self) { // Some code goes here } } I'm consuming it from main like so: fn main() { let obj = MyStruct {}; obj.doSomething(); } It's intended for the doSomething call to block and execute for the lifetime of the application. I'm running into issues with the lifetime checks where it's stating that it may outlive the main function, which seems strange to me as once main is

Lifetime of a string literal returned by a function

本秂侑毒 提交于 2019-11-26 21:00:06
Consider this code: const char* someFun() { // ... some stuff return "Some text!!" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } In the function someFun() , where is "Some text!!" stored (I think it may be in some static area of ROM) and what is its scope lifetime? Will the memory pointed by retStr be occupied throughout the program or be released once the block A exits? The C++ Standard does not say where string literals should be stored. It does however guarantee that their lifetime is the lifetime of the program. Your code is therefore valid. The "Some text!

Connection Timeout and Connection Lifetime

你。 提交于 2019-11-26 20:36:53
问题 What is the advantage and disadvantage of connection timeout=0? And what is the use of Connection Lifetime=0? e.g (Database=TestDB; port=3306; Uid=usernameID; Pwd=myPassword; Server=192.168.10.1; Pooling=false; Connection Lifetime=0; Connection Timeout=0) and what is the use of Connection Pooling? 回答1: Timeout is how long you wait for a response from a request before you give up. TimeOut=0 means you will keep waiting for the connection to occur forever. Good I guess if you are connecting to a

Why am I being allowed to use a const qualified variable as an array size in C?

北战南征 提交于 2019-11-26 19:11:57
When I run the following code,it works fine for C: #include<stdio.h> int main(void) { const int x=5; char arr[x]; printf("%d",sizeof(arr)); } But not only had I read before that const qualified variables are not real constants (that's why they can't be used in case condition of switch-case ),but the following link from IBM corroborates that ( IBMLINK ) and says: const int k = 10; int ary[k]; /* allowed in C++, not legal in C */ Why then am I allowed to use a const qualified variable in C as an array size without any error? c99 support variable length arrays but c90 does not support variable