rust

Can I get a Rust array's length with only a type, not a concrete variable?

纵然是瞬间 提交于 2020-12-06 07:06:52
问题 I want to rewrite the following C++ code into Rust: using storage = array<int, 3>; const size_t storage_len = sizeof(storage) / sizeof(storage::value_type); How can I get that constant length value without a concrete variable? As motivation, although it may seem trivial, I want to print the array's element count without declaring a variable. I know I could use a constant value or declare a dummy variable, but I wonder how Rust can preserve C++ code. I admit without a concrete variable is not

Confused about variable lifetime in tokio::spawn(async move

匆匆过客 提交于 2020-12-06 04:34:25
问题 I am new to rust and tokio async, and I am trying to compile the following seemingly straightforward code: async fn network_handler(network_config: &config::NetworkConfig) -> Result<(), Error> { Ok(()) } pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> { let network_config_copy = network_config.clone(); tokio::spawn(async move { network_handler(&network_config_copy).await }).await? } But the compiler complains: error: cannot infer an appropriate lifetime --> src

Confused about variable lifetime in tokio::spawn(async move

你。 提交于 2020-12-06 04:34:21
问题 I am new to rust and tokio async, and I am trying to compile the following seemingly straightforward code: async fn network_handler(network_config: &config::NetworkConfig) -> Result<(), Error> { Ok(()) } pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> { let network_config_copy = network_config.clone(); tokio::spawn(async move { network_handler(&network_config_copy).await }).await? } But the compiler complains: error: cannot infer an appropriate lifetime --> src

Is it legal to cast a struct to an array?

给你一囗甜甜゛ 提交于 2020-12-06 04:15:32
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

string concat error: expected a literal

ぐ巨炮叔叔 提交于 2020-12-06 04:15:32
问题 So I have this: struct User { reference: String, email: String, firstname: String, lastname: String } fn main() { let user = User { reference: "ref".to_string(), email: "em@ail.com".to_string(), firstname: "John".to_string(), lastname: "Doe".to_string() }; concat!(&user.firstname.as_string(), " ", &user.lastname.as_string()); } that's returning an error: error: expected a literal concat!(&user.firstname.as_string(), " ", &user.lastname.as_string()); ^~~~~~~~~~~~~~~~~~~~~~~~~~ But I thought

Is it legal to cast a struct to an array?

喜欢而已 提交于 2020-12-06 04:14:20
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Unable to compile Rust program: LNK1181: cannot open input file 'C:\\Program.obj'

泪湿孤枕 提交于 2020-12-05 12:36:26
问题 I am getting the error: C:\rust\hello_world\src>where link.exe C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.24.28314\bin\Hostx64\x86\link.exe C:\rust\hello_world\src>rustc main.rs error: linking with `link.exe` failed: exit code: 1181 | = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX64\\x86\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LARGEADDRESSAWARE" "/SAFESEH" "/LIBPATH:C:\\Users\\user\\.rustup\

开发者实测 M1 芯片:除了大型应用程序启动慢点,整体性能优秀!

限于喜欢 提交于 2020-12-05 11:07:41
来源|CSDN 作者|Peter 译者|弯月 苹果新出的M1芯片推出已经有一段时间了,这个让人兴奋的Mac专用芯片对于开发者来说究竟是不是友好?PSPDFKit的创始人、开发者Peter Steinberger购置了一台新版MacBook Air,从开发者的角度进行了测试。测试结果如何?快来看看吧!‍‍‍ 以下为译文: 最近大家都因为苹果的新 M1 芯片而兴奋不已。我也买了一台 MacBook Air 16GB M1 版,想看看是否能当作主力开发机使用。下面是我在测试了一个星期之后的报告。 01 Xcode Xcode 在 M1 上的运行速度非常快。编译 PSPDFKit PDF SDK( debug, arm64 版)几乎能与加载了目前最快的英特尔芯片的 MacBook Pro 媲美, 前者的编译时间是 8 分 49 秒,后者是 7 分 31 秒。 作为比较,我的黑苹果编译同样的项目需要不到5分钟。 对于一台无风扇的机器来说,这已经非常了不起了。苹果的上一款无风扇 MacBook 是 2017 年的 12 寸版本,用它编译同一个项目需要 41 分钟。 绝大多数测试都能通过,尽管我发现了我们之前没有注意到的一些针对 arm64 的错误,因为我们的 CI 并没有真正在 arm64 的硬件上运行过测试。模拟器采用与真实设备同样的架构很有好处,可以帮我们找到更多错误。 iOS 14

How do I add days to a Chrono UTC?

…衆ロ難τιáo~ 提交于 2020-12-04 15:43:20
问题 I am trying to find the preferred way to add days to a Chrono UTC . I want to add 137 days to the current time: let dt = UTC::now(); 回答1: Just use Duration and appropriate operator: use chrono::{Duration, Utc}; fn main() { let dt = Utc::now() + Duration::days(137); println!("today date + 137 days {}", dt); } Test on playground. 回答2: I just wanted to improve on @Stargateur answer. There is no need to use time crate, since chrono crate has Duration struct in it: extern crate chrono; use chrono:

How do I add days to a Chrono UTC?

这一生的挚爱 提交于 2020-12-04 15:34:11
问题 I am trying to find the preferred way to add days to a Chrono UTC . I want to add 137 days to the current time: let dt = UTC::now(); 回答1: Just use Duration and appropriate operator: use chrono::{Duration, Utc}; fn main() { let dt = Utc::now() + Duration::days(137); println!("today date + 137 days {}", dt); } Test on playground. 回答2: I just wanted to improve on @Stargateur answer. There is no need to use time crate, since chrono crate has Duration struct in it: extern crate chrono; use chrono: