“Life-time” of a string literal in C

Yes, the lifetime of a local variable is within the scope({,}) in which it is created. Local variables have automatic or local storage. Automatic because they are automatically destroyed once the scope within which they are created ends. However, What you have here is a string literal, which is allocated in an implementation-defined read-only memory. … Read more

How can I create my own data structure with an iterator that returns mutable references?

You can’t compile this because mutable references are more restrictive than immutable references. A shortened version that illustrates the issue is this: struct MutIntRef<‘a> { r: &’a mut i32 } impl<‘a> MutIntRef<‘a> { fn mut_get(&mut self) -> &’a mut i32 { &mut *self.r } } fn main() { let mut i = 42; let mut … Read more

Singleton Per Call Context (Web Request) in Unity

Neat solution, but each instance of LifetimeManager should use a unique key rather than a constant: private string _key = string.Format(“PerCallContextLifeTimeManager_{0}”, Guid.NewGuid()); Otherwise if you have more than one object registered with PerCallContextLifeTimeManager, they’re sharing the same key to access CallContext, and you won’t get your expected object back. Also worth implementing RemoveValue to ensure … Read more

Revive object from destructor in C++?

The short answer is: no. C++ does not employ garbage collection, like Java or C#. When an object is destroyed, it’s destroyed immediately. Gone for good. Joined the choir invisible. Pining for the fjords, etc… And to say this over a couple of times in different words so that there is no possible weasily reinterpretation… … Read more

The compiler suggests I add a ‘static lifetime because the parameter type may not live long enough, but I don’t think that’s what I want

Check out the entire error: error[E0310]: the parameter type `U` may not live long enough –> src/main.rs:9:24 | 8 | fn add<U: Bar<T>>(&mut self, x: U) { | — help: consider adding an explicit lifetime bound `U: ‘static`… 9 | self.data.push(Box::new(x)); | ^^^^^^^^^^^ | note: …so that the type `U` will meet its required lifetime … Read more

Lifetimes in Rust

Update 2015-05-16: the code in the original question applied to an old version of Rust, but the concepts remain the same. This answer has been updated to use modern Rust syntax/libraries. (Essentially changing ~[] to Vec and ~str to String and adjusting the code example at the end.) Is my understanding vaguely accurate? […] What … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)