#Borrow checker saying stuff I don't understand.

49 messages · Page 1 of 1 (latest)

open edge
#

I'm trying to implement a simple calculator Lexer(just trying, dunno how I'm gonna parse it).
While doing it, the borrow checker is boring me... a lot. In the code below, it complains about something like "creating tempory value", when I created nothing, I'm just using native code. ```rs
let value = match self {
Sign::PlusSign => "+",
Sign::MinusSign => "-",
Sign::MultiplicationSign => "*",
Sign::DivisionSign => "/",
Sign::Number(value) => value.to_string().as_str(),
};

    String::from(value)

I solved that by calling `String::from` in every match, but I don't understand why it doesn't work. What I solved with:rs
let value = match self {
Sign::PlusSign => String::from("+"),
Sign::MinusSign => String::from("-"),
Sign::MultiplicationSign => String::from("*"),
Sign::DivisionSign => String::from("/"),
Sign::Number(value) => value.to_string(),
};

    value

``` PS: Sign::Number holds an i32

edgy tiger
#

I created nothing,
the thing created is value.to_string()

#

that is a newly-allocated string

open edge
#

?

#

But it's a method in the core, I didn't do anything.

edgy tiger
#

you called to_string(), which returns a String value

#

then you called as_str() on it, which borrows that String

#

but then the string is dropped, by exiting that scope, before the borrow (value) is used

#

you need to avoid doing that

#

which you did by using String instead of &str in the match

open edge
#

I didn't understand anything, lol.

open edge
#

basically the same thing..

edgy tiger
#

as_str() borrows the String it's called on

#

as_str() returns the borrow, not the string

#

the problem is that the String didn't get stored in any variable

open edge
#

so I have to do a block? wot..

edgy tiger
#

in this situation you can't (or well, can't directly)

#

your second code block is one correct way to handle this problem

#

there are ways to make it a little more concise, though

open edge
#
let smth = match self {
  Sign::Number(value) => {
    let string_value = value.to_string();

    string_value.as_str()
  }
}```Like that?
#

Like, this is confusing, lol

edgy tiger
#

no, that does not help because string_value is out of scope before the match finishes

#

there is a way to use a variable to extend it

open edge
#

?

#

what do I do them

#

cause I don't want to repeat String::from everytime.

edgy tiger
#

you can do this:

let tmp_str;
let value = match self {
    Sign::PlusSign => "+",
    Sign::MinusSign => "-",
    Sign::MultiplicationSign => "*",
    Sign::DivisionSign => "/",
    Sign::Number(value) => {
        tmp_str = value.to_string();
        tmp_str.as_str()
    }
};

String::from(value)

this is a bit of an odd technique but it does work when you just need to make one temporary value live longer

#

however, this isn't a great solution to the bigger problem

#

because you're allocating a string and then copying it into a second string (the String::from(value))

open edge
#

not my fault.

#

i32's fault

edgy tiger
#

here's how to make the match with conversion less busy:

match self {
    Sign::PlusSign => "+".into(),
    Sign::MinusSign => "-".into(),
    Sign::MultiplicationSign => "*".into(),
    Sign::DivisionSign => "/".into(),
    Sign::Number(value) => value.to_string(),
}
open edge
#

for not having as_str or something

edgy tiger
#

it's not possible for i32 to have an as_str, because as_str() is "give me a reference to the characters that are already in memory" and an i32 is not made of characters

open edge
#

it could do .to_string().as_str() in back, dunno.

#

anyways, now I have to think a way to parse stuff... somehow.

#

cause I dunno how to do it

edgy tiger
edgy tiger
open edge
#
pub enum Sign {
    PlusSign,
    MinusSign,
    MultiplicationSign,
    DivisionSign,
    Number(i32),
}```this kind of parsing.
edgy tiger
#

ah, I have no advice there as I haven't tackled that problem yet for myself

open edge
#

and I literally dunno how to do it

edgy tiger
#

I know there are libs for parsing but I haven't tried any

open edge
#

I'm not redoing this...

#

Anyways, I'm probably giving up on it, but thx for answering.

#

Cause I don't think I'll be able to parse those.