{"id":24669128,"url":"https://github.com/pymongo/learn_rust","last_synced_at":"2026-06-18T23:31:39.890Z","repository":{"id":37144176,"uuid":"252932303","full_name":"pymongo/learn_rust","owner":"pymongo","description":"Learn async, C ABI, FFI, http_client, macros on Rust","archived":false,"fork":false,"pushed_at":"2023-09-15T07:50:39.000Z","size":946,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-07T22:45:49.551Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pymongo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-04-04T07:04:02.000Z","updated_at":"2023-03-31T15:16:50.000Z","dependencies_parsed_at":"2025-09-07T22:45:20.287Z","dependency_job_id":"2663f36d-54e3-44b7-b2ba-c02e5627de2c","html_url":"https://github.com/pymongo/learn_rust","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pymongo/learn_rust","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymongo%2Flearn_rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymongo%2Flearn_rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymongo%2Flearn_rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymongo%2Flearn_rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pymongo","download_url":"https://codeload.github.com/pymongo/learn_rust/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pymongo%2Flearn_rust/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34511617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-18T02:00:06.871Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-26T09:19:39.124Z","updated_at":"2026-06-18T23:31:39.717Z","avatar_url":"https://github.com/pymongo.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learn Rust\n\n## file descriptor\n\nhttps://stackoverflow.com/questions/27665396/how-can-i-read-from-a-specific-raw-file-descriptor-in-rust\n\nfile descriptors index into a per-process file descriptor table maintained by the kernel\n文件描述符在数据结构上是非负整数，每个进程都有各自的文件描述符表，其中0-2分别是stdin,stdout,stderr\n文件描述符索引表类似虚拟内存，每个进程的fd索引表最终会映射到操作系统的fd表，类似每个进程的虚拟内存会最终映射到物理内存上，以此实现多个进程复用同一个块物理内存或fd\n每个进程的fd表会存在`/proc/${PID}/fd`，例如PID=1是systemd，/proc/1/fd# file 11 =\u003e 11: symbolic link to /proc/1/mountinfo\n\n// only work on linux\nuse std::os::unix::io::FromRawFd;\nlet mut f = unsafe { std::fs::File::from_raw_fd(3) };\n\n---\n\n## code snippets\n\n### measure a function time cost\n\ncargo bench or\n\n```text\nlet now = std::time::Instant::now();\n// call a function\nprintln!(\"{:?}\", now.elapsed());\n```\n\n### Rust没有函数重载，但是标准库IPv4的构造函数有类似函数重载的效果\n\n```rust\n// mock constructor overload in C++/java\nstruct Ip(u32);\n\nimpl From\u003cu32\u003e for Ip {\n    fn from(val: u32) -\u003e Self {\n        Self(val)\n    }\n}\n\nimpl From\u003c[u8; 4]\u003e for Ip {\n    fn from(val: [u8; 4]) -\u003e Self {\n        // bigger-endian\n        Self(\n            val[0] as u32 + (val[1] as u32)\n                \u003c\u003c 8 + (val[2] as u32)\n                \u003c\u003c 16 + (val[3] as u32)\n                \u003c\u003c 24,\n        )\n    }\n}\n```\n\n### 一次迭代同时求出最大值和最小值\n\n```rust\n#[test]\nfn iter_once_both_max_and_min() {\n    let nums = vec![1i32, 2, 3, 4, 5];\n    let (max, min) = nums.iter().fold((i32::MIN, i32::MAX), |(max, min), \u0026x| {\n        (max.max(x), min.min(x))\n    });\n    assert_eq!(max, *nums.iter().max().unwrap());\n    assert_eq!(min, *nums.iter().min().unwrap());\n}\n```\n\n---\n\n## CPU硬件相关的编程技术\n\n### simd和atomic\n```text\natomic和simd是Rust两个硬件API」，因为二者都需要考虑CPU架构x86或ARM是否支持\nCPU硬件的API atomic性能要比操作系统层面的信号量内存壁垒API快得多\n而simd则是更高效的利用CPU进行并行计算，提升运算速度\n```\n\nsimd的代码难理解难编译运行，故没有代码演示\n\n### false_sharing and cache_line_padded\n```text\n除了atomic和simd这两个要考虑CPU，多线程伪共享(False Sharing)及其解决方案「缓存行填充」也是CPU相关的编程技术\n为了避免线程1和线程2的thread_local变量内存布局分布在同一个CPU缓存行上，造成线程1和线程2不能同时读取同一缓存行的数据带来的伪共享问题，\n「就必须将多线程之间的数据隔离到不同的缓存行中」，从而提升并发性能\n```\n\n逐行遍历二维数组才能「命中CPU缓存」\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpymongo%2Flearn_rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpymongo%2Flearn_rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpymongo%2Flearn_rust/lists"}