Читаем Rust by Example полностью

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

For slice::from_raw_parts, one of the assumptions which must be upheld is that the pointer passed in points to valid memory and that the memory pointed to is of the correct type. If these invariants aren't upheld then the program's behaviour is undefined and there is no knowing what will happen.

<p id="compatibility"><strong><a l:href="#compatibility">Compatibility</a></strong></p>

The Rust language is fastly evolving, and because of this certain compatibility issues can arise, despite efforts to ensure forwards-compatibility wherever possible.

   • Raw identifiers

<p id="raw_identifiers"><strong><a l:href="#raw_identifiers">Raw identifiers</a></strong></p>

Rust, like many programming languages, has the concept of "keywords". These identifiers mean something to the language, and so you cannot use them in places like variable names, function names, and other places. Raw identifiers let you use keywords where they would not normally be allowed. This is particularly useful when Rust introduces new keywords, and a library using an older edition of Rust has a variable or function with the same name as a keyword introduced in a newer edition.

For example, consider a crate foo compiled with the 2015 edition of Rust that exports a function named try. This keyword is reserved for a new feature in the 2018 edition, so without raw identifiers, we would have no way to name the function.

extern crate foo;

fn main() {

foo::try();

}

You'll get this error:

error: expected identifier, found keyword `try`

--> src/main.rs:4:4

|

4 | foo::try();

| ^^^ expected identifier, found keyword

You can write this with a raw identifier:

extern crate foo;

fn main() {

foo::r#try();

}

<p id="meta"><strong><a l:href="#meta">Meta</a></strong></p>

Some topics aren't exactly relevant to how you program but provide you tooling or infrastructure support which just makes things better for everyone. These topics include:

   • Documentation: Generate library documentation for users via the included rustdoc.

   • Playpen: Integrate the Rust Playpen(also known as the Rust Playground) in your documentation.

<p id="documentation"><strong><a l:href="#documentation">Documentation</a></strong></p>

Use cargo doc to build documentation in target/doc.

Use cargo test to run all tests (including documentation tests), and cargo test --doc to only run documentation tests.

These commands will appropriately invoke rustdoc (and rustc) as required.

<p id="doc_comments"><strong><a l:href="#doc_comments">Doc comments</a></strong></p>

Doc comments are very useful for big projects that require documentation. When running rustdoc, these are the comments that get compiled into documentation. They are denoted by a ///, and support Markdown.

#![crate_name = "doc"]

/// A human being is represented here

pub struct Person {

/// A person must have a name, no matter how much Juliet may hate it

name: String,

}

impl Person {

/// Returns a person with the name given them

///

/// # Arguments

///

/// * `name` - A string slice that holds the name of the person

///

/// # Examples

///

/// ```

/// // You can have rust code between fences inside the comments

/// // If you pass --test to `rustdoc`, it will even test it for you!

/// use doc::Person;

/// let person = Person::new("name");

/// ```

pub fn new(name: &str) -> Person {

Person {

name: name.to_string(),

}

}

/// Gives a friendly hello!

///

Перейти на страницу:

Похожие книги