rust-obsolete

How to convert char to string?

谁说我不能喝 提交于 2019-12-04 22:16:59
This Question pertains to a pre-release version of Rust. This younger Question is similar. I tried to print one symbol by io::println function fn main() { io::println('c'); } But I got next error: $ rustc pdst.rs pdst.rs:2:16: 2:19 error: mismatched types: expected `&str` but found `char` (expected &str but found char) pdst.rs:2 io::println('c'); ^~~ error: aborting due to previous error How to convert char to string? UPDATE Direct typecast does not work: let text:str = 'c'; let text:&str = 'c'; let text:@str = 'c'; let text:~str = 'c'; It returns: pdst.rs:7:13: 7:16 error: bare `str` is not a

Why does #[derive(Show)] not work anymore?

北城余情 提交于 2019-12-03 23:39:24
问题 With today's Rust nightly the following code doesn't compile anymore: #[derive(Show)] enum S { A, B } fn main() { println!("{}", S::A); } Instead it gives me the following error message: error: the trait `core::fmt::String` is not implemented for the type `S` Is there a way to get the old behaviour? Surely it can't be required to implement this by hand for each type. 回答1: The old Show trait was split into Display and Debug . Display is designed for user-facing output, and uses the blank

Declaring array using a constant expression for its size

别说谁变了你拦得住时间么 提交于 2019-12-01 04:37:11
I have a newtype wrapper around an array. I assumed that I could use size_of instead of manually passing the size of the array around, but the compiler thinks I'm wrong. use std::mem::{size_of, size_of_val}; #[repr(C, packed)] struct BluetoothAddress([u8, ..6]); fn main() { const SIZE: uint = size_of::<BluetoothAddress>(); let bytes = [0u8, ..SIZE]; println!("{} bytes", size_of_val(&bytes)); } ( playpen link ) I'm using the nightly: rustc 0.13.0-nightly (7e43f419c 2014-11-15 13:22:24 +0000) This code fails with the following error: broken.rs:9:25: 9:29 error: expected constant integer for

Why does #[derive(Show)] not work anymore?

拟墨画扇 提交于 2019-12-01 02:31:31
With today's Rust nightly the following code doesn't compile anymore: #[derive(Show)] enum S { A, B } fn main() { println!("{}", S::A); } Instead it gives me the following error message: error: the trait `core::fmt::String` is not implemented for the type `S` Is there a way to get the old behaviour? Surely it can't be required to implement this by hand for each type. huon The old Show trait was split into Display and Debug . Display is designed for user-facing output, and uses the blank/default format specifier (e.g. {} , {:.10} {foo:} are all using Display ) Debug is designed for debugging

Declaring array using a constant expression for its size

三世轮回 提交于 2019-12-01 02:12:45
问题 I have a newtype wrapper around an array. I assumed that I could use size_of instead of manually passing the size of the array around, but the compiler thinks I'm wrong. use std::mem::{size_of, size_of_val}; #[repr(C, packed)] struct BluetoothAddress([u8, ..6]); fn main() { const SIZE: uint = size_of::<BluetoothAddress>(); let bytes = [0u8, ..SIZE]; println!("{} bytes", size_of_val(&bytes)); } (playpen link) I'm using the nightly: rustc 0.13.0-nightly (7e43f419c 2014-11-15 13:22:24 +0000)

Two dimensional vectors in Rust

大兔子大兔子 提交于 2019-11-30 09:15:45
问题 Editor's note: This question predates Rust 0.1 (tagged 2013-07-03) and is not syntactically valid Rust 1.0 code. Answers may still contain valuable information. Does anyone know how to create mutable two-dimensional vectors in Rust and pass them to function to be manipulated? This is what I tried so far: extern crate std; fn promeni(rec: &[u8]) { rec[0][1] = 0x01u8; } fn main() { let mut rec = ~[[0x00u8,0x00u8], [0x00u8,0x00u8] ]; io::println(u8::str(rec[0][1])); promeni(rec); io::println(u8:

What is typestate?

夙愿已清 提交于 2019-11-29 22:07:36
What does TypeState refer to in respect to language design? I saw it mentioned in some discussions regarding a new language by mozilla called Rust. Matthieu M. Note: Typestate was dropped from Rust, only a limited version (tracking uninitialized and moved from variables) is left. See my note at the end. The motivation behind TypeState is that types are immutable, however some of their properties are dynamic, on a per variable basis. The idea is therefore to create simple predicates about a type, and use the Control-Flow analysis that the compiler execute for many other reasons to statically

Two dimensional vectors in Rust

爷,独闯天下 提交于 2019-11-29 14:17:34
Editor's note: This question predates Rust 0.1 (tagged 2013-07-03) and is not syntactically valid Rust 1.0 code. Answers may still contain valuable information. Does anyone know how to create mutable two-dimensional vectors in Rust and pass them to function to be manipulated? This is what I tried so far: extern crate std; fn promeni(rec: &[u8]) { rec[0][1] = 0x01u8; } fn main() { let mut rec = ~[[0x00u8,0x00u8], [0x00u8,0x00u8] ]; io::println(u8::str(rec[0][1])); promeni(rec); io::println(u8::str(rec[0][1])); } You could use the macro vec! to create 2d vectors. fn test(vec: &mut Vec<Vec<char>>

How would you implement a bi-directional linked list in Rust?

筅森魡賤 提交于 2019-11-28 20:33:56
Note that this question refers to a version of Rust before Rust 1.0. Although the syntax has changed, the concepts are still valid. You can easily implement a forwards only linked list using owned pointers, something like: struct Node<T> { next: Option<~Node<T>>, data: T } Imagine, though, if you want to efficiently implement a queue that supports four basic operations: push : add to end of list pop : remove and return from the end of the list unshift : add to the front of the list shift : remove and return from the end of the list In a language with normal pointers you might implement this

What is typestate?

不打扰是莪最后的温柔 提交于 2019-11-28 18:29:43
问题 What does TypeState refer to in respect to language design? I saw it mentioned in some discussions regarding a new language by mozilla called Rust. 回答1: Note: Typestate was dropped from Rust, only a limited version (tracking uninitialized and moved from variables) is left. See my note at the end. The motivation behind TypeState is that types are immutable, however some of their properties are dynamic, on a per variable basis. The idea is therefore to create simple predicates about a type, and