推荐 最新
花影流年

veloren/veloren

灵感来自《塞尔达传说:旷野之息》、《矮人要塞》和《我的世界》等游戏。虽然这款游戏的画质低,但拥有广阔

12
0
0
浏览量62
无情编码机器

请问什么是gpui?

我在看最新的开发者IDE: Zed的时候, 发现它有一个标签:gpui 请问"gpui" (https://link.segmentfault.com/?enc=ghiv%2BaTy%2B5s1bYuQbXqCmQ%3D%3D.Ddt6dJj4GqFL5BodI%2Fcpho4ODZHQHcymXDFmdemTTeA%3D)是什么? 和 GPU 有关系吗? "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250113/f73033eea74045179168ef90d05ba565.png)

16
1
0
浏览量274
博学的学渣

rust 怎么与长期存活子进程获取输出 ?

下面的两个尝试代码都是:rust version :"rustc 1.71.0" 尝试1: use std::io::{BufRead, BufReader}; use std::process::{Command, Stdio}; use std::thread; use std::thread::sleep; use std::time::Duration; fn start_listener(cb: T) { let mut child = Command::new("jupyter") .arg("lab") .arg("--no-browser") // .arg("google.com") // .stdout(Stdio::inherit()) .stdout(Stdio::piped()) .spawn() .expect("Failed to start ping process"); println!("Started process: {}", child.id()); child.wait(); thread::spawn(move || { let mut f = BufReader::new(child.stdout.unwrap()); loop { let mut buf = String::new(); match f.read_line(&mut buf) { Ok(_) => { cb(buf.as_str()); } Err(e) => println!("an error!: {:?}", e), } } }); } fn main() { start_listener(|s| { println!("Got this back: {}", s); }); sleep(Duration::from_secs(5)); println!("Done!"); } 使用这个代码启动 jupyter lab 启动是正确, 但 rust 这边一直没有运行 "println!("Got this back: {}", s);" 是为什么? 没有接收到 子进程的输出? 尝试2 use std::io::{BufRead, BufReader, Write}; use std::process::{Command, Stdio}; use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::Mutex; use std::thread; use std::thread::sleep; use std::time::Duration; fn start_process(sender: Sender, receiver: Receiver) { let child = Command::new("jupyter") .arg("lab") .arg("--no-browser") .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() .expect("Failed to start process"); println!("Started process: {}", child.id()); thread::spawn(move || { let mut f = BufReader::new(child.stdout.unwrap()); let mut stdin = child.stdin.unwrap(); for line in receiver { stdin.write_all(line.as_bytes()).unwrap(); let mut buf = String::new(); match f.read_line(&mut buf) { Ok(_) => { sender.send(buf).unwrap(); continue; } Err(e) => { println!("an error!: {:?}", e); break; } } } }); } fn start_command_thread(mutex: Mutex>) { thread::spawn(move || { let sender = mutex.lock().unwrap(); sleep(Duration::from_secs(3)); sender .send(String::from("Command from the thread\n")) .unwrap(); }); } fn main() { let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); start_process(tx1, rx2); tx2.send(String::from("Command 1\n")).unwrap(); start_command_thread(Mutex::new(tx2)); for line in rx1 { println!("Got this back: {}", line); } } 下面的仍然是没有执行,这是为什么 for line in rx1 { println!("Got this back: {}", line); }

17
1
0
浏览量283
PunkMaccccc

rust reqwest get请求中文乱码如何解决?

use reqwest; // 定义一个自定义错误类型,可以保存 InvalidHeaderValue 或 reqwest::Error。 #[derive(Debug)] enum MyError { InvalidHeader(reqwest::header::InvalidHeaderValue), Reqwest(reqwest::Error), } impl From for MyError { fn from(error: reqwest::Error) -> Self { MyError::Reqwest(error) } } #[tokio::main] async fn main() -> Result { // 创建一个 reqwest 客户端。 let client = reqwest::Client::new(); // 定义 User-Agent 值;用你实际的 User-Agent 替换它。 let user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31"; let accept = "Accept: */*"; let accept_encoding = "Accept-Encoding: gzip, deflate, br"; let connection = "Connection: keep-alive"; let content_type = "Content-Type: application/json; charset=UTF-8"; // 创建一个请求构建器,设置 URL 和头部。 let response = client .get("https://api.bilibili.com/x/space/wbi/arc/search?mid=259333&ps=1&pn=1") .header(reqwest::header::USER_AGENT, user_agent) .header(reqwest::header::ACCEPT, accept) .header(reqwest::header::ACCEPT_ENCODING, accept_encoding) .header(reqwest::header::CONNECTION, connection) .header(reqwest::header::CONTENT_TYPE, content_type) .send() .await?; let body = response.text().await?; println!("{:?}", body); Ok(()) } "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241225/21ac3146dcbd885e9c87eaa039e37e51.png) 思路一:更改编码 本人尝试无效 思路二:编译器编码问题 不知从何入手未尝试 思路三:需要处理gzip 本人尝试无效 希望大佬们可以赐教

14
1
0
浏览量269
kunkun小黑子

Rust中借用和隐式重借用的区别?

rust新手,看到隐式重借用,有些不解,望能够帮忙解答。 下面代码都是重复可变引用,但为什么通过隐式重借用方式,就可以实现多个可变引用?rust不是明确说同一时间只能有一个可变引用吗? // err 能够理解,rust明确不能有两个可变引用。 fn compile_err() { let mut s = String::from("hello"); let r1 = &mut s; { let r2 = &mut s; dbg!(r2); } dbg!(r1); // compile ERR } // 但通过隐式重借用,为啥就可以,(*r1) 解应用的最终对应的不还是 s? fn compile_ok() { let mut s = String::from("hello"); let r1 = &mut s; { let r2 = &mut (*r1); dbg!(r2); } dbg!(r1); // compile OK }

12
1
0
浏览量283
CTang

rust的clap如何传参数?

use std::fs::{File, OpenOptions}; use std::io::{BufRead, BufReader, Seek, SeekFrom, Write}; use std::path::Path; use std::thread; use std::time::Duration; use clap::{Command, arg}; fn main() { let mut log_file_path_str: &str = ""; let mut index_file_path_str: &str = ""; let matches = Command::new("watch_log") .version("1.0") .author("357888473@qq.com") .about("watch log and handle log every line") .subcommand_required(true) .arg_required_else_help(true) .subcommand( Command::new("logpath") .about("log's path") .arg(arg!(--logpath ).help("String").required(true)), ) .subcommand( Command::new("indexpath") .about("index's path") .arg(arg!(--indexpath ).help("save read log's line count").required(true)), ).get_matches(); match matches.subcommand() { Some(("logpath", sub_matches)) => { if let Some(input) = sub_matches.get_one::("logpath") { log_file_path_str = input; } }, Some(("indexpath", sub_matches)) => { if let Some(input) = sub_matches.get_one::("indexpath") { index_file_path_str = input; } }, _ => unreachable!(), } let log_file_path = Path::new(log_file_path_str); // 日志文件路径 let index_file_path = Path::new(index_file_path_str); // 索引文件路径 loop { let mut current_line_number = get_current_line_number(&index_file_path); // 获取上次读取的行数 let mut file = File::open(&log_file_path).unwrap(); // 打开日志文件 file.seek(SeekFrom::Start(0)).unwrap(); // 移动到文件开头 let reader = BufReader::new(&mut file); let mut new_lines = false; // 用于标记是否有新的日志行读取 for line in reader.lines().skip(current_line_number as usize) { let line = line.unwrap(); println!("{}", line); current_line_number += 1; // 更新当前行数 new_lines = true; } if new_lines { set_current_line_number(&index_file_path, current_line_number); // 更新索引文件中的行数 } println!("等待新日志..."); thread::sleep(Duration::from_secs(1)); // 暂停1秒钟,然后重新开始循环 } } fn get_current_line_number(index_file_path: &Path) -> u64 { if let Ok(index_content) = std::fs::read_to_string(index_file_path) { // 读取索引文件中的内容,转换为已读行数 if let Ok(current_line_number) = index_content.trim().parse::() { return current_line_number; } } 0 // 默认为0 } fn set_current_line_number(index_file_path: &Path, line_number: u64) { // 更新索引文件中的已读行数 let mut index_file = OpenOptions::new().write(true).create(true).truncate(true).open(index_file_path).unwrap(); writeln!(index_file, "{}", line_number).expect("无法写入索引文件"); } 请教给为,我用rust 通过命令行传入两个参数 logpath 和 indexpath,但是在执行的时候从事报错,我的执行命令是: cargo run -- logpath=hello.log --indexpath=index.txt 报错是 error: unexpected argument '--logpath' found 我试过各种命令哈,再比如: cargo run -- --logpath=hello.log --indexpath=index.txt 我查了很多资料,都无法解决,请各位大佬看下我上边代码,看看是否是我哪里有问题造成的,应该如何传参,谢谢

0
1
0
浏览量204
Yourselffff

Rust闭包返回引用时的规则及与变量本身的关系是什么?

fn main() { let mut my_string = String::from("Hello, Rust!"); let mut my_s_ref = &mut my_string; let consume_string = || { my_s_ref }; consume_string(); // consume_string(); println!("闭包外部消费字符串: {}", my_string); } fn main() { let my_string = String::from("Hello, Rust!"); let my_s_ref = & my_string; let consume_string = || { // my_s_ref.push_str("string"); my_s_ref }; consume_string(); consume_string(); println!("闭包外部字符串: {}", my_string); } 以上两段代码,一个返回String的可变引用,一个返回不可变引用,为什么返回可变引用的闭包实现了FnOnce,返回不可变引用的闭包只实现了Fn呢? rust返回引用时,遵循什么规则?和my_s_ref本身有关还是my_string有关?

0
1
0
浏览量193
我头像最美

rust axum 接口传参问题?

use axum::Json; use axum::extract::Request; use axum::http::header::HeaderMap; use axum::http::StatusCode; use serde::{Serialize, Deserialize}; use serde_json::{Value, json}; #[derive(Deserialize)] pub struct Info { name: String, age: u8, } pub async fn post_foo(info:Json, header: HeaderMap) -> Result, StatusCode> { let body = info; println!("name:{:?}, apge:{}, {header:?}", body.name, body.age); // 返回包含请求信息的 JSON 响应 Ok(Json(json!({"data": 42}))) } .route("/foo", get(api::get_foo).post(api::post_foo)) 各位好,我用rust的axum进行学习,现在有个问题,我提供的代码 第一个函数传了2个参数,一个是info,一个是header,问题是:第一个info加上后没问题,但函数如果加上第二个参数 下边一行.route中的api::post_foo就会报错,如果我把两个参数任意一个参数去掉,都不会报错。所以我就不知道该怎么办了,我其实是想拿到post的json数据,以及查看header,甚至想看request的一些数据。现在不知道怎么解决,还清各位大佬指点迷津,谢谢 补充: 我打该明白 因为两个参数类型一样,所以不让传,但有些时候,我需要获取request里的一些数据,做验证,以及使用用户传过来的参数做有业务,那么如何可以同时获取这些数据呢?求教大佬,找了好多资料都找不到,也可能是我能力还比较差,看不懂文档,请各位大佬帮助

0
1
0
浏览量169
强哥喝假酒

这段rust代码返回值和match匹配不符,请问什么问题,怎么改,谢谢?

async fn get_block(client: &Arc>, block_number: U64) -> Result, Box> { let block: Option>; let new_block_number: U64; if block_number == U64::from(0) { block = client.get_block(BlockNumber::Latest).await?; } else { block = client.get_block(block_number).await?; } match block { Some(b) => { if let Some(number) = b.number { new_block_number = number + 1; Ok(Some(BlockAndNumber { block: b, number: new_block_number })) } else { Err("Block number is None".into()) } }, None => { // 块不存在,返回 None Ok(None) }, } } fn main() { let result = get_block(&client, block_number).await?; match result { Ok(Some(block_and_number)) => { }, Ok(None) => { }, Err(e) => { println!("err: {}", e); } } } 各位好,我在get_block里 返回的是result 一个option和一个err,但是 在下面调取的时候,match result {} 我定一个ok 和 err报错,提示result的类型是个Option 理论上应该是result才对,想问我的代码哪里有问题,新手rust,还请各位多多帮助 这段代码有什么问题,怎么改

0
1
0
浏览量159
米斯达

rust 在 vscode 编辑器上使用遇到的问题?

有一个 rust 项目,项目的顶级目录作为当前的 vscode 工作区目录的子目录,这个时候发现 rust-analyzer 扩展无法识别出 rust 项目,除非把 vscode 工作区目录切换为项目目录,有没有办法让 vscode 工作区目录在项目目录上层时 rust-analyzer 扩展也能工作。 还有一个调试的问题,按照调试时的提示安装了 Microsoft C++ 扩展,这个时候调试可以正常执行,但是设置的断点无法打断。

0
1
0
浏览量139