rust , Passing a component of a pointer by value

Passing a component of a pointer by value


Tag: rust

I've just started out with Rust, so some of my concepts may be wrong. I'd really appreciate corrections if this is the case.

I was following the lifetimes guide and trying some of the examples. I modified the rectangle example a little.

I changed the compute_distance function to accept Point by value in the first parameter. I then removed the & before on_the_stack.origin in the call to compute_distance.

This gives me the following error:

cannot move out of dereference of &-pointer

If instead I add the & before the on_the_stack.origin call as well as accept Point by reference in the compute_distance function, it works without a hitch.

The second method working makes sense to me, but why does my original approach throw an error?

use std::num::Float;

struct Point {
    x : f64,
    y : f64

struct Size {w: f64, h: f64}
struct Rectangle {origin: Point, size: Size}

fn main() {
    let on_the_stack = &Rectangle{origin: Point {x: 1.0, y: 2.0},
                                  size: Size {w: 3.0, h: 4.0}};

    let on_the_heap = box Rectangle {origin: Point {x: 5.0, y: 6.0},
                                  size: Size {w: 3.0, h: 4.0}};

    println!("Distance: {}", compute_distance(on_the_stack.origin,&on_the_heap.origin));

fn compute_distance ( p1:Point,p2:&Point) -> f64 {
    let x_d = p1.x - p2.x;
    let y_d = p1.y - p2.y;
    Float::sqrt(x_d * x_d + y_d * y_d)


As you said, your changed compute_distance wants a value as the first parameter. This means that the function is going to take ownership of the argument.

However, you are attempting to pass a smaller piece of a structure (origin is embedded in Rectangle). To do this, Rust would have to give ownership of part of Rectangle to the method, but exactly what state would Rectangle be left in? A very broken one, so Rust prevents you from doing that. Yay, you didn't shoot yourself in the foot!

"But wait", you say, "it's totally cool if the function gets a copy of origin". You, as a human, can recognize that and tell the compiler that it's "totally cool":

struct Point { /* ... */ }

Now Rust knows that it is safe to make a bit-for-bit copy of a Point if it would like to. Huzzah!

"But wait", you say, "what if it's not cool to make a bit-for-bit copy? My structure needs more love than being treated like a bag of bits!". Again, as an intelligent human, you can express that:

struct Point { /* ... */ }

// later...


Now you can make an explicit call (clone()) to get a semantic duplicate of the option. Because this could be an expensive operation, the compiler won't do it for you.

A note on deriving

As I write this answer, deriving is transitioning to derive. Adjust as needed for your code.


Stack behavior when returning a pointer to local variable

I have a simple example where the behaviour of Rust does not match my mental image, so I am wondering what am I missing: fn make_local_int_ptr() -> *const i32 { let a = 3; &a } fn main() { let my_ptr = make_local_int_ptr(); println!("{}", unsafe { *my_ptr } ); }...

Recovering from `panic!` in another thread

I know that in Rust there is no try/catch, and you can't throw a rolling save from the thread that is currently panicking. I know you should not create and handle errors like this. This is just for example's sake. However, I am wondering what the best way to recover...

More convenient way to work with strings in winapi calls

I'm looking for more convenient way to work with std::String in winapi calls in Rust. Using rust v 0.12.0-nigtly with winapi 0.1.22 and user32-sys 0.1.1 Now I'm using something like this: use winapi; use user32; pub fn get_window_title(handle: i32) -> String { let mut v: Vec<u16> = Vec::new(); v.reserve(255); let...

Struct that owns some data and a reference to the data [duplicate]

This question already has an answer here: How to initialize struct fields which reference each other 1 answer Construction of an object allocates data needed for lifetime of that object, but also creates another object that needs to keep references to the data: pub fn new() -> Obj {...

“conflicting implementations for trait” when trying to be generic

Background: I'm using the nalgebra library and I want to create a structure that represents a multivariate normal distribution. M is the type of the matrix, e.g. Mat4<f64>. My current attempt looks like this: use std::ops::Mul; use std::marker::PhantomData; use nalgebra::*; #[allow(non_snake_case)] pub struct Multivar𝒩<N, V, M: SquareMat<N, V>> { μ:...

Return a moving window of elements resulting from an iterator of Vec

I'm trying to figure out how to return a window of elements from a vector that I've first filtered without copying it to a new vector. So this is the naive approach which works fine but I think will end up allocating a new vector from line 5 which I...

Casting to a generic type

I have a newbie question about generics in Rust (version 1.0). Let's say I write a generic function to do division. Never mind the usefulness of such a function; it's a simple function to keep this question simple. fn divide<T: std::ops::Div>(a: T, b: T) -> T { a / b...

Reading immutable value inside spawned thread

I'm trying to make this example work use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc; use std::thread; use std::sync::Arc; struct User { reference: String, email: String } struct UserIndex { reference: usize, email: usize } fn main() { let rows = vec![ vec!["abcd", "[email protected]"], vec!["efgh", "[email protected]"], vec!["wfee", "[email protected]"], vec!["rrgr", "[email protected]"] ]; let user_index...

Immutable reference after mutable borrow

I've been reading about rust for the past week and (trying) to play around with it. It seems I run into similar problems related to ownership/borrowing every time I use it, so here is the simplest piece of code that sort of illustrates the usual problems. use std::cell::RefCell; struct Res...

Rust: Lifetime of String from file [duplicate]

This question already has an answer here: Return local String as slice 1 answer I'm trying to read in some external GLSL code into Rust. The reading works properly, but I run into a lifetime issue in the final expression (in the Ok(_) branch) error: s does not live...

Create a vector from iterating hashmap

What is the optional way in Rust to iterate a HashMap and collect the result into a Vec? This is my attempt so far: use std::collections::HashMap; struct User { reference: String, email: String } fn main() { let mut users: HashMap<String, User> = HashMap::new(); users.insert("first".to_string(), User { reference: "ref1".to_string(), email:...

Indexing a String

I want to perform a very simple task, but I cannot manage to stop the compiler from complaining. fn transform(s: String) -> String { let bytes = s.as_bytes(); format!("{}/{}", bytes[0..2], bytes[2..4]) } [u8] does not have a constant size known at compile-time. Some tips making this operation to work as...

How to reverse after zip two chains

I have the following code which does not compile. fn main() { let a = "123" .chars() .chain("4566".chars()) .zip( "bbb" .chars() .chain("yyy".chars())) .rev() .map(|x, y| y) .collect::<String>(); println!("Hello, world! {}", a); } Got error like following: src/ 37:15 error: the trait `core::iter::ExactSizeIterator` is not implemented for the type `core::iter::Chain<core::str::Chars<'_>, core::str::Chars<'_>>`...

What is the idiomatic way to write a linked list with a tail pointer?

As a learning project for Rust, I have a very simple (working, if incomplete) implementation of a singly linked list. The declaration of the structs looks like this: type NodePtr<T> = Option<Box<Node<T>>>; struct Node<T> { data: T, next: NodePtr<T>, } pub struct LinkedList<T> { head: NodePtr<T>, } Implementing size and...

How can I send a function to another thread?

I am attempting to write a simpler unit test runner for my Rust project. I have created a TestFixture trait that my test fixture structs will implement, similar to inheriting from the unit test base class in other testing frameworks. The trait is fairly simple. This is my test fixture...

Can Rust handle cyclic data structures without any garbage collector?

Is it possible to completely avoid a garbage collector and manual deallocation? Is it possible to implement an interpreter for a language that needs garbage collection (say, Scheme) in Rust, without implementing or using any garbage collector? ...

Struct vs enum lifetime differences

Why does this work #[derive(Debug)] pub struct Foo<'a,'b> { s : &'a str, n : &'b i32 } #[test] fn test_struct() { let f = Foo { s : &"bar" , n : &17 }; println!("{:?}",f); } but this doesn't #[derive(Debug)] pub enum Bar<'a,'b> { Baz ( &'a str), Fub...

Global feature gates in Cargo

I would like to enable a feature gate for my entire Cargo project. For example, I would like #![feature(non_ascii_idents)] added to every source file. Is there a place to list them in Cargo.toml?

Why is `Sized` bound necessary in this trait?

I have a simple trait with two associated functions like this: trait WithConstructor : Sized { fn new_with_param(param: usize) -> Self; fn new() -> Self { Self::new_with_param(0) } } The default implementation of the second method (new()) force me to put the Sized bound on the type. Why ? (I...

How to implement variable increment via side effect?

For learning purposes, I tried this solution, but it does not work: use std::ops::Add; fn inc<T: Add>(x:&mut T) { *x += 1; } fn main() { let mut x:i32 = 10; let mut y:u8 = 1; inc(&mut x); inc(&mut y); println!("{} {}", x, y); } Error message: <anon>:4:5: 4:7 error:...

the type of this value must be known in this context

I want to get a length of a string which I've split: fn fn1(my_string: String) -> bool { let mut segments = my_string.split("."); if segments.collect().len() == 55 { //...... } // error: the type of this value must be known in this context How can I fix that error?...

Filter vector in place

In Rust is it possible to operate on a vector in-place? I'd like to remove some elements from a Vec, but vec.iter().filter().collect() creates a new vector with borrowed items. I'd like to mutate the original Vec without extra memory allocation (and keep memory of removed elements as an extra capacity...

Pass Python list to Rust function

I have a Rust library that needs to be imported into Python via the ctypes module. My goal is to use Rust functions that take Vec<T> / i32 as arguments and return those types, from Python. Currently, I can pass integers to the Rust functions, and have them return lists...

Bug in FFI when passing CString followed by an int

My Rust test code extern "C" { fn test_int_only(n : libc::c_int); fn test_int_and_str(s : CString , n : libc::c_int); } pub fn test1() { unsafe { test_int_only(0); test_int_only(1); test_int_only(2); test_int_only(4); test_int_only(-12); } } pub fn test2() { unsafe { test_int_and_str(CString::new("Foo").unwrap(),0); test_int_and_str(CString::new("Bar").unwrap(),1); test_int_and_str(CString::new("Baz").unwrap(),2); test_int_and_str(CString::new("Fub").unwrap(),4); test_int_and_str(CString::new("Bub").unwrap(),-12); } } My C code void...

How to use multiple variables in routes with Nickel?

Nickel states that you can use variables in the URLs, which sounds very useful, but is it possible to use multiple variables? Something like: server.get("/start/:userid?:passwd", middleware! { |request| // format!("This is user: {:?} = {:?}", // request.param("userid"), // request.param("passwd") // ); }); ...

Why do I have to expose a macro implementation's 'use' in the client library?

I'm trying to use a macro I've created in a separate module. With reference to this SO question, I've imported a macro fine. However it seems that I have Update to add macro implementation #![macro_use] use std::fmt; use std::ffi::CString; use std::ffi::CStr; use std::str; extern crate libc; pub enum DbaxError...

Modify Collection While Iterating Over it

Gist to source Disclaimer: I'm just starting to learn Rust and I know this is not the best way to do this. I'm simply playing around to see what I can and cannot do. I'm also trying to limit any copying to restrict myself a little bit. I have a...

Create shared C object linked to Rust dylib for use in R

I am trying to create a shared object I can load into R that calls Rust functions by way of R's C API. To call Rust from C, I am following this blog post. My problem arises when I try to create the shared library and link to the Rust...

Renaming a crate with a hyphen doesn't compile anymore

This no longer compiles: extern crate "my-crate" as my_crate1; What's the new syntax? I did not find anything by searching....

remove duplicates from vector of custom struct

I'm trying to remove duplicates in the below example: struct User { reference: String, email: String } fn main() { let mut users: Vec<User> = Vec::new(); users.push(User { reference: "abc".into(), email: "[email protected]".into() }); users.push(User { reference: "def".into(), email: "[email protected]".into() }); users.push(User { reference: "ghi".into(), email: "[email protected]".into() }); users.sort_by(|a, b|;...

How can I create Haskell-like functional dependencies

Background: I'm using the nalgebra library and I want to create a structure that represents a multivariate normal distribution. The number and row type is uniquely determined by a square matrix type, so I want to write something like this: #[allow(non_snake_case)] pub struct Multivar𝒩<M: SquareMat<N, V>> { μ: V, Σ:...

Can I create a macro that unrolls loops?

I'm trying to write some fast matrix code in Rust and to do this needs to ensure that loops are unrolled. Is there a way to create a compile-time for-loop? E.g: I want unroll_loop!(f, a, 3); to generate f(a, 0); f(a, 1); f(a, 2); ...

Cargo dependency version syntax

Is there a page documenting the different Cargo syntax for dependencies? So far I have seen three... [dependencies] crate = "1.0.0" # I think this is an exact version match crate = "^1.0.0" # I think this means "use that latest 1.x.x" crate = "*" # I think this means...

Value does not live long enough when using multiple threads

This is a simplified example of my situation: use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc; use std::thread; struct User { reference: String, email: String } fn main() { let rows = vec![ vec!["abcd", "[email protected]"], vec!["efgh", "[email protected]"], vec!["wfee", "[email protected]"], vec!["rrgr", "[email protected]"] ]; let chunk_len = (rows.len() / 2) as usize; let mut chunks...

Calling an impl method from another impl method

It seems I can't call the method of the same struct in Rust or maybe I don't understand something: struct St1 { aa: String } impl St1 { pub fn method1() -> String { //.... method2() //error: unresolved name method2 } pub fn method2() -> String { //.... } }...

What is the most appropriate way to convert nibbles to a u64?

I am trying to learn Rust and decided to write a program that converts a hex string into a u64. Currently, I have parsed the string into a vector of u8 values, each representing four bits (or "nibble"). I wrote the following code to take a Vec<u8> and return a...

Result has no method called “unwrap()”?

What a strange error: let res1 = get_res(); assert!(res1.is_ok()); assert_eq!("just for test", res1.unwrap()); //error The error is: type `core::result::Result<(MyStruct1, collections::btree::map::BTreeMap<collections::string::String, collections::string::String>), Error>` does not implement any method in scope named `unwrap` ...

Conditionally compile only one module at a time

Sometimes while refactoring a change in one of the modules has impact on every other module (or many others), because say a type has been changed or a function parameter has been changed etc. In this case instead of changing everything and then compiling at once is there a way...

How can I open a file with the standard text editor?

My console based program has a config file which can be opened and edited in a text editor. How would I open said config file with the default text editor? I know there is std::process, but I can't get it to work. The program should be able to run on...

vector method push_all is not found for a custom struct

So in this simple example #![feature(collections)] struct User { reference: String, email: String } fn main() { let rows = vec![ vec!["abcd".to_string(), "[email protected]".to_string()], vec!["efgh".to_string(), "[email protected]".to_string()], vec!["wfee".to_string(), "[email protected]".to_string()], vec!["rrgr".to_string(), "[email protected]".to_string()] ]; let mut rows_mut: Vec<Vec<String>> = Vec::new(); rows_mut.push_all(&rows); let mut users_mut: Vec<User> = Vec::new(); let users = vec![ User { reference:...

cannot move out of borrowed content - ref doesn't work [duplicate]

This question already has an answer here: “cannot move out of borrowed context” and “use of moved value” 1 answer I have this code: enum MyEnum1 { val1, val2 } struct Struct1 { field1: MyEnum1, field2: String } fn fn1(a: Struct1, b: String, c: String) { let let1 =...

Situations where Cell or RefCell is the best choice

When would you be required to use Cell or RefCell? It seems like there are many other type choices that would be suitable in place of these, and the documentation warns that using RefCell is a bit of a "last resort". Is using these types a "code smell"? Can anyone...

Lifetime of a mutable element in struct

How can I define a mutable element in a struct? If I have a following example struct User<'a> { reference: String, email: String, addresses: &'a mut Vec<Address> } struct Address { street: String, city: String } fn main() { let mut users = Vec::new(); users.push(User { reference: "ref".to_string(), email: "[email protected]".to_string(),...

Writing Vec to a file

I'm having trouble on writing Vec<u16> content to a file. #[derive(Debug, Copy, Clone, PartialEq)] pub enum ImageFormat { GrayScale, Rgb32, } #[derive(Debug, Copy, Clone, PartialEq)] pub struct ImageHeader { pub width: usize, pub height: usize, pub format: ImageFormat, } pub struct Image { pub header: ImageHeader, pub data: Vec<u16>, }...

Why do I need to use self::core::ops?

I'm trying to use Mul from core. This is suggested by the compiler and works: extern crate core; use self::core::ops::Mul; but why doesn't extern crate core; use core::ops::Mul; work? I get the error error: unresolved import `core::ops::Mul`. Did you mean `self::core::ops`?...

Cannot infer appropriate lifetime for autoderef in Iterator impl

I am trying to implement the Iterator trait for a struct which basically just acts as a borrower of an array of i32 values. However, I keep running into the compiler complaining about not being able to infer a lifetime inside the next method. I am aware of this answer,...

Why does Drop take &mut self instead of self?

Why does Drop’s method have signature fn drop(&mut self) instead of fn drop(self)? This makes it difficult to move values out of the fields e.g. self.join_handle.join() or std::mem::drop(self.file) (error: cannot move out of type X, which defines the Drop trait).

Implementing a generic conversion from an object implementing the `Error` trait

I cannot get the following code to compile. I get an error that From is already implemented. If I remove the manual impl of From I get the error that From is not implemented. If I do not implement Error it works fine. I suppose that this is due to...

Implement Debug trait for large array type

I gather that Rust provides Debug impl's for arrays size 32 and smaller. I also gather that I could implement Debug on a larger array by simply using write! with a very long format specifier. But I'm wondering if there's a better way. What is the recommended method for implementing...

Load a shared library linked to Rust library in R

Following up on this question here, I am having issues using dyn.load to load a shared library that is linked to a Rust dylib. I suspect it has something to do with where R is looking for the Rust dylib, but I have not found a way to specify another...