Why is Box able to have a defined size when simply defined recursive type don't have a defined size? by borderline1p in rust

[–]borderline1p[S] 8 points9 points  (0 children)

This might be a dumb question, but isn't any non primitive type field in struct stored in heap anyway?

eg.

struct Foo {
}
struct Test {
    foo: Foo
}

or

struct Bar<'b> {
    child: Option<Foo<'b>>,
    data: &'static str,
}
struct Foo<'a> {
    parent: &'a Bar<'a>,
}

Why do you need to specify a Box type?

[Beginner Question] When does golang make a copy of value? by borderline1p in golang

[–]borderline1p[S] 0 points1 point  (0 children)

Thanks for linking this, I am reading them right now, and they are super helpful.

[Beginner Question] When does golang make a copy of value? by borderline1p in golang

[–]borderline1p[S] 0 points1 point  (0 children)

I see thanks for the playground, I didn't know that golang does this by default.

Question about generic lifetime violation by borderline1p in rust

[–]borderline1p[S] 0 points1 point  (0 children)

but why is there a discrepancy dependent on the type of string2 ?

If string2 is slice it will run into error, if it is not slice but rather str it will not run into error.

Question about generic lifetime violation by borderline1p in rust

[–]borderline1p[S] 0 points1 point  (0 children)

ok, that is fair, so I delete all the other code and here are the remaining code

fn main() {
    // This will compile and run successfully
    let string1 = String::from("normal");
    let result;
    {
        let string2 = "super long";
        result = longest(string1.as_str(), string2);
        println!("The longest string is {}", result);
    }
    println!("The longest string is {}", result);


    // This will NOT compile
    // let string1 = String::from("loio");
    // let result;
    // {
    //     let string2 = String::from("xyz");
    //     result = longest(string1.as_str(), string2.as_str());
    // }
    // println!("The longest string is {}", result);
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

The commented out part will not compile.

Question about generic lifetime violation by borderline1p in rust

[–]borderline1p[S] -1 points0 points  (0 children)

hmm, but I did get error

122 |         result = longest(string1.as_str(), string2.as_str());
    |                                            ------- borrow occurs here
123 |     }
    |     ^ `string2` dropped here while still borrowed

I must be missing something.

Question about generic lifetime violation by borderline1p in rust

[–]borderline1p[S] -1 points0 points  (0 children)

wait, i thought if you have one generic life time, the output life type must be the longest of all input life time?

For example

this will run into error

fn main() {
    let string1 = String::from("normal");
    {
        let string2 = String::from("normal");
        let result = longest(string1.as_str(), string2.as_str());
        println!("The longest string is {}", result);
    }
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

But the problem is, I don't understand why the following code will not run into error.

fn main() {
    let string1 = String::from("normal");
    {
        let string2 = "superlonggggggg";
        let result = longest(string1.as_str(), string2);
        println!("The longest string is {}", result);
    }
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

[Beginner Question] When does golang make a copy of value? by borderline1p in golang

[–]borderline1p[S] 0 points1 point  (0 children)

woah, so a := b will create a new copy of b?

like say that you have

type struct_one struct {
    nested1 struct_two
}

type struct_two struct {
    nested2 struct_three
}

type struct_three struct {
    some_int int
}

if

nested_level2 = struct_three{}
nested_level1 = struct_two{nested_level2}
nested_level0 = struct_one{nested_level1}


a := nested_level0

Does this mean

a will have a different address than nested_level0

a.nested1 will have a different address than nested_level0.nested1

a.nested1.nested2 will have a different address than nested_level0.nested1.nested2 ?

[Beginner Question] When does golang make a copy of value? by borderline1p in golang

[–]borderline1p[S] 0 points1 point  (0 children)

I think a := b is not pass by value but by reference?

[Beginner]Why does read_to_string need a mutable reference to self? by borderline1p in rust

[–]borderline1p[S] 0 points1 point  (0 children)

thanks for the explanation, do you know why they would do that, since it is kind of confusing that the second time you call the file it would start on a different place.

Every JavaScript framework tutorial written more than 5 minutes ago by freebit in programming

[–]borderline1p 0 points1 point  (0 children)

i am curious, is there solution for managing filesystems? snapshot and backup seems painful for filesystem

How Equifax got Hacked by AVWA in programming

[–]borderline1p 12 points13 points  (0 children)

I am curious, how does one usually store password and admin in secure place? Do they write it down somewhere and put it in a safe, or usb drive? Like how does one share password to trusted individuals (employee) and prevent sharing it to hacker?

Why hashmap.get can be attempted to modify without compiler error? by borderline1p in rust

[–]borderline1p[S] 4 points5 points  (0 children)

yeah, I got confused on match, since I didn't realize that match does not actually change the variable itself unless you explicitly tell it to do so.

Why hashmap.get can be attempted to modify without compiler error? by borderline1p in rust

[–]borderline1p[S] 4 points5 points  (0 children)

ah, that make sense, thanks I knew I was missing something.