FAQ Database Discussion Community


Cannot borrow as immutable - String and len()

rust,immutability,mutability,borrow-checker
let mut result = String::with_capacity(1000); result.push_str("things... "); result.push_str("stuff... "); result.truncate((result.len() - 4)); However, this is a compile error. Something to do with the borrow checker and possibly mutability. multipolygon.rs:63:21: 63:27 error: cannot borrow `result` as immutable because it is also borrowed as mutable multipolygon.rs:63 result.truncate((result.len() - 2)); ^~~~~~ multipolygon.rs:63:4: 63:10...

Parsing data into a module-level mutable static variable

static,rust,mutability
I have a set of functions within a module that need access to some shared initialization-time state. Effectively I'd like to model this with a static mutable vector like: static mut defs: Vec<String> = vec![]; fn initialize() { defs.push("One".to_string()); defs.push("Two".to_string()); } (Example: http://is.gd/TyNQVv, fails with "mutable statics are not allowed...

Are mutable constants safe?

python,constants,mutability
Are there any sort of 'gotchas' associated with having a constant that is a list, or any other mutable object? Currently my context has to do with constants that will be passed to a call, but I ask the question generically as I don't feel that topic impacts the question...

How to implement a trait for any mutability?

rust,trait,mutability
Can mutability be a generic parameter in traits? I'd like to implement a trait for a mutable and an immutable variant of a type without having to copy&paste the impl block. trait Foo<T> {…} impl<T> Foo for *const T {…} impl<T> Foo for *mut T {…same thing again…} Wishful pseudocode:...

swift: modify dictionary in place

swift,mutability
I have a function that takes a json object whose contents can be of any type (dictionary, array, string, etc) and modifies the object based on the type. In the contrived example function "foo" below, how can I modify the dictionary in place? I get the compiler error: error: '@lvalue...