fn main() {
let s = String::from("hello");
let hello = &s[5..];
println!("{}", hello);
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码没有恐慌?我认为这是索引超出内容大小的条件.此代码示例的输出不显示任何内容.
我想打印没有命名字段的元组枚举数据。
元组是将一些具有多种类型的其他值分组为一个复合类型的通用方法。
#[derive(Debug)]
enum Coin {
Penny(String),
Nickel { id: String },
}
fn main() {
let penny = Coin::Penny(String::from("penny"));
let nickel: Coin = Coin::Nickel { id: String::from("nickel") };
println!("{} {:?} ", penny.0, penny);
println!("{:?}", nickel);
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,Nickel是一个类似结构体的枚举变体,而 则Penny简称为枚举变体。
我收到编译器错误:
#[derive(Debug)]
enum Coin {
Penny(String),
Nickel { id: String },
}
fn main() {
let penny = Coin::Penny(String::from("penny"));
let nickel: Coin = Coin::Nickel { id: String::from("nickel") };
println!("{} {:?} ", penny.0, penny);
println!("{:?}", nickel); …Run Code Online (Sandbox Code Playgroud) 我正在研究Rust by Example并从"Alias"页面运行代码:
struct Point {
x: i32,
y: i32,
z: i32,
}
fn main() {
let mut point = Point { x: 0, y: 0, z: 0 };
{
let borrowed_point = &point;
let another_borrow = &point;
// Data can be accessed via the references and the original owner
println!(
"Point has coordinates: ({}, {}, {})",
borrowed_point.x, another_borrow.y, point.z
);
// Error! Can't borrow point as mutable because it's currently
// borrowed as immutable.
let …Run Code Online (Sandbox Code Playgroud) 如何为混合项目生成文档?怎么做呢?
使用Elixir混合项目的过程:
mix new greeter命令生成一个项目。 greeter.ex文件中添加了一块注释。 mix.exs文件中。 mix docs
命令生成文档。mix help 无法docs在可能的选项列表中提供任务:
mix # Runs the default task (current: "mix run")
mix app.start # Starts all registered apps
mix app.tree # Prints the application tree
mix archive # Lists installed archives
mix archive.build # Archives this project into a .ez file
mix archive.install # Installs an archive locally
mix archive.uninstall # Uninstalls archives
mix clean # Deletes generated application files …Run Code Online (Sandbox Code Playgroud)