rust

Rust 一个 RC RefCell 的小例子。

◇◆丶佛笑我妖孽 提交于 2020-04-24 09:10:52
Rc 和 Refcell 并不能违反 Rust 的借用原则,多个只读,一个可写借用。 use std::cell::RefCell; use std::rc::Rc; use core::borrow::BorrowMut; #[derive(Debug)] struct Tree { name: String, nodes: RefCell<Vec<Node>>, } #[derive(Debug)] struct Node { id: String, tree: Rc<Tree>, } fn main() { let tree1 = Tree {name: "tree1".into(), nodes: RefCell::new(vec![])}; let tree1rc = Rc::new(tree1); let node1 = Node {id: "node1".into(), tree: tree1rc.clone()}; let node2 = Node {id: "node2".into(), tree: tree1rc.clone()}; tree1rc.nodes.borrow_mut().push(node1); tree1rc.nodes.borrow_mut().push(node2); println!("tree1: {:#?}", tree1rc

rust 编译ios静态库

点点圈 提交于 2020-04-23 04:39:28
1. 安装交差编译的targets rustup target list 两个环境,真机与模拟器 x86_64-apple-ios aarch64-apple-ios 2.安装lipo cargo install cargo-lipo 3.编写代码 use num::{BigInt, ToPrimitive}; #[no_mangle] pub extern fn get_rand_key(p: u32) -> u32 { //随机一个小于P的数最为私钥 return rand::random::<u32>() % (p - 1); } #[no_mangle] pub extern fn get_public_key(g: u32,p: u32,pub_key: u32) -> i32 { return match BigInt::from(g).modpow(&BigInt::from(pub_key), &BigInt::from(p)).to_i32() { Some(n) =>n, None => -1 }; } #[no_mangle] pub extern fn get_private_key(p: u32,pub_key:i32,rand: u32)->i32{ return match BigInt::from(pub_key).modpow(&BigInt:

C++智能指针

房东的猫 提交于 2020-04-20 09:24:06
C++智能指针 来源 https://zhuanlan.zhihu.com/p/30933682 参考 https://www.zhihu.com/question/319277442/answer/1094961099 ======================== 智能指针只能代替 T* 的一部分功能,而这部分本来就不适合用 T* (因为容易造成bug)。 如果本身就没有所有权,Bjarne( P1408 )的建议是直接用 T* 。 ======================== 智能指针表示的是某个资源的“ 所有权 ”的概念, unique_ptr表示唯一的所有权; shared_ptr表示可共享的所有权; weak_ptr表示可共享的未来的所有权。 然而资源拥有者如果只是要把一个对象借给别人用一下,用完就归还呢? void borrow_sth(??? resource); 有两种选择: T* 和 const std::unique_ptr<T>& 我更喜欢用第一种 ======================== 嗯,大家都在说普通指针和智能指针,少说了一个“引用” 先考虑用引用和值,减少90%的指针,特别是函数之间,基本上没有必要传指针。 再考虑使用unique_ptr,替换类的成员不能用引用的情况下使用指针。 最后考虑使用share_ptr/weak_ptr

How to call C function in Rust

旧巷老猫 提交于 2020-04-18 05:54:08
问题 I'm a C programmer and I'm trying to call Rust function in my application and the rust function also need call C functions which exist at my application. I know that if I want call C function in Rust I have to do like this #[link(name = "mylib")] extern "C" { pub fn c_function(); } But the c_function doesn't exist in any lib but only in my application env now. For example: My C code is void c_function() { return 1; } void main() { rust_function(); } My Rust code is(cargo new --lib myrustlib)

How to call C function in Rust

徘徊边缘 提交于 2020-04-18 05:54:04
问题 I'm a C programmer and I'm trying to call Rust function in my application and the rust function also need call C functions which exist at my application. I know that if I want call C function in Rust I have to do like this #[link(name = "mylib")] extern "C" { pub fn c_function(); } But the c_function doesn't exist in any lib but only in my application env now. For example: My C code is void c_function() { return 1; } void main() { rust_function(); } My Rust code is(cargo new --lib myrustlib)

简介

落花浮王杯 提交于 2020-04-17 10:11:34
【推荐阅读】微服务还能火多久?>>> Yew 是什么? Yew 是一个设计先进的 Rust 框架,目的是使用 WebAssembly 来创建多线程的前端 web 应用。 基于组件的框架 ,可以轻松的创建交互式 UI。拥有 React 或 Elm 等框架经验的开发人员在使用 Yew 时会感到得心应手。 高性能 ,前端开发者可以轻易的将工作分流至后端来减少 DOM API 的调用,从而达到异常出色的性能。 支持与 Javascript 交互 ,允许开发者使用 NPM 包,并与现有的 Javascript 应用程序结合。 加入我们 😊 你可以在这里 GitHub issues page 报告 Bugs 或者是提出新的想法。 我们欢迎 pull requests 。 如果你想要帮助我们,先参考下 good first issues 吧! 我们的 Gitter chatroom 非常的热闹,也是一个问问题和解决问题的好地方! 我们的社区正在茁壮成长! 准备好开始了吗? 点击下面的链接,来学习并编写你的第一个 Yew 前端 App , 并通过丰富的社区示例项目来学习。 项目设置 / getting-started/project-setup 还没有完全信服? Yew 项目基于划时代的新技术,非常适合那些希望开发未来基础项目的开发者。接下来是一些我们相信 Yew 这样的框架将为成为未来 Web

Wasm-bindgen: access wasm instance's memory buffer (from JS)

≯℡__Kan透↙ 提交于 2020-04-16 06:07:20
问题 According to this github comment, I can re-create an Uint8ClampedArray or Uint8Array returned from Rust/wasm by accessing the memory of the wasm instance directly: const textureRaw = new Uint8ClampedArray(memory.buffer, texture.offset(), texture.size()); The thing is, the js files generated by wasm-bindgen already instantiate a wasm instance, and I'd want to access the memory of this particular instance but it doesn't seem to be exported: // XXXXX_bg.js const path = require('path').join(_

How to communicate a Rust and a Ruby process using a Unix socket pair

二次信任 提交于 2020-04-16 05:49:31
问题 I am trying to communicate a Rust process with a child Ruby process using a Unix socket pair. I have tried the same using only Ruby and it works, but I can't seem to get it to work with Rust. I have tried passing the "rust_socket" file descriptor to the Ruby script, passing the "ruby_socket" file descriptor to Ruby and different combinations of reading / writing to the socket. I feel like I should be passing the "ruby_socket" file descriptor, but when I do that I get a bad file descriptor

How do I make a TLS connection using the rustls library?

和自甴很熟 提交于 2020-04-16 05:13:20
问题 The documentation provides an example — unfortunately it does not compile; a lot of stuff got renamed and the interface of the ClientSession constructor changed. I managed to fix the errors to a point where it compiles, but not to a point where it works. Here is my best attempt at getting the minimum example to work: extern crate rustls; use io::Read; use io::Write; use rustls::Session; use std::io; fn main() { let mut socket = std::net::TcpStream::connect("www.google.com:443").unwrap(); let

What is the difference between literals and non-literals, other than the fact that non-literals go into the heap?

梦想的初衷 提交于 2020-04-16 04:06:21
问题 I am confused by the difference between literals and non-literals (the ones that go on the heap, I do not know what they are called). For example, taking the String type as an example: We’ve already seen string literals, where a string value is hardcoded into our program. String literals are convenient, but they aren’t always suitable for every situation in which you want to use text. One reason is that they’re immutable. ... I do not understand the above, as we have already seen an example