T
pezcore: How can it be possible to borrow self.slice in the expression &mut self.slice on the last line when self.slice is already exclusively borrowed by self? This, again, is a case where you can characterize the reborrow fully by what would also work, pretending this was a borrow of the variable self then projected down to a reference to self.slice. In types, because English can be messy when talking about reference to reference to stuff, I mean: start with self: &'s mut MyIterMut<'a, T>…
T
That's an example of ownership, still.
T
Amazing! I looked at what it is and it might be very helpful for muscle memory! Thanks for the suggestion friend!
T
Levied: Do concepts like ownership and borrowing specifically exist in Rust to prevent UB in multithreaded context? Obligatory link: manishearth.github.io The Problem With Single-threaded Shared Mutability - In Pursuit of Laziness Edit (Jan 2017): I re-discovered Niko’s post which touches on this and reaches for the same realization. I suspect I subconsciously got the idea for this from that post, at least in part. This is a … Over a decade old now, but still perfectly applicable.
T
I don't know about Mac, but for most Linux distributions this seems quite pointless, since rustup ships as a distro's package that already pulls in all the necessary dependencies via the respective package manager. On my system: > LANG=C pacman -Qi rustup Name : rustup Version : 1.29.3-1 Description : The Rust toolchain installer Architecture : x86_64 URL : https://github.com/rust-lang/rustup.rs Licenses : MIT Apache-2.0 Groups : None Provides : rust cargo rust-nightly cargo-nightly rustfmt…
T
I am working on a project that allows Rust to be installed on the following operating systems: Linux, Windows and macOS. The aim is to check that the necessary libraries are installed and that rustup is not installed. At the moment, I only have it set up for one Linux distribution, but the plan is to gradually extend it to other distributions, as well as Windows and macOS. I’d like to hear your feedback. I know the code isn’t quite up to scratch yet, but the idea is to create a working…
T
MusicalNinjaDad: As for new lints, I think I would be most likely to make a 5 minute PR to set that lint to allow in Cargo.toml and add a linked issue. Thus staying up-to-date on my toolchain and having control over when I address the new lint. I've not yet suffered from this (as far as I can remember) have you had the problem often? It happened a few times to me that a new Clippy version found ways how I can make my code more efficient or easier to read, and usually that was quite helpful.…
T
Actually, I wonder if your real intent was to use references for the iteration as in fn main() { const CLEAR_THE_DECKS: u32 = 42; let mut input = vec![1]; // Code filling in the Vec for &item in &input { if item == CLEAR_THE_DECKS { input.clear() } } } as this gives in the playground the expected error: Compiling playground v0.0.1 (/playground) error[E0502]: cannot borrow `input` as mutable because it is also borrowed as immutable --> src/main.rs:8:13 | 6 | for &item in &input { | - ------ | |…
T
Thanks for response, you help me so, the error is was enter parameter
T
Hi, I've been working on zeroio. My goal is to build a fast network dataplane in Rust using XDP and a userspace TCP stack, with support for congestion control, SNI/QUIC forwarding and DDoS protection. So far, L7 performance has been quite disappointing compared to just using the kernel TCP stack. Has anyone here tried something similar? I'm wondering if I'm missing something obvious or if userspace TCP just isn't a good fit for this kind of workload. Would love to hear your thoughts.
T
Assuming you are making a cli program, you probably just want to let sudo ask the user for the password. sudo should automatically ask for the password on the tty through /dev/tty even when stdin and stdout are redirected.
T
geek_of_the_west: I cannot afford "the book" Normally "the book" means this free online book, The Rust Programming Language: doc.rust-lang.org The Rust Programming Language - The Rust Programming Language
T
I need install a packege how to curl in linux, I need to install a package such as Curl on Linux, but the problem is that when it comes to installing it, I don’t know how to enter the superuser password when prompted. Here is the function I’m using. use std::process::Command; pub fn install_curl(package_manager: &str, args: &[&str]) -> bool { Command::new(package_manager) .args(args) .output() .map(|value| value.status.success()) .unwrap_or(false) } Example of command with Arch: sudo pacman -S…
T
This problem has been solved by the OP. The problem was that in writing the source (for a modified hello world program after using: cargo new hello I wrote hello.rs in that folder, rather than modifying ./src/main.rs and running: cargo build cargo run once I did that, compilation worked after: cargo add clearscreen before this I was explicitly running: rustc hello.rs Thanks for the replies, I cannot afford "the book" yet, learning by reading others and web searches. Been a rough row to hoe…
T
pezcore: "When a value is exclusively borrowed twice the latter borrow has to end before the former borrow can be used again" Yes, and that's why the term "active references" is better if substituted in the original rule. Active, meaning it is actually used.
T
I've been working on koru.do It is an open source workspace for notes, files, tasks and your own AI. It is written in Rust Works fully offline and you can p2p sync with other devices. It has built in OCR support for PDFs and images. You can run your agents inside Koru It has a built in MCP server so you can connect agents and collaborate on notes. The next apps coming are Koru Bots (an alternative to Grok Bots) and p2p collaboration. Please star if you like it Koru in Github
T
That's reborrow. Somewhat underdocumented feature — yet, again, something that one needs to make language usable. Without it many important patterns become impossible… and, again, it's built around refinement about what does it mean for two borrows to be “exclusive”.
T
In your example: pub struct MyIterMut<'a, T> { slice: &'a mut [T] } impl<'a, T> Iterator for MyIterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { // Temporarily take the slice. This is really needed only to make some lifetimes match up :( let slice = std::mem::take(&mut self.slice); How can it be possible to borrow self.slice in the expression &mut self.slice on the last line when self.slice is already exclusively borrowed by self? It seems to me from this…
T
Yeah, something like that maybe. Edit: To clarify, thanks for the code. My hesitance here is more that I have to go through the documentation and re-verify things for safety, re-write my code documentation, things like that. Edit 2: Yeah it's possible to not use CStr but I'm also using CString/CStr to convert to C strings anyway so I might as well use it.
T
unsafe fn cstr_len(cstr_ptr: *const c_char, max_len: isize) -> Option<usize> { for pos in 0..(max_len + 1) { let current_ptr = unsafe { cstr_ptr.offset(pos) }; let current_char = unsafe { current_ptr.read() }; if current_char == 0 { return Some(pos); // including terminator (to match fn name semantics) } } None } pub unsafe fn cstr_to_string(cstr_ptr: *const c_char, max_len: isize) -> Option<String> { let length = unsafe { cstr_len(cstr_ptr, max_len) }?; let cstr = unsafe {…
T
The code looks something like this (documentation/SAFETY notes removed, I don't want to clog up this thread): unsafe fn cstr_len_longer(cstr_ptr: *const c_char, max_len: isize) -> bool { for pos in 0..(max_len + 1) { let current_ptr = unsafe { cstr_ptr.offset(pos) }; let current_char = unsafe { current_ptr.read() }; if current_char == 0 { return false; } } true } pub unsafe fn cstr_to_string(cstr_ptr: *const c_char, max_len: isize) -> Option<String> { let too_long = unsafe {…
T
I haven't tried it myself yet, but Advent of code could be a fun way to practice once you understand the basics of rust.
T
Jookia: For my own code I'm going to write a length check function that checks the length of a string before passing it to CStr::from_ptr. Wouldn't you have effectively allowed yourself to skip the CStr entirely if you do that. I'm thinking you'll be iterating for i in 0..MAX through each bytes with pointer.add & read then checking for \0 (?) At which point you could slice::from_raw_parts and have a [u8] to convert to utf8?? Or am I missing a simpler way to check
T
how are you running the project? cargo run or something else? and how did you create it? via cargo new or manually?
T
If you're using a more recent edition than 2015 (and you should be), then extern crate <crate>; is unnecessary 99% of the time. Does your Cargo.toml contain an edition field under [package]? What is it set to? More importantly, what error message are you getting when you use extern crate <crate>;?
T
Yes, it is explicit, but less work than it sounds: A type which never changed needs only an impl Schema; the defaults handle version 0. You keep an old struct only while nodes running the old version may be around, usually just the previous one. After the rollout it can go. Bumping VERSION is one deliberate line: only you know whether a change alters the serialized shape. Nested types need no dispatch of their own. The schema version belongs to the message type, so a change in a nested struct…
T
Interesting. Well I'm not sure where to go from here. For my own code I'm going to write a length check function that checks the length of a string before passing it to CStr::from_ptr. I guess it doesn't matter too much in practice, but it feels weird to just ignore a safety requirement most the time.
T
the error is the parameter type `P` may not live long enough the parameter type `P` must be valid for the static lifetime...rustcClick for full compiler diagnostic test30.rs(11, 17): consider adding an explicit lifetime bound: ` + 'static` the parameter type `P` may not live long enough ...so that the type `P` will meet its required lifetime boundsrustcClick for full compiler diagnostic test30.rs(11, 17): consider adding an explicit lifetime bound: ` + 'static` lifetime may not live long enough…
T
Again, though, platform specific. The 32 bit ARMv7 Linux platform I work on uses signed integers in the allocator, so while I have the address space for a bigger than 2 GiB allocation, nothing can actually allocate that much. And a quick experiment shows that if I try and mmap a 2 GiB + 1 page file, I get EINVAL back, despite /proc/self/maps showing that I have 2.5 GiB of contiguous address space available - something (whether it's in libc or the kernel, I don't know) simply won't let me have a…
T
So, there are no convenience mechanisms at all, and the developer still has to keep all old versions of a struct and manually bump wire version, has to manually dispatch for sub-structs contained inside?