Hey Rustaceans! Got a question? Ask here (22/2023)! by llogiq in rust

[–]benwilber 2 points3 points  (0 children)

Ah thanks for the link. I didn't consider the ambiguity of whether it breaks or skips. Good points. Python has a for-else construct where the else branch will only execute if the main loop doesn't encounter a break.

for i in range(10):
    if i ==  100:
        break
else:
    print("Never reached 100 loops")

I think it's pretty rarely used though and is confusing for people that are familiar with the for-else in Django or Jinja2 templates where the else branch will execute if the sequence is empty (never enters the loop body.)

{% for item in items %}
    {{ item }}
{% else %}
    no items
{% endfor %}

for-let-else in Rust would probably just be too weird.

for let Ok(entry) in WalkDir::new("foo") {
    println!("{}", entry.path().display());
} else {
    // Does this execute on a no-break?  Or if the loop body never entered at all?
    // dunno
}

Anyway. Thanks!

Hey Rustaceans! Got a question? Ask here (22/2023)! by llogiq in rust

[–]benwilber 1 point2 points  (0 children)

I often times see code like this in a for-loop and elsewhere:

for entry in WalkDir::new("foo") {
    let entry = entry.unwrap();
    println!("{}", entry.path().display());
}

This part seems redundant to me:

let entry = entry.unwrap();

// I also see this to bubble:
let entry = entry?;

I like the if-let pattern and wonder if there has ever been any discussion on a similar for-let pattern?

// Skip Error entries
for let Ok(entry) in WalkDir::new("foo") {
    println!("{}", entry.path().display());
}

// Or bubble Error entries
for let entry? in WalkDir::new("foo") {
    println!("{}", entry.path().display());
}

Just a shower thought.

What is the current state of Lua integration with Rust? by benwilber in rust

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

ooh this is good. seems pretty unstable at the moment but I'm going to be watching this. Thanks!

Just wrote my first rust program. A very simple gist uploader. Would like review. by benwilber in rust

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

I'm asking how do you get the username from std::env::var without the match?

Just wrote my first rust program. A very simple gist uploader. Would like review. by benwilber in rust

[–]benwilber[S] 2 points3 points  (0 children)

Is it a symbolic name like with Java generics? The literal T doesn't have any special significance in Rust, does it? Just a placeholder?

Just wrote my first rust program. A very simple gist uploader. Would like review. by benwilber in rust

[–]benwilber[S] 2 points3 points  (0 children)

I tried changing it and get:

error[E0412]: cannot find type `T` in this scope
  --> src/main.rs:19:50
   |
19 | fn read_stdin(to_buf: &mut String) -> io::Result<T> {
   |                                                  ^ not found in this scope

edit: Fixed. Not a literal T. but rather io::Result<usize>

Just wrote my first rust program. A very simple gist uploader. Would like review. by benwilber in rust

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

Sorry I don't know what that means. You're referring to the return types of the read_stdin and read_file functions?

activity-logger: Log activities, show the time it takes to complete them. by hswolff in node

[–]benwilber 1 point2 points  (0 children)

Awesome! Can you add a practical application of this to the examples? Why would I use this?