Okay, hereâs a thing I like about Rust: Returning things as Option
and error handling. (Or the more complex Result
, but itâs easier to explain with Option
.)
fn mydiv(num: f64, denom: f64) -> Option<f64> {
// (Letâs ignore precision issues for a second.)
if denom == 0.0 {
return None;
} else {
return Some(num / denom);
}
}
fn main() {
// Explicit, verbose version:
let num: f64 = 123.0;
let denom: f64 = 456.0;
let wrapped_res = mydiv(num, denom);
if wrapped_res.is_some() {
println!("Unwrapped result: {}", wrapped_res.unwrap());
}
// Shorter version using "if let":
if let Some(res) = mydiv(123.0, 456.0) {
println!("Hereâs a result: {}", res);
}
if let Some(res) = mydiv(123.0, 0.0) {
println!("Huh, we divided by zero? This never happens. {}", res);
}
}
You canât divide by zero, so the function returns an âerrorâ in that case. (Option
isnât really used for errors, IIUC, but the basic idea is the same for Result
.)
Option
is an enum. It can have the value Some
or None
. In the case of Some
, you can attach additional data to the enum. In this case, we are attaching a floating point value.
The caller then has to decide: Is the value None
or Some
? Did the function succeed or not? If it is Some
, the caller can do .unwrap()
on this enum to get the inner value (the floating point value). If you do .unwrap()
on a None
value, the program will panic and die.
The if let
version using destructuring is much shorter and, once you got used to it, actually quite nice.
Now the trick is that you must somehow handle these two cases. You must either call something like .unwrap()
or do destructuring or something, otherwise you canât access the attached value at all. As I understand it, it is impossible to just completely ignore error cases. And the compiler enforces it.
(In case of Result
, the compiler would warn you if you ignore the return value entirely. So something like doing write()
and then ignoring the return value would be caught as well.)
fn sub(foo: &String) {
println!("We got this string: [{}]", foo);
}
fn main() {
// "Hello", 0x00, 0x00, "!"
let buf: [u8; 8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x00, 0x00, 0x21];
// Create a string from the byte array above, interpret as UTF-8, ignore decoding errors.
let lossy_unicode = String::from_utf8_lossy(&buf).to_string();
sub(&lossy_unicode);
}
Create a string from a byte array, but the result isnât a string, itâs a cow đź, so you need another to_string()
to convert your âstringâ into a string.
- https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8_lossy
- https://doc.rust-lang.org/std/borrow/enum.Cow.html
I still have a lot to learn.
(into_owned()
instead of to_string()
also works and makes more sense to me, itâs just that the compiler suggested to_string()
first, which led to this funny example.)
@lyse@lyse.isobeef.org Rust is so different and, at the same time, so complex â itâs not far fetched to assume that I simply donât understand whatâs going on here. The docs appear to be clear, but alas ⊠is it a bugs in the docs? Is it a lack of experience on my part? Who knows.
By the way, looks like there was a bit of a discussion regarding that name:
So I was using this function in Rust:
https://doc.rust-lang.org/std/path/struct.Path.html#method.display
Note the little 1.0.0
in the top right corner, which means that this function has been âstable since Rust version 1.0.0â. Weâre at 1.87 now, so weâre good.
Then I compiled my program on OpenBSD with Rust 1.86, i.e. just one version behind, but well ahead of 1.0.0.
The compiler said that I was using an unstable library feature.
Turns out, that function internally uses this:
https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.display
And that is only available since Rust 1.87.
How was I supposed to know this? đ€šđ«©
I wanted to port this to Rust as an excercise, but they still have no random number generator in the core library: https://github.com/rust-lang/rust/issues/130703
@arne@uplegger.eu Das ist ein recht zuverlĂ€ssiger Wetterbericht. Wenn die Bauern mit ihren GĂŒllefĂ€ssern hier vorbeifahren, weiĂ ich sofort, dass Regen angekĂŒndigt ist. :-)
Ha, das Lied gefĂ€llt mir auĂerordentlich gut! \o/ Mit Abstand das beste GĂŒllelied. Ich kenn noch ein paar schwĂ€bische, aber die gehen lang nicht so ab wie dieses hier.
@eapl.me@eapl.me @arne@uplegger.eu @andros@twtxt.andros.dev Thanks mates!
Hmmm, Eberhardt. Ist das eine plattdeutsche Sache? Dass ich den flinken Nagern so lang zuschauen konnte, war ein seltener GlĂŒcksfall. Normalerweise sind die nach fĂŒnf oder spĂ€testens zehn Minuten wieder aus dem Sichtfeld verschwunden.
[lang=es] definitivamente es una buena llamada de atenciĂłn para promover mĂĄs donaciones a proyectos opensource. La verdad apoyo menos proyectos de los que âdeberĂaâ, por el valor que me ofrecen.
Una opiniĂłn pragmĂĄtica es que hay la libertad de no pagar, pero tambiĂ©n esto nos deberĂa llevar a que tenemos la libertad de SĂ reconocer los proyectos que nos dan valor, por medio de un donativo puntual o constante. Adaptarnos al contexto de lo que estamos ofreciendo.
Mi chava trabaja en Asociaciones Civiles (tipo OSALs/ONGs) y es un reto pedir donativos, por lo que es comĂșn pedir âCuotas de recuperaciĂłnâ pues ayudan a valorar mĂĄs el servicio, y a que fluya el donativo. Creo que se puede hacer algo asĂ en el cĂłdigo libre, apelando a diferentes motivadores en los usuarios.
it seems to be confused with the subject right next to it.. it works better at the end of the twt string.
Yarn wonât display anything. but the parser does add it to the AST in a way that you can parse it out using twt.Attrs().Get("lang")
https://git.mills.io/yarnsocial/go-lextwt/src/branch/main/ast.go#L1270-L1272
https://git.mills.io/yarnsocial/go-types/src/branch/main/twt.go#L473-L478
lang=en @xuu@txt.sour.is gotcha!
From that PR #17 I think it was reverted? We could discuss about metadata later this month, as it seems that Iâm the only person using it.
Iâve added a [lang=en]
to this twt to see current yarn behaviour.
For point 1 and others using the metadata tags. we have implemented them in yarnd as [lang=en][meta=data]
Heute waren das Ziehkind und ich zwei Stunden lang auf drei SpielplĂ€tzen und quer durch die Stadt unterwegs. Ein riesiger SpaĂ!
Vorab habe ich im hiesigen Spielzeugladen ein Konvolut von Klemmbausteinen erstanden, welche wohl zu einer Polizeistation gehörten!?
cli/q: đ± A simple programming language. - q - Projects I really like this little q lang that Ed has created â€ïž Really nice and simpler, great design and implementation and really lovely cross-platform compiler supporting DOS, Windows, Darwin and Linux on AMD64 and ARM64 đȘ
So Go lang is at a funny version huhâ v1.23.4
will there ever be a v1.23.45678? đ« đ€Ą
[lang=en] Random idea: twtxt.txt
files should be named tw.txt
instead.
This resort, Angsana Lang Co has a big huge canal that goes right around the resort!
# follow_notify = gemini://foo/bar
to your feedâs metadata, so that clients who follow you can ping that URL every now and then? How would you even notice that, do you regularly read your gemini logs? đ€
@movq@www.uninformativ.de @prologic@twtxt.net Hey! I may have found a silly trick to announce my following to people hosting their feeds on the Gemini space using the requested URI
itself instead of relaying on the USER Agent
đ. Iâve copied my current feed over to my (to be) Gemlog for testing. And if I do a jenny -D "gemini://gem.aelaraji.com/twtxt.txt?follower=aelaraji@https://aelaraji.com/twtxt.txt"
and this happens:
A) As a follower, I get the feed as usual.
B) As the feed owner, I get this in logs:
hostname:1965 - âgemini://gem.aelaraji.com/twtxt.txt?follower=aelaraji@https://aelaraji.com/twtxt.txtâ 20 âtext/plain;lang=en-USâ
You could do the same for Gopher feeds but only if you want to announce yourself by throwing in an error in their logs, then youâll need a second request to fetch the feed. jenny -D "gopher://gopher.aelaraji.com/twtxt.txt&follower=aelaraji@https:/aelaraji.com/twtxt.txt"
gave me this :
gopher.aelaraji.com:70 - [09/Sep/2024:22:08:54 +0000] âGET 0/twtxt.txt&follower=aelaraji@https:/aelaraji.com/twtxt.txt HTTP/1.0â 404 0 ââ âUnknown gopher clientâ
NB: the follower=...
string wonât appear in gopher logs after a ?
but if I replace it with a +
or a &
and it works. There will be a missing /
after the https:
. Probably a client thing.
@bender@twtxt.net Iâm using both machines in English.
Checked my locale and it spits out:
LANG=en_US.UTF-8
LC_CTYPE=âen_US.UTF-8â
LC_NUMERIC=âen_US.UTF-8â
LC_TIME=âen_US.UTF-8â
LC_COLLATE=âen_US.UTF-8â
LC_MONETARY=âen_US.UTF-8â
LC_MESSAGES=âen_US.UTF-8â
LC_PAPER=âen_US.UTF-8â
LC_NAME=âen_US.UTF-8â
LC_ADDRESS=âen_US.UTF-8â
LC_TELEPHONE=âen_US.UTF-8â
LC_MEASUREMENT=âen_US.UTF-8â
LC_IDENTIFICATION=âen_US.UTF-8â
LC_ALL=
đ€·đœ ⊠and that only happens when vi, vim or nvim are launched by Jenny to compose a twt.
>
?
@eapl.me@eapl.me this is interesting. Is the square bracket something used in the wild for multilingual twts?
@prologic@twtxt.net what are your thoughts? Should we extend the parser to handle [lang] and [boost] ? Or a generic attribute spec. Single word is a boolean attribute. And one with an =
is a string key/value.
[lang=en] hey! What are you playing now?
Iâve been into PokĂ©mon Black, although itâs going slowly and switched to a more recent game, Inscryption, which has a 90s-2000s vibes.
[lang=en] By the way, have you played with Station on Gemini?
I like that using Gemtext, you can have a pretty decent microblogging platform. Imagine that with decentralization from twtxt. That sounds appealing to me!
[lang=en] you can find it here: https://github.com/eapl-gemugami/twtxt-php
There are still many things to do, but itâs already usable.
[lang=en] I love this topic about definitions, itâs key to have meaningful conversations. Thanks for sharing that link.
Communication is vital for succeeding in any activity that requires the participation of more than one individual.
This is why it is so important to be tight and consistent with definitions and to fully understand them.
Love this
[lang=en] That was the reason for twtxt-php =P
I tried using CLI tools but it was too hacky, I think.
More if we consider Jakobâs Law, where we have prior expectations of a microblogging system.
A Web interface could be quite minimalistic and usable as well. (And mobile-friendly)
@prologic@twtxt.net https://twitter.com/ardenthistorian/status/1625653951776292864?lang=en
lolol actually, Iâm now building a quickânâdirty repl in C to test some mechanics, ended up implementing a small VM, adding sound asap, letâs see where that leads me #crow #raven #lang #coding #sound #livecoding #nyx
@movq@www.uninformativ.de This is my env, on language:
LANGUAGE=en_US.UTF-8
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
I had to explicitly set it on the cron job to make jenny work.
@lyse@lyse.isobeef.org Yup, I did. I setup the three of them: LC_ALL
, LANGUAGE
, and LANG
. Working as intended now, beautifully! Thank you!
@lyse@lyse.isobeef.org I think that was it, mate! đ I was calling . $HOME/.bashrc
on the cron job line, but was missing some extra LANG
ones. Letâs see how it goes now.
@quark@ferengi.one I mean, if LANG=en_US.UTF-8
were a problem, it wouldnât run manually, right? Or is it that the variable isnât defined under cron?
@lyse@lyse.isobeef.org Yes, I have LANG=en_US.UTF-8
on my system. So, it is not that it canât find the config?
@prologic@twtxt.net would that need a NLP library? The lang would be great for a search engine to find language prefs.
@xuu@txt.sour.is Speak of lang⊠Do you think we could detect the userâs lang by what they write? Probably just inspect a random subset?
The keys that have some use would be nick, url/feedurl, avatar, lang
@prologic@twtxt.net yep!some of the lexer is directly copied from monkey-lang. love that book series.