A little bit of neuroscience and a little bit of computing
I imagine the fact that both of those are interpreted languages plays somewhat heavily into it.
Yea I’d imagine so too.
Yep. And then you realise that “move semantics” aren’t just a safety net that you have to “fight with” but actually a language feature against which you can develop/deploy desirable patterns.
A minor thing I noted reading your code snippets was that I immediately noticed, like at a gestalt level, the lack of ampersands (&
) and therefore references and could immediately tell that this was a “faux-O”/pipline style system. Not too bad for a syntax often derided as messy/bad.
Personally, I find this incredibly elegant
I’m not entirely sure I understand exactly what you mean here.
Do you appreciate it as an implementation design for the language (I do too)?
Or do you see some utility in being able to call MyStruct::my_method(&my_var)
… or both, cuz there’s something assuring in knowing the simple pattern underneath the syntactic sugar is there?
No worries!
I myself am not on top of the “smart pointers” (yet) … so I’m hoping it’ll be helpful when I go through that just to keep perspective.
I believe you … gate-keeping types are all over the place really.
But vegans or the “vegan-curious” aren’t one thing or one kind of person, at least not any more. I personally have only ever had positive conversations with vegan types.
This instance, in being insistent on not entertaining any “harm reduction” or “compromises”, makes sense though … because it’s a space for people to talk about that sort of dedicated approach.
they drive away potential allies because the concept of harm reduction is anathema to their binary thinking. If you’re not ALL in, you’re the enemy.
I can resonate with that. But I come back to … “it’s totally ok for people to create their own spaces, especially on federated social media and especially for minority groups/ideas”.
There are likely plenty of other spaces for “potential allies” to engage and talk about veganism if they want to, or plenty they, or you, could make on their own.
Tacitly admitting that vegans are usually antisocial zealots. “It’s right in the name!”
Well, they’re running their own social media platform, so I’m not sure how anti-social they are.
Right. Well, I think the instance name “vegantheory.org” was doing that already, and I’m betting you drew your conclusion from the public description of the place too (and aren’t in the modlog or anything for challenging their ideas).
And what would a non-vegan want to do in there?
What’s wrong with minority views and practices creating their own spaces?
On which, is there any non-vegan/anti-vegan thought or idea that a vegan is likely to have not heard already? How many haven’t they heard relative to the amount of decent “pro-vegan” ideas they also haven’t heard of?
Maybe a specialised space, echo chamber even, makes sense in order to balance against the gravity of the mainstream?
I’ve peaked at that issue a couple of times, but I never worked out what the issue/feature got stuck on. Naively I would have thought it a relatively workable feature to add.
/// Makes a string to separate lines of text,
/// returning a default if the provided string is blank
fn make_separator(user_str: &str) -> &str {
if user_str == "" {
let default = "=".repeat(10);
&default
} else {
user_str
}
}
When compiling, what’s the best description of the compiler error?
Cannot return reference to a local variable
&default
isn’t allowed as default
is local to the function.
"=".repeat(10)
directly?String
(requires converting user_str
to a String
with to_string()
)Context: Because default lives on the stack within make_separator, it will be deallocated once a call to make_separator ends. This leaves &default pointing to deallocated memory. Rust therefore complains that you cannot return a reference to a local variable.
/// Makes a string to separate lines of text,
/// returning a default if the provided string is blank
fn make_separator(user_str: &str) -> &str {
if user_str == "" {
let default = "=".repeat(10);
&default
} else {
user_str
}
}
Normally if you try to compile this function, the compiler returns the following error:
error[E0515]: cannot return reference to local variable `default`
--> test.rs:6:9
|
6 | &default
| ^^^^^^^^ returns a reference to data owned by the current function
Assume that the compiler did NOT reject this function. Which (if any) of the following programs would (1) pass the compiler, and (2) possibly cause undefined behavior if executed? Check each program that satisfies both criteria, OR check “None of these programs” if none are satisfying.
// 1
let s = make_separator("");
// 2
let s = make_separator("");
println!("{s}");
// 3
println!("{}", make_separator("Hello world!"));
&default
, which results in a dangling pointer being returned. Then, it’s any program that uses the returned reference (so printing will do the trick)Context: First, the caller must pass an empty string to trigger the problematic if-condition. This returns a dangling pointer. Second, the caller must use the result of make_separator, e.g. via println.
Of the following fixes (highlighted in yellow), which fix best satisfies these three criteria:
1:
fn make_separator(user_str: &str) -> &str {
if user_str == "" {
let default = "=".repeat(10);
&default
} else {
&user_str
}
}
2:
fn make_separator(user_str: String) -> String {
if user_str == "" {
let default = "=".repeat(10);
default
} else {
user_str
}
}
3:
fn make_separator(user_str: &str) -> String {
if user_str == "" {
let default = "=".repeat(10);
default
} else {
user_str.to_string()
}
}
3
default
user_str
to a String
to keep a consistent return typeString
2
is too restrictive in requiring use_str
to be a String
1
doesn’t solve the problemfn make_separator(user_str: &str) -> String {
if user_str == "" {
let default = "=".repeat(10);
default
} else {
user_str.to_string()
}
}
Context: There is no valid way to return a pointer to a stack-allocated variable. The simple solution is therefore to change the return type to String and copy the input user_str into an owned string. However, requiring user_str to be a String would reduce the flexibility of the API, e.g. a caller could not call make_separator on a substring of a bigger string. It would also require callers to heap-allocate strings, e.g. they could not use a string literal like make_separator(“Rust”).
The most idiomatic solution to this problem uses a construct you haven’t seen yet: Cow - Clone-on-write. The clone-on-write smart pointer would enable this function to return either an owned string or a string reference without a type error.
/// Gets the string out of an option if it exists,
/// returning a default otherwise
fn get_or_default(arg: &Option<String>) -> String {
if arg.is_none() {
return String::new();
}
let s = arg.unwrap();
s.clone()
}
2
arg
in arg.unwrap()
arg
is a reference.unwrap()
has signature unwrap(self) -> T
: it takes ownership!unwrap
cannot take ownership (arg
doesn’t have ownership to move/give).Context: The function Option::unwrap expects self, meaning it expects ownership of arg. However arg is an immutable reference to an option, so it cannot provide ownership of the option. Therefore the compiler complains that we cannot move out of arg via unwrap.
/// Gets the string out of an option if it exists,
/// returning a default otherwise
fn get_or_default(arg: &Option<String>) -> String {
if arg.is_none() {
return String::new();
}
let s = arg.unwrap();
s.clone()
}
Options:
// 1
let opt = Some(String::from("Rust"));
let s = get_or_default(&opt);
println!("{}", s);
// 2
let opt = Some(String::from("Rust"));
get_or_default(&opt);
// 3
let opt = Some(String::from("Rust"));
get_or_default(&opt);
println!("{:?}", opt);
All programs (1, 2 and 3).
Once arg.unwrap()
occurs, s
takes ownership of the underlying String
.
Once s
“dies”, the String
is deallocated.
But, opt
, from before the call to get_or_default
also owns the same String
, and so once it “dies” and its memory is deallocated, a double-free
will occur.
This actually threw me at first
My answer was program 3
, as I figured that opt
had to be used in some way for “undefined behaviour” to occur, as only then, did I figure, would the inappropriate memory be used resulting in an incorrect result
This is kinda wrong, though, because deallocation occurs once opt
’s lifetime ends, which will cause a double-free. println
prolongs the lifetime while in program 2
the lifetime of opt
clearly ends, I suppose, causing the double-free.
Of the following fixes (highlighted in yellow), which fix best satisfies these three criteria:
/// Gets the string out of an option if it exists,
/// returning a default otherwise
fn get_or_default(arg: &Option<String>) -> String {
if arg.is_none() {
return String::new();
}
let s = arg.unwrap();
s.clone()
}
1:
fn get_or_default(arg: &Option<&str>) -> String {
if arg.is_none() {
return String::new();
}
let s = arg.unwrap();
s.to_string()
}
2:
fn get_or_default(arg: &mut Option<String>) -> String {
if arg.is_none() {
return String::new();
}
let s = arg.as_mut().unwrap();
s.clone()
}
3:
fn get_or_default(arg: Option<String>) -> String {
if arg.is_none() {
return String::new();
}
let s = arg.unwrap();
s.clone()
}
4:
fn get_or_default(arg: &Option<String>) -> String {
match arg {
None => String::new(),
Some(s) => s.clone()
}
}
4
4
is a better, more idiomatic version of 3
, especially because it requires ownership of arg
which is restrictive and may not even be available
3
does fix the problem1
doesn’t fix the problem2
… I’m not sure about … but I don’t think having a mutable s
helps with the problem either (?)Context: The combination of is_none and unwrap here is a Rust anti-pattern, since a match combines the two functionalities and automatically deals with pushing the reference &Option into the interior to produce &String. Therefore the match solution is the most idiomatic, and passes the compiler without changing the intended type signature of the function.
The solution of changing &Option to Option is not desirable because it requires the caller to provide ownership of their option, which is a far more restrictive API.
deleted by creator
Every browser released since 2020 supports this
It’s a little paranoid of me, but I like the idea that a basic web app I make can be thrown onto any old out of date machine, where ~2015 or younger seems about right for me ATM.
You mean the Html template Element? I’ve never really got that to work, but I also never seriously tried.
Yea. From memory, it’s just an unrendered chunk of HTML that you can select and clone with a bit of JS. I always figured there’d be a pattern that isn’t too much of a cludge and gets you some useful amount of the way to components for basic “vanilla-js” pages, just never gave it a shot either.
Yea, I’m unclear on how you can take web components and still have widespread browser support (not knowing enough about their ins and outs).
Plain template elements are widely supported and have been for ~10 years (which ideologically matters to me along the same lines as the top post’s article) … perhaps a little bit of hacking together can get you close with just that?
But nah. These goobers got high off npm modules and did shots of JSX in the bathroom at lunch time.
Fucking LoL!
Anyone here have thoughts about doing basic interactive stuff with at most vanilla JS?
Yea anything big and mainstream just seems super shallow.
I’m not on top of things to compare accurately, but it was always kinda like that (and is like that here sometimes too). But whenever I’ve gone back, I’ve definitely felt like it has gotten somewhat worse. Some of that could easily be a shifting standard from spending more time on other less “mainstream” platforms though.