0

I am writing a library in rust which builds a self-referential data structure; it uses indexes internally for nodes which are actually identified by URIs (or IRIs which is the same thing).

I have a method like so:

fn iri(self, s: String)-> IRI

IRI is actually just a type alias for usize:

struct IRI(usize)

so that I can do this, where ont is the main data structure:

let i = ont.iri("http://example.com");

I can then pass this to further methods:

let c = ont.class(i);

This is okay, but I'd like to combine these into one line:

let c = ont.class(ont.iri("http://example.com"))

This fails because I can't borrow the reference to ont twice. Both the iri and class calls are mutable and need to be.

Is there any way of achieving this without an intermediate variable?

Phil Lord
  • 2,917
  • 1
  • 20
  • 31
  • 1
    Have you tried defining `iri` as `fn iri(&self, s: String) -> IRI` and `class` similarly? Note that Rust doesn't borrow by default, so that might be your issue. – Claudia Dec 31 '17 at 09:16
  • Both methods change the `ont` data structure unfortunately, so need to borrow mutably. I've added clarification to the question. – Phil Lord Dec 31 '17 at 09:22
  • `IRI` is not a type alias - it's a wrapper struct. A type alias would be `type IRI = usize`. – ljedrz Dec 31 '17 at 09:22
  • There is no way of working around this. The reasons are given in the accepted answer to the question you yourself linked above. – EvilTak Dec 31 '17 at 10:45

0 Answers0